Skip to content

Commit 597ac5b

Browse files
committed
move init-file processing into a function
files found within /docker-entrypoint-initdb.d/* are processed using specific rules based on their filename. It was previously difficult to re-use this logic recursively, or to add logic for handling additional filetypes. By moving this logic into a shell function, we both enable inner shell scripts to easily re-use the same logic (for example, for including files from additional directories or sub-directories), and the possibility of overriding these processing rules entirely, for example to enable handling of additional filename patterns. There is a potential change of functionality compared to the previous version: the mysql command can no-longer be overridden by changing the mysql variable. Instead, the mysql command may be overridden only by overriding the process_init_file() function. There is a potentially loss of functionality in that included scripts can no-longer use "break" to escape the file loop. There is presently no work-around for this, as this is not expected to have been an intended feature.
1 parent 30bf2b7 commit 597ac5b

File tree

4 files changed

+76
-28
lines changed

4 files changed

+76
-28
lines changed

5.5/docker-entrypoint.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ file_env() {
4040
unset "$fileVar"
4141
}
4242

43+
# usage: process_init_file FILENAME MYSQLCOMMAND...
44+
# ie: process_init_file foo.sh mysql -uroot
45+
# (process a single initializer file, based on its extension. we define this
46+
# function here, so that initializer scripts (*.sh) can use the same logic,
47+
# potentially recursively, or override the logic used in subsequent calls)
48+
process_init_file() {
49+
local f="$1"; shift
50+
local mysql=( "$@" )
51+
52+
case "$f" in
53+
*.sh) echo "$0: running $f"; . "$f" ;;
54+
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
55+
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
56+
*) echo "$0: ignoring $f" ;;
57+
esac
58+
echo
59+
}
60+
4361
_check_config() {
4462
toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" )
4563
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
@@ -169,13 +187,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
169187

170188
echo
171189
for f in /docker-entrypoint-initdb.d/*; do
172-
case "$f" in
173-
*.sh) echo "$0: running $f"; . "$f" ;;
174-
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
175-
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
176-
*) echo "$0: ignoring $f" ;;
177-
esac
178-
echo
190+
process_init_file "$f" "${mysql[@]}"
179191
done
180192

181193
if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then

5.6/docker-entrypoint.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ file_env() {
4040
unset "$fileVar"
4141
}
4242

43+
# usage: process_init_file FILENAME MYSQLCOMMAND...
44+
# ie: process_init_file foo.sh mysql -uroot
45+
# (process a single initializer file, based on its extension. we define this
46+
# function here, so that initializer scripts (*.sh) can use the same logic,
47+
# potentially recursively, or override the logic used in subsequent calls)
48+
process_init_file() {
49+
local f="$1"; shift
50+
local mysql=( "$@" )
51+
52+
case "$f" in
53+
*.sh) echo "$0: running $f"; . "$f" ;;
54+
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
55+
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
56+
*) echo "$0: ignoring $f" ;;
57+
esac
58+
echo
59+
}
60+
4361
_check_config() {
4462
toRun=( "$@" --verbose --help --log-bin-index="$(mktemp -u)" )
4563
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
@@ -169,13 +187,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
169187

170188
echo
171189
for f in /docker-entrypoint-initdb.d/*; do
172-
case "$f" in
173-
*.sh) echo "$0: running $f"; . "$f" ;;
174-
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
175-
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
176-
*) echo "$0: ignoring $f" ;;
177-
esac
178-
echo
190+
process_init_file "$f" "${mysql[@]}"
179191
done
180192

181193
if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then

5.7/docker-entrypoint.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ file_env() {
4040
unset "$fileVar"
4141
}
4242

43+
# usage: process_init_file FILENAME MYSQLCOMMAND...
44+
# ie: process_init_file foo.sh mysql -uroot
45+
# (process a single initializer file, based on its extension. we define this
46+
# function here, so that initializer scripts (*.sh) can use the same logic,
47+
# potentially recursively, or override the logic used in subsequent calls)
48+
process_init_file() {
49+
local f="$1"; shift
50+
local mysql=( "$@" )
51+
52+
case "$f" in
53+
*.sh) echo "$0: running $f"; . "$f" ;;
54+
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
55+
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
56+
*) echo "$0: ignoring $f" ;;
57+
esac
58+
echo
59+
}
60+
4361
_check_config() {
4462
toRun=( "$@" --verbose --help )
4563
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
@@ -174,13 +192,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
174192

175193
echo
176194
for f in /docker-entrypoint-initdb.d/*; do
177-
case "$f" in
178-
*.sh) echo "$0: running $f"; . "$f" ;;
179-
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
180-
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
181-
*) echo "$0: ignoring $f" ;;
182-
esac
183-
echo
195+
process_init_file "$f" "${mysql[@]}"
184196
done
185197

186198
if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then

8.0/docker-entrypoint.sh

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ file_env() {
4040
unset "$fileVar"
4141
}
4242

43+
# usage: process_init_file FILENAME MYSQLCOMMAND...
44+
# ie: process_init_file foo.sh mysql -uroot
45+
# (process a single initializer file, based on its extension. we define this
46+
# function here, so that initializer scripts (*.sh) can use the same logic,
47+
# potentially recursively, or override the logic used in subsequent calls)
48+
process_init_file() {
49+
local f="$1"; shift
50+
local mysql=( "$@" )
51+
52+
case "$f" in
53+
*.sh) echo "$0: running $f"; . "$f" ;;
54+
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
55+
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
56+
*) echo "$0: ignoring $f" ;;
57+
esac
58+
echo
59+
}
60+
4361
_check_config() {
4462
toRun=( "$@" --verbose --help )
4563
if ! errors="$("${toRun[@]}" 2>&1 >/dev/null)"; then
@@ -174,13 +192,7 @@ if [ "$1" = 'mysqld' -a -z "$wantHelp" ]; then
174192

175193
echo
176194
for f in /docker-entrypoint-initdb.d/*; do
177-
case "$f" in
178-
*.sh) echo "$0: running $f"; . "$f" ;;
179-
*.sql) echo "$0: running $f"; "${mysql[@]}" < "$f"; echo ;;
180-
*.sql.gz) echo "$0: running $f"; gunzip -c "$f" | "${mysql[@]}"; echo ;;
181-
*) echo "$0: ignoring $f" ;;
182-
esac
183-
echo
195+
process_init_file "$f" "${mysql[@]}"
184196
done
185197

186198
if [ ! -z "$MYSQL_ONETIME_PASSWORD" ]; then

0 commit comments

Comments
 (0)