0
0
Supabasecloud~30 mins

Rolling back migrations in Supabase - Mini Project: Build & Apply

Choose your learning style9 modes available
Rolling Back Migrations in Supabase
📖 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.