Learn how to roll back a migration in Supabase with our step-by-step guide. Manually reverse changes, verify your schema, and update your migration records.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Step 1: Understanding Migrations in Supabase
Migrations in Supabase are a way to define and apply database schema changes over time. They are important for version control of the database schema and facilitate teamwork in collaborating environments. Migrations allow you to apply incremental changes to your database schema.
Step 2: Checking Existing Migrations
Before rolling back a migration, it's essential to know which migrations have already been applied to your database. You can use the Supabase CLI to list these migrations. Navigate to your Supabase project directory in your terminal or command prompt and execute:
supabase db reset
This will print a list of all migrations that have been applied. Take note of the migration you intend to roll back.
Step 3: Identifying the Target Migration
Decide which migration you would like to roll back. Typically, migrations are named using a timestamp and a descriptive name, such as 20230101123000_create_users_table.sql
. Identify the migration you wish to revert to or the precise one you want to roll back.
Step 4: Rolling Back a Migration
To roll back a migration in Supabase, you'll use a custom SQL script to undo the changes made by the migration you want to reverse. Unfortunately, Supabase currently does not have a direct CLI command for rolling back a migration automatically. You'll need to manually execute SQL that reverses the changes.
For example, if a migration added a new table, manually create a new SQL file to drop that table:
rollback_create_users_table.sql
DROP TABLE IF EXISTS users;
Execute this SQL using the command:
supabase db remote commit -f rollback_create_users\_table.sql
Step 5: Verifying the Rollback
After running the rollback SQL script, it's important to verify that the database schema is in the expected state. You can check the schema using the Supabase dashboard or execute SQL queries to confirm the absence of the changes made by the migration.
Step 6: Tracking Rollback Changes
Once you've confirmed that the rollback is successful, document this change appropriately. This documentation helps keep track of the changes in your migration history and assists other team members who may be pulling the latest changes. You might add notes in your version control system referencing the manual rollback SQL file.
Step 7: Updating Migration Records
It's good practice to update your migration records or logs to indicate that a particular migration was rolled back and no longer in effect. This step helps maintain clear insight into the current state of the database schema.
Step 8: Testing the Updated Schema
Finally, test your application to ensure that rolling back the migration has not caused any unexpected issues. Ensure that the application functions as expected with the updated schema and that no reliant features break due to the rollback.
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.