Search code examples
ruby-on-railsrubyrollbackdbmigrate

Rails: Rollback Mistake


I have created a table called "property" and I did not migrate it yet. I then wanted to delete the table, so when I did rake db:rollback as below. My previously created table, which I did not want to delete at all, is now reverted. How should I revive my model "comment" and instead delete the last model "property" only.

Some comments say do "redo" or "run migrate" but if I do a rake db:migrate, I feel like it will delete the comment model permanently. Am I wrong? What is right solution to bring back my comment model?

:~/workspace (revoke) $ rails g model property title address note price$ priceW 
Running via Spring preloader in process 3066
      invoke  active_record
      create    db/migrate/20160517222114_create_properties.rb
      create    app/models/property.rb
      invoke    test_unit
      create      test/models/property_test.rb
      create      test/fixtures/properties.yml
:~/workspace (revoke) $ bundle exec rake db:rollback
== 20160506122941 CreateComments: reverting ===================================
-- drop_table(:comments)
   -> 0.0011s
== 20160506122941 CreateComments: reverted (0.0096s) ==========================

Solution

  • Once you've done the rake db:rollback, you've made a change to the database. In this case, it dropped your comments table, as you can see in the output: drop_table(:comments). The data is gone, already. We've all been there at least once.

    Running rake db:migrate will restore the comments table structure, but not the data in it. In this situation, if you want to get to the point that your new table property isn't migrated, you can run rake db:migrate to run all new migrations, and then rake db:rollback once to rollback the migration for property.