Supabase CLI migration repair after a failed db push: when is it safe to mark a migration reverted?
0 reputation · 13 Mar 2022, 04:22 UTC
0 reputation · 13 Mar 2022, 04:22 UTC
When supabase db push fails partway through a migration, the CLI does not roll back automatically. The documented recovery path is supabase migration repair, which marks a migration as applied or reverted in the schema_migrations table without re-running the SQL, or supabase db reset, which destroys local data. Neither option verifies that the recorded state matches the actual database state.
Assume a recent Supabase CLI version and a hosted project on the Pro plan with PITR enabled (7-day retention). The goal is a repeatable team procedure for deciding which recovery mechanism to use after a failed push: repair the migration record, restore via PITR, or rebuild from a branch.
The uncertainty is consistency: if a migration partially executed (some DDL committed, some not), marking it reverted may leave orphaned objects, while marking it applied may skip statements that never ran. PITR restores the whole cluster to a timestamp, not a single migration, and branching environments do not sync schema back to main automatically.
migration repair is safe versus when PITR or a full reset is the correct recovery path?35275 reputation · 13 Mar 2022, 10:03 UTC
Marking a migration as reverted using supabase migration repair is safe only if no DDL statements from that migration file were committed to the database. Because migration repair only modifies the schema_migrations tracking table and does not execute or undo SQL, marking a partially applied migration as reverted will leave orphaned objects in your database, leading to "already exists" errors during the next push.
migration repair --status reverted: When a schema audit confirms that zero tables, columns, constraints, or types defined in the failing migration exist in the database.migration repair --status applied: When a schema audit confirms that 100% of the intended changes were successfully committed, despite the CLI reporting a failure.supabase db reset: In local development environments where data persistence is not required and a clean slate is preferred over manual repair.Before choosing a repair status, you must verify the actual state of the database against the SQL in the migration file. Since most PostgreSQL DDL is transactional, a failure usually rolls back the entire block—unless you used non-transactional operations like CREATE INDEX CONCURRENTLY.
Run targeted queries against the information_schema for every object introduced in the failed file:
-- Check for a table introduced in the migration
SELECT table_name FROM information_schema.tables
WHERE table_schema = 'public' AND table_name = 'your_new_table';
-- Check for a column added to an existing table
SELECT column_name FROM information_schema.columns
WHERE table_schema = 'public' AND table_name = 'existing_table' AND column_name = 'new_column';
To validate your team's recovery procedure without risking production, use a local Docker-based Supabase stack in your CI pipeline:
supabase start.information_schema audit to verify the partial state.migration repair command and verify that subsequent migrations can be pushed without conflict.To provide a more specific recommendation, it is necessary to know if the failed migration contained non-transactional DDL (such as CONCURRENTLY keywords). If it did, the likelihood of a partial state is significantly higher, making PITR the safer default over migration repair.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.