#!/bin/sh
# Apply tickets table migration (003) on existing MariaDB installs.
# Requires MySQL root (app user androidcast cannot CREATE TABLE).
#
# Usage (from backend/):
#   ./scripts/migrate-003-tickets.sh
#   CRASH_DB_NAME=my_db ./scripts/migrate-003-tickets.sh
set -eu

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SQL="$ROOT/sql/migrations/003_tickets.sql"
DB_NAME="${CRASH_DB_NAME:-androidcast_crashes}"

if [ ! -f "$SQL" ]; then
  echo "Missing $SQL" >&2
  exit 1
fi

echo "Applying tickets migration to database: $DB_NAME"
echo "File: $SQL"
echo "(you will be prompted for MySQL root password)"
mysql -u root -p "$DB_NAME" < "$SQL"

echo "Verifying tickets table..."
mysql -u root -p "$DB_NAME" -e "SHOW TABLES LIKE 'tickets'; DESCRIBE tickets;" | head -20

echo "Done. Restart php-fpm if needed, then reload Tickets in the console."
