0

My situation is the following: I was working on my rails app and decided to create a model so I used the command rails generate model Micropost content:string user_id:integer. After that, the migration file db/migrate/[timestamp]_create_microposts.rb was created, and started to play with it adding some columns and indexes. Finally I migrated using rake db:migrate.

The problem comes when I decided that I needed to go back and delete the model Micropost, so I made the mistake to go directly to delete the model using rails destroy model Micropostand then I realized that I needed to make a rollback to the database. But it doesn't work anymore because the migration file was deleted when I hitted rails destroy.

Is there any "nice" or "correct" way to go back to the initial database version, the one before I created the model Micropost?

My rake db:migrate:state is the following:

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20140101222024  Create users
   up     20140102220824  Add index to users email
   up     20140102222352  Add password digest to users
   up     20140105055539  Add remember token to users
   up     20140108013447  Add admin to users
   up     20140109211729  ********** NO FILE **********

By the way, solutions like creating a "dummy" migration file (as i found here) with the same name id is NOT a solution, it will only skip the rollback (the table Microposts will still exist). Also, I found this question useful, but is not the same problem, because I destroyed the model before rolling back (or migrating down to a previous version).

Bottom Line:

Is there a way to perform this rollback? or is just impossible because the migration file was deleted?

Community
  • 1
  • 1
juliancrg
  • 151
  • 2
  • 8

2 Answers2

0

It looks like you can just login to your database server and do a drop table users; (it seems you are doing all this locally, right?). You should really use version control - you wouldn't have such problems in the first place.

Paweł Obrok
  • 22,568
  • 8
  • 74
  • 70
  • I'm using git, but I didn't commit after creating the model, so migration file wasn't saved. Any other way to recover the file? – juliancrg Jan 11 '14 at 19:09
0

You have two options. You can drop the table by logging into your database server and using drop table microposts, or you can run rails g migration drop_microposts to generate a migration which you can then run to delete the table. After that migration is run, you can delete the migration.

Ryan Bigg
  • 106,965
  • 23
  • 235
  • 261
  • so the only solution is by undoing the migration by hand directly in the database? there's no way to make a rollback? – juliancrg Jan 11 '14 at 19:12
  • Not if your migration is missing... You can create the migration to drop the table as I suggested, or go into the database and delete it manually. – Ryan Bigg Jan 13 '14 at 04:07