🔧add init script for mysql container to run tests without db permission errors

This commit is contained in:
Abdullah Adeel 2022-01-28 14:52:07 +05:00
parent 455f06f90d
commit 8ddf8dbb90
2 changed files with 44 additions and 0 deletions

View File

@ -4,3 +4,7 @@ COPY ./compose/production/mysql/maintenance /usr/local/bin/maintenance
RUN chmod +x /usr/local/bin/maintenance/*
RUN mv /usr/local/bin/maintenance/* /usr/local/bin \
&& rmdir /usr/local/bin/maintenance
COPY ./compose/production/mysql/init.sh /docker-entrypoint-initdb.d
RUN sed -i 's/\r$//g' /docker-entrypoint-initdb.d/init.sh
RUN chown -R mysql:mysql /docker-entrypoint-initdb.d/init.sh

View File

@ -0,0 +1,40 @@
#!/bin/bash
initialize() {
{
mysql_note "Giving user ${MYSQL_USER} access to schema test_${MYSQL_DATABASE}"
docker_process_sql --database=mysql <<<"GRANT ALL ON \`test_${MYSQL_DATABASE//_/\\_}\`.* TO '$MYSQL_USER'@'%' ;"
# exporting dummy MYSQL_ONETIME_PASSWORD to avoid -> MYSQL_ONETIME_PASSWORD: unbound variable
export DUMMY_ONETIME_PASSWORD="$MYSQL_ROOT_PASSWORD"
} || {
exit 1
}
}
docker_process_sql() {
if [ -n "$MYSQL_DATABASE" ]; then
set -- --database="$MYSQL_DATABASE" "$@"
fi
mysql --protocol=socket -uroot --password="${MYSQL_ROOT_PASSWORD}" -hlocalhost --socket="${SOCKET}" --comments "$@"
}
# logging functions
mysql_log() {
local type="$1"; shift
# accept argument string or stdin
local text="$*"; if [ "$#" -eq 0 ]; then text="$(cat)"; fi
local dt; dt="$(date --rfc-3339=seconds)"
printf '%s [%s] [Entrypoint]: %s\n' "$dt" "$type" "$text"
}
mysql_note() {
mysql_log Note "$@"
}
until (initialize); do
>&2 echo 'Waiting for MYSQL to execute init'
sleep 1
done