add incremental migration runner to deploy recipe — execute whole SQL files (not semicolon-split), catch 'no such column' for idempotent re-runs, merge into migrate.sh

This commit is contained in:
Pontoporeia
2026-05-11 15:54:09 +02:00
parent c1960d224b
commit cb6394e119
4 changed files with 84 additions and 39 deletions

View File

@@ -1,5 +1,5 @@
#!/usr/bin/env bash
# Initialise the XAMXAM SQLite database from schema.sql.
# Initialise the XAMXAM SQLite database from schema.sql, then run incremental migrations.
# Safe to run on existing databases — schema uses IF NOT EXISTS / INSERT OR IGNORE.
# Usage:
# scripts/migrate.sh # xamxam.db (default)
@@ -12,20 +12,27 @@ REPO_ROOT="${REPO_ROOT:-$(cd "$(dirname "$0")/.." && pwd)}"
if [ -d "$REPO_ROOT/app/storage" ]; then
SCHEMA="$REPO_ROOT/app/storage/schema.sql"
PROD_DB="$REPO_ROOT/app/storage/xamxam.db"
MIGRATIONS="$REPO_ROOT/app/migrations/run.php"
elif [ -f "$REPO_ROOT/storage/schema.sql" ]; then
SCHEMA="$REPO_ROOT/storage/schema.sql"
PROD_DB="$REPO_ROOT/storage/xamxam.db"
MIGRATIONS="$REPO_ROOT/migrations/run.php"
else
echo "ERROR: cannot find storage/schema.sql" >&2
exit 1
fi
init_db() {
local db="$1"
local label="$2"
echo " [$label] applying schema…"
sqlite3 "$db" < "$SCHEMA"
echo " [$label] done"
}
echo "──────────────────────────────────────────────"
echo " [schema] applying schema…"
if command -v syntaqlite &>/dev/null; then
syntaqlite analyze "$SCHEMA" || { echo "ERROR: schema.sql validation failed" >&2; exit 1; }
fi
sqlite3 "$PROD_DB" < "$SCHEMA"
echo " [schema] done"
init_db "$PROD_DB" "xamxam.db"
if [ -f "$MIGRATIONS" ]; then
echo "──────────────────────────────────────────────"
php "$MIGRATIONS" "$PROD_DB"
else
echo " [migrations] run.php not found — skipping"
fi