How to revert the last migration?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
In software development, specifically within database management and version control, 'migration' refers to the process where changes in the database schema are applied in a version-controlled manner. Migrations help in maintaining consistency across different environments and aid in team collaboration. However, there can be instances where a recent migration might introduce bugs or issues, necessitating a revert to the previous state. This article delves into how one can revert the last migration in different systems such as Rails, Django, and Laravel, each of which handles migrations differently.
Reverting a Migration in Rails
Ruby on Rails uses Active Record Migrations to manage changes to the database schema. Here's how to revert the most recent migration:
- Use the Rollback Command: To revert the last migration in Rails, you can run:
This command reverts the last migration by invoking the down method of the Active Record Migration class.
- Reversing Specific Migration: If you need to revert a specific migration, use:
Replace your_migration_version_number with the actual version number of the migration you wish to revert.
Reverting a Migration in Django
Django maintains its database schema changes through Django Migrations. Here’s how to revert migrations in Django:
- Identify the Migration to Revert: First, list all migrations and their statuses with:
- Revert to a Specific Migration: To revert to a specific migration, use:
Replace app_label with the name of the Django app and migration_name with the name of the migration prior to the one you want to revert.
Reverting a Migration in Laravel
Laravel handles database schema changes using Migration classes. To revert a migration in Laravel, follow these steps:
- Run the Rollback Command:
This will roll back the last batch of migrations which might include multiple migration files if they were migrated together in the last batch.
- Rollback Multiple Steps: If you want to revert more than the last batch, you can specify a step option:
This rolls back the last two batches of migrations.
Key Points Summary
Here’s a table summarizing the commands to revert the last migration across different frameworks:
| Framework | Command to Revert Last Migration | Command to Revert Specific Migration |
| Rails | rake db:rollback | rake db:migrate:down VERSION=num |
| Django | python manage.py migrate app_label last_good_migration | python manage.py migrate app_label migration_name |
| Laravel | php artisan migrate:rollback | php artisan migrate:rollback --step=num_of_steps |
Additional Considerations
- Testing: Always test the reversal of migrations in a development environment before applying changes in production.
- Data Integrity: Be cautious with migrations that involve data manipulation. Reverting such migrations might not always restore the previous data state.
- Version Control: Keep migrations under version control. This practice helps in maintaining a history of changes and simplifies collaboration.
By understanding how to effectively revert a migration, developers can manage their database schema and minimize disruptions caused by problematic migrations.
Related reading
- How to revert the last migration?
- How to rotate a table 45 degrees and save the result into another table?
- How to run Django's test database only in memory?
- How to run multiple Hibernate SessionFactories with the SAME db schema using a distributed Ehcache
- How to run SQL script in MySQL?
- How to run SQL scripts and get data on application startup?
- How to sample large database and implement K-means and K-nn in R?
- How to save Amazon Redshift output to local CSV through SQL Workbench?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.