diff --git a/backend/prisma/migrations/20251004999999_reconcile_user_sessions_migration/migration.sql b/backend/prisma/migrations/20251004999999_reconcile_user_sessions_migration/migration.sql index 09b1087..1b207c9 100644 --- a/backend/prisma/migrations/20251004999999_reconcile_user_sessions_migration/migration.sql +++ b/backend/prisma/migrations/20251004999999_reconcile_user_sessions_migration/migration.sql @@ -4,17 +4,9 @@ DO $$ DECLARE - old_migration_exists boolean := false; table_exists boolean := false; - failed_migration_exists boolean := false; - new_migration_exists boolean := false; + migration_exists boolean := false; BEGIN - -- Check if the old migration name exists - SELECT EXISTS ( - SELECT 1 FROM _prisma_migrations - WHERE migration_name = 'add_user_sessions' - ) INTO old_migration_exists; - -- Check if user_sessions table exists SELECT EXISTS ( SELECT 1 FROM information_schema.tables @@ -22,54 +14,14 @@ BEGIN AND table_name = 'user_sessions' ) INTO table_exists; - -- Check if there's a failed migration attempt + -- Check if the migration record already exists SELECT EXISTS ( SELECT 1 FROM _prisma_migrations - WHERE migration_name = '20251005000000_add_user_sessions' - AND finished_at IS NULL - ) INTO failed_migration_exists; + WHERE migration_name = '20251005000000_add_user_sessions' + ) INTO migration_exists; - -- Check if the new migration already exists and is successful - SELECT EXISTS ( - SELECT 1 FROM _prisma_migrations - WHERE migration_name = '20251005000000_add_user_sessions' - AND finished_at IS NOT NULL - ) INTO new_migration_exists; - - -- FIRST: Handle failed migration (must be marked as rolled back) - IF failed_migration_exists THEN - RAISE NOTICE 'Found failed migration attempt - marking as rolled back'; - - -- Mark the failed migration as rolled back (required by Prisma) - UPDATE _prisma_migrations - SET rolled_back_at = NOW() - WHERE migration_name = '20251005000000_add_user_sessions' - AND finished_at IS NULL; - - RAISE NOTICE 'Failed migration marked as rolled back'; - - -- If table exists, it means the migration partially succeeded - IF table_exists THEN - RAISE NOTICE 'Table exists - migration was partially successful, will be handled by next migration'; - ELSE - RAISE NOTICE 'Table does not exist - migration will retry after rollback'; - END IF; - END IF; - - -- SECOND: Handle old migration name (1.2.7 -> 1.2.8+ upgrade) - IF old_migration_exists AND table_exists THEN - RAISE NOTICE 'Found 1.2.7 migration "add_user_sessions" - updating to timestamped version'; - - -- Update the old migration name to the new timestamped version - UPDATE _prisma_migrations - SET migration_name = '20251005000000_add_user_sessions' - WHERE migration_name = 'add_user_sessions'; - - RAISE NOTICE 'Migration name updated: add_user_sessions -> 20251005000000_add_user_sessions'; - END IF; - - -- THIRD: Handle case where table exists but no migration record exists (1.2.7 upgrade scenario) - IF table_exists AND NOT old_migration_exists AND NOT new_migration_exists THEN + -- If table exists but no migration record, create one + IF table_exists AND NOT migration_exists THEN RAISE NOTICE 'Table exists but no migration record found - creating migration record for 1.2.7 upgrade'; -- Insert a successful migration record for the existing table @@ -94,26 +46,10 @@ BEGIN ); RAISE NOTICE 'Migration record created for existing table'; - END IF; - - -- FOURTH: If we have a rolled back migration and table exists, mark it as applied - IF failed_migration_exists AND table_exists THEN - RAISE NOTICE 'Migration was rolled back but table exists - marking as successfully applied'; - - -- Update the rolled back migration to be successful - UPDATE _prisma_migrations - SET - finished_at = NOW(), - rolled_back_at = NULL, - logs = 'Reconciled from failed state - table already exists' - WHERE migration_name = '20251005000000_add_user_sessions'; - - RAISE NOTICE 'Migration marked as successfully applied'; - END IF; - - -- If no issues found - IF NOT old_migration_exists AND NOT failed_migration_exists AND NOT (table_exists AND NOT new_migration_exists) THEN - RAISE NOTICE 'No migration reconciliation needed'; + ELSIF table_exists AND migration_exists THEN + RAISE NOTICE 'Table exists and migration record exists - no action needed'; + ELSE + RAISE NOTICE 'Table does not exist - migration will proceed normally'; END IF; END $$;