What if you could instantly undo a risky database change with one command?
Why Rolling back migrations in Supabase? - Purpose & Use Cases
Imagine you just updated your database with new changes, but suddenly your app breaks because of a mistake in the update.
You try to fix it by manually undoing each change in the database, one by one, hoping nothing else breaks.
Manually reversing database changes is slow and risky.
You might forget a step or make a typo, causing more errors.
It's like trying to untangle a knot without knowing how it was tied.
Rolling back migrations lets you undo database changes safely and quickly.
It tracks each change and can reverse them automatically, so you don't have to guess or do it by hand.
DROP TABLE users; ALTER TABLE orders DROP COLUMN discount;
supabase db rollback --steps 1You can fix mistakes instantly and keep your database healthy without stress.
A developer deploys a new feature but finds a bug in the database schema.
Using rollback, they quickly undo the last migration and fix the bug before users notice.
Manual undoing of database changes is error-prone and slow.
Rolling back migrations automates safe reversal of changes.
This keeps your app stable and saves time during fixes.
Practice
supabase db rollback do?Solution
Step 1: Understand the purpose of rollback
Rollback means undoing changes, so this command reverses migrations already applied.Step 2: Identify the command effect
supabase db rollbackspecifically undoes the last migration applied to the database.Final Answer:
It undoes the last applied database migration. -> Option BQuick Check:
Rollback = undo last migration [OK]
- Confusing rollback with applying migrations
- Thinking rollback creates new migrations
- Assuming rollback deletes the database
Solution
Step 1: Recall the rollback command syntax
The Supabase CLI uses--stepsto specify how many migrations to rollback.Step 2: Match the correct option
supabase db rollback --steps 3 usessupabase db rollback --steps 3, which is the correct syntax.Final Answer:
supabase db rollback --steps 3 -> Option AQuick Check:
Use --steps to rollback multiple migrations [OK]
- Using incorrect flags like -n instead of --steps
- Wrong command order like 'rollback db'
- Using 'undo' instead of 'rollback'
supabase db pushsupabase db pushsupabase db rollback --steps 1What is the state of migrations after these commands?
Solution
Step 1: Apply two migrations
Runningsupabase db pushtwice applies two migrations in order.Step 2: Rollback one migration
The rollback with--steps 1undoes the last applied migration, leaving the first migration applied.Final Answer:
Only the first migration is applied. -> Option DQuick Check:
Rollback 1 step removes last migration applied [OK]
- Assuming rollback removes all migrations
- Thinking rollback does not affect applied migrations
- Confusing push and rollback order
supabase db rollback --steps 2 but get an error saying no migrations to rollback. What is the most likely cause?Solution
Step 1: Understand rollback error
The error indicates no migrations to rollback, meaning none or fewer than requested are applied.Step 2: Identify cause
If fewer than 2 migrations are applied, trying to rollback 2 steps causes this error.Final Answer:
There are fewer than 2 migrations applied to rollback. -> Option AQuick Check:
Rollback steps > applied migrations causes error [OK]
- Assuming rollback always works without applied migrations
- Thinking rollback needs --force flag
- Ignoring database connection issues
Solution
Step 1: Understand migration order
Migrations apply in order; you cannot rollback a middle migration without undoing later ones first.Step 2: Plan rollback and reapply
Rollback migrations after the 3rd (steps 2), then rollback the 3rd, then reapply the later migrations.Final Answer:
Rollback all migrations from the 3rd onwards, then reapply the ones after it. -> Option CQuick Check:
Rollback middle migration requires undoing later ones first [OK]
- Trying to rollback a single middle migration directly
- Deleting migration files without rollback
- Editing database manually causing inconsistency
Ready to prove it?
Pass the test and this pattern moves unknown
