DEV Community

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

Posted on

1 1

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.

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay