50 lines
1.8 KiB
Bash
Executable File
50 lines
1.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
# Run the repository DDL through the live compose MySQL container. Docker's
|
|
# /docker-entrypoint-initdb.d hook only runs for an empty volume, while local
|
|
# development volumes often outlive schema additions between services.
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
PROJECT_ROOT="$(cd "${SCRIPT_DIR}/.." && pwd)"
|
|
MYSQL_ROOT_PASSWORD="${MYSQL_ROOT_PASSWORD:-root}"
|
|
|
|
cd "${PROJECT_ROOT}"
|
|
|
|
SQL_FILES=(
|
|
"services/room-service/deploy/mysql/initdb/001_room_service.sql"
|
|
"services/user-service/deploy/mysql/initdb/001_user_service.sql"
|
|
"services/wallet-service/deploy/mysql/initdb/001_wallet_service.sql"
|
|
"services/activity-service/deploy/mysql/initdb/001_activity_service.sql"
|
|
"deploy/mysql/initdb/005_utf8mb4_chinese_support.sql"
|
|
"deploy/mysql/initdb/999_local_grants.sql"
|
|
)
|
|
|
|
# Keep MySQL as the only required dependency for this script. Business services
|
|
# are started after schema/grants are known to match the current repo.
|
|
docker compose up -d mysql >/dev/null
|
|
|
|
# `docker compose up` may recreate MySQL after command/config changes. Wait on
|
|
# the server port before applying DDL so initdb is reliable on fresh containers.
|
|
for _ in {1..60}; do
|
|
if docker compose exec -T mysql mysqladmin ping -h 127.0.0.1 -uroot -p"${MYSQL_ROOT_PASSWORD}" --silent >/dev/null 2>&1; then
|
|
break
|
|
fi
|
|
|
|
sleep 1
|
|
done
|
|
|
|
if ! docker compose exec -T mysql mysqladmin ping -h 127.0.0.1 -uroot -p"${MYSQL_ROOT_PASSWORD}" --silent >/dev/null 2>&1; then
|
|
printf 'mysql did not become ready before initdb\n' >&2
|
|
exit 1
|
|
fi
|
|
|
|
for sql_file in "${SQL_FILES[@]}"; do
|
|
if [[ ! -f "${sql_file}" ]]; then
|
|
printf 'missing mysql init file: %s\n' "${sql_file}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
printf 'applying mysql init file: %s\n' "${sql_file}"
|
|
docker compose exec -T mysql mysql --default-character-set=utf8mb4 -h 127.0.0.1 -uroot -p"${MYSQL_ROOT_PASSWORD}" < "${sql_file}"
|
|
done
|