July 9, 2022
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.
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.
The next database migration rollback option is to go back to a specific migration version using the timestamp on the files inside the
rails db:migrate:down VERSION=2022070100
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
STEP environment variable, you can denote how many migrations to roll back to, which is easier to remember most of the time.
Finally, if you ever want to confirm the status of your migrations, you can run:
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.
Leave a reply
Part of the Rails Quick Tips collection
Sign in or Sign up to leave a response.