4

New to Rails here. Couple of questions about migrations:

  1. I created a migration that I no longer want. I want to remove it. Is the correct command simply rails destroy migration AddMyColumnToMyModel ?

  2. Let's say I mistype that migration name that I want to destroy... Here's what happens when I attempt to destroy a migration that does not exist.

    $ rails destroy migration Blah
         invoke active_record
         remove migration.rb
    

    It says it's removing migration.rb... Is this a bad thing?

Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
Ricky
  • 2,850
  • 5
  • 28
  • 41

1 Answers1

5
  1. Sure, that's the right command. Just be careful: if you actually ran the unwanted migration by using rake db:migrate to commit the changes to your database, be sure to run this before anything else:

    rake db:rollback
    

    What that does is run the down method on your latest migration. It does absolutely the same thing as:

    rake db:migrate:down VERSION=20130529014413
    

    Where the version number corresponds to that of your latest migration. It can also take a STEP parameter in case you need to roll back a bunch of migrations instead of only one, like so:

    rake db:rollback STEP=3
    

    Of course, if you just generated your unwanted migration and never ran it, there's no need to roll anything back. You can use the command you posted or manually delete the corresponding file to get rid of it.

    Source: http://guides.rubyonrails.org/migrations.html#rolling-back

  2. Don't worry, that's not doing anything to your code.

Marcelo De Polli
  • 28,123
  • 4
  • 37
  • 47
  • Thanks @depa. So the proper process is: 1) run db:rollback, and 2) run the destroy command on the migration? Do I need to pass any paramters to db:rollback? E.g. how does it know where to rollback to? – Ricky May 30 '13 at 14:46
  • It rolls back the latest migration. Check my updated answer. Keep in mind that if you only *generated* the migration but didn't run it, there's no need to roll anything back. – Marcelo De Polli May 30 '13 at 14:50
  • Sorry @depa, one last question about this db:rollback. I understand that when there are `up` and `down` method defined in the migration, they correspond to applying of the migration and rollback of it... However, my migration only has a `change` method. Does this mean Rails automatically can infer what the proper rollback action is? – Ricky May 30 '13 at 16:06
  • That's right. The change method is equivalent to having up and down. – Marcelo De Polli May 30 '13 at 17:51