migrate.sh 支持 DSN 与可选客户端路径
生产的宝塔机器把数据库配置放在一个 Go 风格的 IM_DB_DSN 里,而不是 compose 的那组 MYSQL_* 变量;同机自带的 mysql 客户端又缺 caching_sha2_password 插件, 连不上 MySQL 8.4。脚本原先把主机写死成 compose 的服务名 mysql,在非 Docker 环境用不了。 现在 MYSQL_HOST/MYSQL_PORT/MIGRATIONS_DIR/MYSQL_CLIENT 都可覆盖,未设置 MYSQL_USER 时从 IM_DB_DSN 解析;显式的 MYSQL_* 仍然优先。默认值保持 compose 原样,Docker 路径行为不变。 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5
parent
a024d59827
commit
6cd4f1b1db
+31
-7
@@ -1,18 +1,42 @@
|
|||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
set -eu
|
set -eu
|
||||||
|
|
||||||
mysql_args="--host=mysql --user=${MYSQL_USER} --database=${MYSQL_DATABASE} --default-character-set=utf8mb4 --batch --skip-column-names"
|
# Defaults match the compose topology; the BaoTa host runs MySQL outside Docker
|
||||||
|
# on the loopback, so host and port are overridable there.
|
||||||
|
mysql_host="${MYSQL_HOST:-mysql}"
|
||||||
|
mysql_port="${MYSQL_PORT:-3306}"
|
||||||
|
|
||||||
until MYSQL_PWD="${MYSQL_PASSWORD}" mysql ${mysql_args} --execute "SELECT 1" >/dev/null 2>&1; do
|
# The BaoTa host keeps one Go-style DSN instead of the compose MYSQL_* set, and
|
||||||
|
# its bundled client cannot speak caching_sha2_password, so the 8.x client has
|
||||||
|
# to be selectable. Explicit MYSQL_* values still win.
|
||||||
|
if [ -z "${MYSQL_USER:-}" ] && [ -n "${IM_DB_DSN:-}" ]; then
|
||||||
|
dsn="${IM_DB_DSN}"
|
||||||
|
MYSQL_USER="${dsn%%:*}"
|
||||||
|
rest="${dsn#*:}"
|
||||||
|
MYSQL_PASSWORD="${rest%@tcp(*}"
|
||||||
|
hostport="${rest##*@tcp(}"
|
||||||
|
hostport="${hostport%%)*}"
|
||||||
|
mysql_host="${hostport%%:*}"
|
||||||
|
mysql_port="${hostport##*:}"
|
||||||
|
database="${dsn##*)/}"
|
||||||
|
MYSQL_DATABASE="${database%%\?*}"
|
||||||
|
fi
|
||||||
|
mysql_bin="${MYSQL_CLIENT:-mysql}"
|
||||||
|
|
||||||
|
mysql_args="--host=${mysql_host} --port=${mysql_port} --user=${MYSQL_USER} --database=${MYSQL_DATABASE} --default-character-set=utf8mb4 --batch --skip-column-names"
|
||||||
|
|
||||||
|
until MYSQL_PWD="${MYSQL_PASSWORD}" "${mysql_bin}" ${mysql_args} --execute "SELECT 1" >/dev/null 2>&1; do
|
||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
|
|
||||||
MYSQL_PWD="${MYSQL_PASSWORD}" mysql ${mysql_args} --execute "CREATE TABLE IF NOT EXISTS schema_migrations (version VARCHAR(255) NOT NULL PRIMARY KEY, checksum CHAR(64) NOT NULL, applied_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"
|
MYSQL_PWD="${MYSQL_PASSWORD}" "${mysql_bin}" ${mysql_args} --execute "CREATE TABLE IF NOT EXISTS schema_migrations (version VARCHAR(255) NOT NULL PRIMARY KEY, checksum CHAR(64) NOT NULL, applied_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"
|
||||||
|
|
||||||
for file in /migrations/*.sql; do
|
migrations_dir="${MIGRATIONS_DIR:-/migrations}"
|
||||||
|
|
||||||
|
for file in "${migrations_dir}"/*.sql; do
|
||||||
version="$(basename "${file}")"
|
version="$(basename "${file}")"
|
||||||
checksum="$(sha256sum "${file}" | awk '{print $1}')"
|
checksum="$(sha256sum "${file}" | awk '{print $1}')"
|
||||||
existing="$(MYSQL_PWD="${MYSQL_PASSWORD}" mysql ${mysql_args} --execute "SELECT checksum FROM schema_migrations WHERE version='${version}'" 2>/dev/null || true)"
|
existing="$(MYSQL_PWD="${MYSQL_PASSWORD}" "${mysql_bin}" ${mysql_args} --execute "SELECT checksum FROM schema_migrations WHERE version='${version}'" 2>/dev/null || true)"
|
||||||
if [ -n "${existing}" ]; then
|
if [ -n "${existing}" ]; then
|
||||||
if [ "${existing}" != "${checksum}" ]; then
|
if [ "${existing}" != "${checksum}" ]; then
|
||||||
echo "Migration checksum mismatch: ${version}" >&2
|
echo "Migration checksum mismatch: ${version}" >&2
|
||||||
@@ -22,6 +46,6 @@ for file in /migrations/*.sql; do
|
|||||||
continue
|
continue
|
||||||
fi
|
fi
|
||||||
echo "Applying: ${version}"
|
echo "Applying: ${version}"
|
||||||
MYSQL_PWD="${MYSQL_PASSWORD}" mysql ${mysql_args} < "${file}"
|
MYSQL_PWD="${MYSQL_PASSWORD}" "${mysql_bin}" ${mysql_args} < "${file}"
|
||||||
MYSQL_PWD="${MYSQL_PASSWORD}" mysql ${mysql_args} --execute "INSERT INTO schema_migrations(version,checksum) VALUES('${version}','${checksum}')"
|
MYSQL_PWD="${MYSQL_PASSWORD}" "${mysql_bin}" ${mysql_args} --execute "INSERT INTO schema_migrations(version,checksum) VALUES('${version}','${checksum}')"
|
||||||
done
|
done
|
||||||
|
|||||||
Reference in New Issue
Block a user