Initial project source
这个提交包含在:
@@ -0,0 +1,94 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
readonly SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
readonly PROJECT_ROOT="$(cd "${SCRIPT_DIR}/../.." && pwd)"
|
||||
readonly ENV_FILE="${AIXUN_DATAGOV_ENV_FILE:-${PROJECT_ROOT}/script/datagov/.env.datagov}"
|
||||
readonly CONTAINER_NAME="aixun-datagov-mysql"
|
||||
readonly CATALOG="${PROJECT_ROOT}/db/migration/catalog.yml"
|
||||
|
||||
fail() {
|
||||
echo "DataGov migration failed: $*" >&2
|
||||
exit 1
|
||||
}
|
||||
|
||||
only_version=""
|
||||
case "$#" in
|
||||
0) ;;
|
||||
2)
|
||||
[[ "$1" == "--only-version" ]] || fail "usage: $0 [--only-version VERSION]"
|
||||
only_version="$2"
|
||||
[[ "${only_version}" =~ ^[0-9]{8}\.[0-9]{3}$ ]] ||
|
||||
fail "invalid migration version: ${only_version}"
|
||||
;;
|
||||
*)
|
||||
fail "usage: $0 [--only-version VERSION]"
|
||||
;;
|
||||
esac
|
||||
|
||||
# Only an explicit migration invocation acquires this short process lock.
|
||||
# Development and read-only verification do not reserve a runtime lease.
|
||||
if [[ "${AIXUN_DATAGOV_RUNTIME_LOCKED:-0}" != "1" ]]; then
|
||||
exec python3 "${PROJECT_ROOT}/script/datagov/with-runtime-lock.py" "$0" "$@"
|
||||
fi
|
||||
|
||||
"${PROJECT_ROOT}/script/quality/verify-data-contracts.rb" --catalog-only
|
||||
|
||||
catalog_rows="$(ruby -ryaml -e '
|
||||
catalog_path = ARGV.fetch(0)
|
||||
only_version = ARGV.fetch(1)
|
||||
migrations = YAML.safe_load(File.read(catalog_path)).fetch("migrations")
|
||||
if only_version.empty?
|
||||
selected = migrations
|
||||
else
|
||||
selected = migrations.select { |migration| migration.fetch("version") == only_version }
|
||||
abort "migration version #{only_version} must match exactly one catalog row" unless selected.one?
|
||||
end
|
||||
selected.each do |migration|
|
||||
puts [migration.fetch("version"), migration.fetch("ownerUnit"), migration.fetch("path"), migration.fetch("sha256")].join("\t")
|
||||
end
|
||||
' "${CATALOG}" "${only_version}")"
|
||||
|
||||
[[ -f "${ENV_FILE}" ]] || fail "DataGov environment file is missing"
|
||||
|
||||
set -a
|
||||
# shellcheck disable=SC1090
|
||||
source "${ENV_FILE}"
|
||||
set +a
|
||||
|
||||
project_label="$(docker inspect "${CONTAINER_NAME}" --format '{{index .Config.Labels "com.docker.compose.project"}}' 2>/dev/null)" ||
|
||||
fail "DataGov MySQL container is unavailable"
|
||||
owner_label="$(docker inspect "${CONTAINER_NAME}" --format '{{index .Config.Labels "com.aixun.project"}}')"
|
||||
[[ "${project_label}" == "aixun-datagov-local" && "${owner_label}" == "aixun-datagov" ]] ||
|
||||
fail "refusing to use a MySQL container not owned by DataGov"
|
||||
|
||||
mysql_exec() {
|
||||
docker exec -e MYSQL_PWD="${AIXUN_DATAGOV_MYSQL_ROOT_PASSWORD}" "${CONTAINER_NAME}" \
|
||||
mysql --protocol=TCP -h127.0.0.1 --default-character-set=utf8mb4 \
|
||||
--batch --skip-column-names -uroot "${AIXUN_DATAGOV_DB_SCHEMA}" "$@"
|
||||
}
|
||||
|
||||
mysql_exec_stdin() {
|
||||
docker exec -i -e MYSQL_PWD="${AIXUN_DATAGOV_MYSQL_ROOT_PASSWORD}" "${CONTAINER_NAME}" \
|
||||
mysql --protocol=TCP -h127.0.0.1 --default-character-set=utf8mb4 \
|
||||
--batch --skip-column-names -uroot "${AIXUN_DATAGOV_DB_SCHEMA}"
|
||||
}
|
||||
|
||||
while IFS=$'\t' read -r version owner_unit relative_path expected_sha256 || [[ -n "${version}" ]]; do
|
||||
migration_path="${PROJECT_ROOT}/${relative_path}"
|
||||
history_exists="$(mysql_exec -e "SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=DATABASE() AND table_name='dg_schema_history';")"
|
||||
installed_checksum=""
|
||||
if [[ "${history_exists}" == "1" ]]; then
|
||||
installed_checksum="$(mysql_exec -e "SELECT checksum_sha256 FROM dg_schema_history WHERE version='${version}' AND success='1';")"
|
||||
fi
|
||||
|
||||
if [[ -n "${installed_checksum}" ]]; then
|
||||
[[ "${installed_checksum}" == "${expected_sha256}" ]] || fail "checksum drift for installed migration ${version}"
|
||||
echo "Migration ${version} already applied with the reviewed checksum."
|
||||
continue
|
||||
fi
|
||||
|
||||
mysql_exec_stdin < "${migration_path}"
|
||||
mysql_exec -e "INSERT INTO dg_schema_history(version, description, owner_unit, checksum_sha256, success) VALUES ('${version}', '$(basename "${relative_path}" .sql)', '${owner_unit}', '${expected_sha256}', '1');"
|
||||
echo "Migration ${version} applied for owner ${owner_unit}."
|
||||
done <<< "${catalog_rows}"
|
||||
在新工单中引用
屏蔽一个用户