Andy from Webcrunch

Subscribe for email updates:

Portrait of Andy Leverenz
Andy Leverenz

July 9, 2022

Last updated November 5, 2023

Rails Quick Tips - Rolling Back Database Migrations

Migrating database changes is relatively straightforward using Ruby on Rails. You may have gotten ahead in the local development environment and need to modify something added long ago. Depending on whether your app is long-standing or brand new, you have a few options.

Generic rollback

The first database roll back option is a simple rails db:rollback command and this command rolls back a migration one step at a time.

rails db:rollback

Version rollback

The next database migration rollback option is to go back to a specific migration version using the timestamp on the files inside the db folder.

rails db:migrate:down VERSION=2022070100

Step rollback

Remembering the timestamp and grabbing it to copy into the VERSION environment variable is tedious, so I’ll often prefer using the STEP environment variable option.

That looks like this:

rails db:rollback STEP=2

Using the STEP environment variable, you can denote how many migrations to roll back to, which is easier to remember most of the time.

Seeing status

Finally, if you ever want to confirm the status of your migrations, you can run:

rails db:migrate:status

There are many database commands in the Ruby on Rails framework. We've only scratched the surface here. Look for upcoming tips where I go a bit deeper into those features.

Link this article
Est. reading time: 1 minutes
Stats: 6,280 views


Part of the Rails Quick Tips collection