📖 Scenario: You are managing a Supabase project where you have applied database schema changes using migrations. Sometimes, you need to undo a migration to fix mistakes or revert to a previous state.Imagine you deployed a migration that added a new table, but you realized it was incorrect. You want to roll back that migration safely.
🎯 Goal: Learn how to roll back the last applied migration in Supabase using the CLI commands and understand the migration files involved.
📋 What You'll Learn
Create a migration file with a specific name
Add a configuration variable for the migration directory
Use the Supabase CLI command to roll back the last migration
Verify the rollback command is correctly configured
💡 Why This Matters
🌍 Real World
Rolling back migrations is common when a database change causes errors or needs adjustment. It helps maintain data integrity and application stability.
💼 Career
Database migration management is a key skill for backend developers, DevOps engineers, and cloud architects working with modern cloud databases like Supabase.
Progress0 / 4 steps
1
Create a migration file named 001_create_users_table.sql
Create a migration file called 001_create_users_table.sql with the following SQL content exactly: CREATE TABLE users (id SERIAL PRIMARY KEY, name TEXT NOT NULL);
Supabase
Hint
Migration files in Supabase are SQL files named with a number prefix and description.
2
Add a configuration variable MIGRATION_DIR set to ./supabase/migrations
Add a variable called MIGRATION_DIR and set it to the string "./supabase/migrations" to specify where migration files are stored.
Supabase
Hint
This variable helps you refer to the migrations folder easily in commands.
3
Use the Supabase CLI command to roll back the last migration
Write the Supabase CLI command to roll back the last migration using the supabase db reset --project-ref command. Use the exact command: supabase db reset --project-ref your-project-ref --force
Supabase
Hint
The db reset command rolls back all migrations and reapplies them. The --force flag skips confirmation.
4
Verify the rollback command includes the correct project reference
Ensure the rollback command includes the project reference your-project-ref as shown: supabase db reset --project-ref your-project-ref --force
Supabase
Hint
Double-check the command to avoid mistakes when rolling back migrations.
Practice
(1/5)
1. What does the command supabase db rollback do?
easy
A. It creates a new migration file.
B. It undoes the last applied database migration.
C. It applies all pending migrations to the database.
D. It deletes the entire database.
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 rollback specifically undoes the last migration applied to the database.
Final Answer:
It undoes the last applied database migration. -> Option B
Quick Check:
Rollback = undo last migration [OK]
Hint: Rollback means undo last migration applied [OK]
Common Mistakes:
Confusing rollback with applying migrations
Thinking rollback creates new migrations
Assuming rollback deletes the database
2. Which of the following is the correct syntax to rollback the last 3 migrations using Supabase CLI?
easy
A. supabase db rollback --steps 3
B. supabase db rollback -n 3
C. supabase rollback db 3
D. supabase db undo 3
Solution
Step 1: Recall the rollback command syntax
The Supabase CLI uses --steps to specify how many migrations to rollback.
Step 2: Match the correct option
supabase db rollback --steps 3 uses supabase db rollback --steps 3, which is the correct syntax.
Final Answer:
supabase db rollback --steps 3 -> Option A
Quick Check:
Use --steps to rollback multiple migrations [OK]
Hint: Use --steps to specify rollback count [OK]
Common Mistakes:
Using incorrect flags like -n instead of --steps
Wrong command order like 'rollback db'
Using 'undo' instead of 'rollback'
3. Given the following commands run in order: supabase db push supabase db push supabase db rollback --steps 1 What is the state of migrations after these commands?
medium
A. All migrations are rolled back except the last one.
B. No migrations are applied.
C. Both migrations are still applied.
D. Only the first migration is applied.
Solution
Step 1: Apply two migrations
Running supabase db push twice applies two migrations in order.
Step 2: Rollback one migration
The rollback with --steps 1 undoes the last applied migration, leaving the first migration applied.
Final Answer:
Only the first migration is applied. -> Option D
Quick Check:
Rollback 1 step removes last migration applied [OK]
Hint: Rollback with --steps 1 undoes last migration only [OK]
Common Mistakes:
Assuming rollback removes all migrations
Thinking rollback does not affect applied migrations
Confusing push and rollback order
4. You run supabase db rollback --steps 2 but get an error saying no migrations to rollback. What is the most likely cause?
medium
A. There are fewer than 2 migrations applied to rollback.
B. The migrations folder is missing.
C. The database connection is not configured.
D. Rollback command requires a --force flag.
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 A