Explorar el Código

fix: init-admin.sh — add prisma db push before creating admin

Without schema sync, init container fails on fresh DB because
tables don't exist yet. Switch from `prisma migrate deploy`
(which requires a migrations directory) to `prisma db push`
which syncs the schema directly.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Claude Dev hace 1 mes
padre
commit
d28e6e903d
Se han modificado 1 ficheros con 22 adiciones y 10 borrados
  1. 22 10
      scripts/init-admin.sh

+ 22 - 10
scripts/init-admin.sh

@@ -1,7 +1,7 @@
 #!/bin/sh
 # VidReview Init Script
-#   - FRESH: creates admin + locks registration + saves credentials
-#   - UPDATE: skips, leaves DB intact
+#   - FRESH: runs DB migrations, creates admin + locks registration + saves credentials
+#   - UPDATE: skips, leaves DB and data intact
 
 DB_HOST="${DB_HOST:-vidreview-db}"
 DB_NAME="${DB_NAME:-vidreview}"
@@ -19,25 +19,37 @@ run_node() {
   docker exec "$API_CONTAINER" node "$@" 2>&1
 }
 
+run_api() {
+  docker exec "$API_CONTAINER" "$@" 2>&1
+}
+
 mkdir -p "$OUTPUT_DIR"
 
 echo "============================================================"
 echo "  VidReview Init Script"
 echo "============================================================"
 
-# Check if admin already exists
 echo ""
-echo "  Checking database..."
-
-COUNT_RAW=$(docker exec "$DB_HOST" psql -U "$DB_USER" -d "$DB_NAME" \
-  -t -c "SELECT COUNT(*) FROM \"User\" WHERE \"globalRole\"='ADMIN';" 2>&1)
-echo "  [debug] raw count: $COUNT_RAW"
+echo "  Checking database state..."
+
+# Run Prisma migrations first (creates tables on fresh DB)
+echo "  Running DB migrations..."
+# db push creates/updates tables without needing a migrations directory
+run_api npx prisma db push --accept-data-loss
+MIGRATE_EXIT=$?
+if [ "$MIGRATE_EXIT" -ne 0 ]; then
+  echo "  ERROR: db push failed (exit $MIGRATE_EXIT). Output above."
+  exit 1
+fi
+echo "  DB schema synced."
 
-ADMIN_COUNT=$(echo "$COUNT_RAW" | tr -d '[:space:]' | grep -E '^[0-9]+$' || echo "")
+# Check if admin already exists
+ADMIN_COUNT_RAW=$(run_psql -t -c "SELECT COUNT(*) FROM \"User\" WHERE \"globalRole\"='ADMIN';" 2>&1)
+ADMIN_COUNT=$(echo "$ADMIN_COUNT_RAW" | tr -d '[:space:]' | grep -E '^[0-9]+$' || echo "")
 
 if [ -z "$ADMIN_COUNT" ]; then
   echo "  ERROR: Could not read DB count."
-  echo "  Output was: $COUNT_RAW"
+  echo "  Output was: $ADMIN_COUNT_RAW"
   exit 1
 fi