1
0
mirror of git://f0xx.org/ac/ac-deploy synced 2026-07-29 02:57:38 +03:00

deploy: idempotent migrations with schema_migrations tracking table

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Anton Afanasyeu
2026-06-28 22:47:04 +02:00
parent 9edcd44689
commit 8359608ecd

View File

@@ -15,6 +15,29 @@ else
fi
[ "$(hostname -s)" = cast01 ] || { echo "run on cast01 only"; exit 1; }
ensure_migrations_table() {
db="$1"
mariadb -u root "$db" <<'SQL'
CREATE TABLE IF NOT EXISTS schema_migrations (
migration VARCHAR(255) NOT NULL PRIMARY KEY,
applied_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
);
SQL
}
migration_applied() {
db="$1"
name="$2"
count=$(mariadb -u root -N -e "SELECT COUNT(*) FROM schema_migrations WHERE migration='${name}'" "$db" 2>/dev/null || echo 0)
[ "$count" -gt 0 ]
}
mark_migration() {
db="$1"
name="$2"
mariadb -u root -e "INSERT IGNORE INTO schema_migrations (migration) VALUES ('${name}')" "$db" 2>/dev/null || true
}
apply() {
f="$1"
[ -f "$f" ] || { echo "missing $f"; exit 1; }
@@ -26,8 +49,15 @@ apply_db() {
db="$1"
f="$2"
[ -f "$f" ] || { echo "skip (missing): $f"; return 0; }
name="$(basename "$f")"
ensure_migrations_table "$db"
if migration_applied "$db" "$name"; then
echo "skip (already applied): $name"
return 0
fi
echo "==> $db < $f"
mariadb -u root "$db" < "$f"
mark_migration "$db" "$name"
}
apply "$SQL_CRASHES/schema.mariadb.sql"