#!/usr/bin/env bash # 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) set -euo pipefail REPO_ROOT="${REPO_ROOT:-$(cd "$(dirname "$0")/.." && pwd)}" # Detect layout: local dev has app/ subdir, server has files at repo root 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 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" if [ -f "$MIGRATIONS" ]; then echo "──────────────────────────────────────────────" php "$MIGRATIONS" "$PROD_DB" else echo " [migrations] run.php not found — skipping" fi