DEV Community

Cover image for Undo a specific migration in Ruby!
Jaspreet kaur
Jaspreet kaur

Posted on

Undo a specific migration in Ruby!

In Ruby, usually if you want to alter the table which you created using the command Eg. "bundle exec rake db:create_migration NAME=create_games", and then "bundle exec rake db:migrate", you need to create a new migration, where you can add or change the columns.

But there is another way how you can alter the table without creating a new migration.

Check the migration status by running below in your terminal.

bundle exec rake db:migrate:status
Enter fullscreen mode Exit fullscreen mode

This will show you status of all migrations you created.
If the status is up, that means this migration is active: it's been run, and has updated the database successfully!

database: db/development.sqlite3

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20210719113216  Create games
   up     20210719113221  Create reviews
   up     20221110080300  Create users
Enter fullscreen mode Exit fullscreen mode

Now, for eg. if you want to change the games table , you need run the command below, where VERSION= 'Migration ID' from the status.

rake db:migrate:down VERSION=20210719113216
Enter fullscreen mode Exit fullscreen mode
database: db/development.sqlite3

 Status   Migration ID    Migration Name
--------------------------------------------------
  down    20210719113216  Create games
   up     20210719113221  Create reviews
   up     20221110080300  Create users
Enter fullscreen mode Exit fullscreen mode

This will change the status from active to inactive (down), plus it will remove the table from database and schema too,
Check your schema file, there is no games table -
Image description

Now you can edit the table in the create_games migration, save and run-

bundle exec rake db:migrate
Enter fullscreen mode Exit fullscreen mode

This will create a new table with all the changes in the database and schema.

Top comments (0)