1
0
mirror of git://f0xx.org/ac/ac-deploy synced 2026-07-29 04:38:48 +03:00
Files
ac-deploy/sim/cluster0/scripts/load-schemas.sh
2026-06-28 22:48:32 +02:00

116 lines
4.3 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
# Pre-populate tracking table for migrations already silently applied before this tracking was added.
# Detect by checking for a known column from each migration.
_existing=$(mariadb -u root -N -e "SELECT COUNT(*) FROM schema_migrations" "$db" 2>/dev/null || echo 0)
if [ "$_existing" -eq 0 ]; then
mariadb -u root "$db" <<'PRECHECK'
INSERT IGNORE INTO schema_migrations (migration) SELECT '004_ticket_workflow.sql'
FROM information_schema.columns
WHERE table_schema=DATABASE() AND table_name='tickets' AND column_name='workflow_state' LIMIT 1;
INSERT IGNORE INTO schema_migrations (migration) SELECT '005_ticket_attachments.sql'
FROM information_schema.tables
WHERE table_schema=DATABASE() AND table_name='ticket_attachments' LIMIT 1;
INSERT IGNORE INTO schema_migrations (migration) SELECT '006_graph_sessions.sql'
FROM information_schema.tables
WHERE table_schema=DATABASE() AND table_name='graph_sessions' LIMIT 1;
INSERT IGNORE INTO schema_migrations (migration) SELECT '007_remote_access.sql'
FROM information_schema.tables
WHERE table_schema=DATABASE() AND table_name='remote_access_sessions' LIMIT 1;
INSERT IGNORE INTO schema_migrations (migration) SELECT '008_graph_sessions_indexes.sql'
FROM information_schema.statistics
WHERE table_schema=DATABASE() AND table_name='graph_sessions' AND index_name='idx_gs_device' LIMIT 1;
INSERT IGNORE INTO schema_migrations (migration) SELECT '008_auth_email_2fa.sql'
FROM information_schema.tables
WHERE table_schema=DATABASE() AND table_name='auth_email_codes' LIMIT 1;
INSERT IGNORE INTO schema_migrations (migration) SELECT '009_rssh_sessions.sql'
FROM information_schema.tables
WHERE table_schema=DATABASE() AND table_name='rssh_sessions' LIMIT 1;
PRECHECK
fi
}
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"