mirror of
git://f0xx.org/ac/ac-deploy
synced 2026-07-29 02:57:38 +03:00
88 lines
2.6 KiB
Bash
88 lines
2.6 KiB
Bash
#!/bin/sh
|
|
# Load project schemas on cast01 primary only.
|
|
set -eu
|
|
# Lab-seeds SQL is under lab-seeds/backend/sql; fall back to legacy sql/ layout.
|
|
if [ -f /shared/cluster/lab-seeds/backend/sql/schema.mariadb.sql ]; then
|
|
SQL_BASE=/shared/cluster/lab-seeds/backend/sql
|
|
SQL_CRASHES="$SQL_BASE"
|
|
SQL_CRASHES_MIGRATIONS="$SQL_BASE/migrations"
|
|
SQL_URL_SHORTENER="$SQL_BASE"
|
|
else
|
|
SQL_BASE=/shared/cluster/sql
|
|
SQL_CRASHES="$SQL_BASE/crashes"
|
|
SQL_CRASHES_MIGRATIONS="$SQL_BASE/crashes/migrations"
|
|
SQL_URL_SHORTENER="$SQL_BASE/url_shortener"
|
|
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; }
|
|
echo "==> $f"
|
|
mariadb -u root < "$f"
|
|
}
|
|
|
|
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"
|
|
|
|
# Migrations after base schema (003 is for legacy; base schema already includes tickets)
|
|
for m in \
|
|
004_ticket_workflow.sql \
|
|
005_ticket_attachments.sql \
|
|
006_graph_sessions.sql \
|
|
007_remote_access.sql \
|
|
008_graph_sessions_indexes.sql \
|
|
008_auth_email_2fa.sql \
|
|
009_rssh_sessions.sql
|
|
do
|
|
apply_db androidcast_crashes "$SQL_CRASHES_MIGRATIONS/$m"
|
|
done
|
|
|
|
# Apply SFU rooms schema if present
|
|
apply_db androidcast_crashes "$SQL_CRASHES_MIGRATIONS/080_sfu_rooms.sql" 2>/dev/null || true
|
|
|
|
if [ -f "$SQL_URL_SHORTENER/schema.mariadb.sql" ] && \
|
|
[ "$SQL_URL_SHORTENER/schema.mariadb.sql" != "$SQL_CRASHES/schema.mariadb.sql" ]; then
|
|
apply "$SQL_URL_SHORTENER/schema.mariadb.sql"
|
|
fi
|
|
|
|
mariadb -u root -e "SHOW DATABASES; SELECT COUNT(*) AS crash_tables FROM information_schema.tables WHERE table_schema='androidcast_crashes'; SELECT COUNT(*) AS url_tables FROM information_schema.tables WHERE table_schema='url_shortener';"
|
|
echo "schemas_loaded_ok"
|