0
0
Supabasecloud~10 mins

Rolling back migrations in Supabase - Step-by-Step Execution

Choose your learning style9 modes available
Process Flow - Rolling back migrations
Start: Current DB state
Identify last migration applied
Run rollback command
Undo changes from last migration
Update migration history
DB state restored to previous version
End
This flow shows how the system finds the last migration applied, undoes its changes, updates the history, and restores the database to the previous state.
Execution Sample
Supabase
supabase migration rollback
-- Rolls back the last applied migration
This command reverses the last migration applied to the database, restoring its previous state.
Process Table
StepActionMigration TargetDB State BeforeDB State AfterNotes
1Check applied migrationsmigration_003Version 3 appliedVersion 3 appliedLast migration is migration_003
2Run rollback commandmigration_003Version 3 appliedRolling back migration_003Start undoing changes
3Undo schema changesmigration_003Version 3 appliedVersion 2 appliedSchema reverted to version 2
4Update migration historymigration_003Version 3 appliedVersion 2 appliedMigration_003 marked as rolled back
5Confirm rollbackmigration_003Version 2 appliedVersion 2 appliedRollback complete, DB at version 2
💡 Rollback stops after last migration is undone and DB state matches previous version
Status Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
Current Migrationmigration_003migration_003migration_003migration_003migration_002
DB Version33222
Migration History[1,2,3][1,2,3][1,2][1,2][1,2]
Key Moments - 3 Insights
Why does the rollback only undo the last migration and not all migrations?
Because the rollback command targets the most recent migration applied, as shown in execution_table step 1 and 2. It stops after undoing that migration to avoid losing all changes.
What happens if the rollback command fails during undoing schema changes?
The database state would remain inconsistent. The execution_table step 3 shows the schema reverting; if this fails, the rollback stops and manual fix is needed.
How does the system know which migration to rollback?
It checks the migration history to find the last applied migration, as seen in execution_table step 1 and variable_tracker 'Current Migration'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the DB version after step 3?
AVersion 3 applied
BVersion 1 applied
CVersion 2 applied
DVersion 4 applied
💡 Hint
Check the 'DB State After' column in row for step 3 in execution_table
At which step is the migration history updated to reflect the rollback?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Action' column in execution_table for when migration history changes
If the last migration was migration_004 instead of migration_003, what would change in the variable_tracker?
ACurrent Migration would start as migration_004
BDB Version would start as 2
CMigration History would be empty
DDB Version would be 5
💡 Hint
Refer to the 'Current Migration' row in variable_tracker and how it tracks the last migration
Concept Snapshot
Rolling back migrations:
- Finds last applied migration
- Runs rollback to undo its changes
- Updates migration history
- Restores DB to previous version
Use 'supabase migration rollback' command
Stops after undoing one migration
Full Transcript
Rolling back migrations means undoing the last set of changes made to the database by the most recent migration. The system first checks which migration was applied last. Then it runs a rollback command that reverses the schema or data changes from that migration. After undoing, it updates the migration history to mark that migration as rolled back. Finally, the database is restored to the previous version state. This process stops after one migration is undone to avoid losing all changes. If rollback fails during undoing, the database may become inconsistent and need manual fixing. The command used is 'supabase migration rollback'.