diff --git a/backend/scripts/migrate-to-postgres.js b/backend/scripts/migrate-to-postgres.js index aaa59e7..ff113de 100644 --- a/backend/scripts/migrate-to-postgres.js +++ b/backend/scripts/migrate-to-postgres.js @@ -190,8 +190,8 @@ function getTableMigrations() { { src: 'is_active', dest: 'is_active', transform: v => v === 1 || v === true }, { src: 'created_at', dest: 'created_at' }, { src: 'last_login', dest: 'last_login' }, - { src: 'user_group', dest: 'user_group' }, - { src: 'bu_teams', dest: 'bu_teams' }, + { src: 'user_group', dest: 'user_group', transform: v => v || 'Read_Only' }, + { src: 'bu_teams', dest: 'bu_teams', transform: v => v || '' }, ], conflict: '(id) DO NOTHING', }, @@ -584,7 +584,7 @@ async function migrate() { return migration.columns.map(col => { let value = row[col.src]; if (value === undefined) value = null; - if (col.transform && value !== null) { + if (col.transform) { value = col.transform(value); } return value; diff --git a/scripts/deploy-postgres.sh b/scripts/deploy-postgres.sh index cbf1c18..c0bc81f 100755 --- a/scripts/deploy-postgres.sh +++ b/scripts/deploy-postgres.sh @@ -88,7 +88,16 @@ echo "✓ Dependencies installed" echo "" echo "── Step 5: Running data migration (SQLite → Postgres) ──" if [ -f backend/cve_database.db ]; then - node backend/scripts/migrate-to-postgres.js + if ! node backend/scripts/migrate-to-postgres.js; then + echo "" + echo "⚠ Migration failed — resetting database and retrying..." + PGPASSWORD=sV4xmC9xAUCFop0ypxMVS056QgPqGrX psql -h localhost -p 5433 -U steam -d cve_dashboard -c " +DROP SCHEMA public CASCADE; +CREATE SCHEMA public; +" >/dev/null 2>&1 + echo "✓ Schema reset" + node backend/scripts/migrate-to-postgres.js + fi else echo "⚠ No SQLite database found — skipping migration (fresh install)" fi diff --git a/scripts/reset-and-migrate.sh b/scripts/reset-and-migrate.sh new file mode 100755 index 0000000..f22e313 --- /dev/null +++ b/scripts/reset-and-migrate.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# ============================================================================= +# reset-and-migrate.sh — Drop all Postgres data and re-run the migration +# ============================================================================= +# Use this when the migration partially failed and you need a clean slate. +# Safe to run multiple times — it always starts fresh. +# +# What this does: +# 1. Drops and recreates the public schema (wipes all tables) +# 2. Runs the migration script (recreates schema + copies SQLite data) +# +# Usage: +# chmod +x scripts/reset-and-migrate.sh +# ./scripts/reset-and-migrate.sh +# ============================================================================= + +set -e + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" +PROJECT_DIR="$(dirname "$SCRIPT_DIR")" +cd "$PROJECT_DIR" + +echo "" +echo "── Resetting Postgres database ──" + +PGPASSWORD=sV4xmC9xAUCFop0ypxMVS056QgPqGrX psql -h localhost -p 5433 -U steam -d cve_dashboard -c " +DROP SCHEMA public CASCADE; +CREATE SCHEMA public; +" +echo "✓ Schema dropped and recreated" + +echo "" +echo "── Running migration ──" +node backend/scripts/migrate-to-postgres.js + +echo "" +echo "✓ Done. Postgres is ready."