How to delete migration files in Rails 3

Ruby on-RailsRuby on-Rails-3File Io

Ruby on-Rails Problem Overview


I would like to remove/delete a migration file. How would I go about doing that? I know there are similar questions on here but as an update, is there a better way than doing script/destroy?

Also, should I do a db:reset or db:drop if I remove/delete a migration?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

I usually:

  1. Perform a rake db:migrate VERSION=XXX on all environments, to the version before the one I want to delete.
  2. Delete the migration file manually.
  3. If there are pending migrations (i.e., the migration I removed was not the last one), I just perform a new rake db:migrate again.

If your application is already on production or staging, it's safer to just write another migration that destroys your table or columns.

Another great reference for migrations is: http://guides.rubyonrails.org/migrations.html

Solution 2 - Ruby on-Rails

Another way to delete the migration:

$ rails d migration SameMigrationNameAsUsedToGenerate

Use it before rake db:migrate is executed because changes in database will stay forever :) - or remove changes in Database manually

Solution 3 - Ruby on-Rails

Run below commands from app's home directory:

  1. rake db:migrate:down VERSION="20140311142212" (here version is the timestamp prepended by rails when migration was created. This action will revert DB changes due to this migration)

  2. Run "rails destroy migration migration_name" (migration_name is the one use chose while creating migration. Remove "timestamp_" from your migration file name to get it)

Solution 4 - Ruby on-Rails

You can also run a down migration like so:

rake db:migrate:down VERSION=versionnumber

Refer to the Ruby on Rails guide on migrations for more info.

Solution 5 - Ruby on-Rails

We can use,

$ rails d migration table_name  

Which will delete the migration.

Solution 6 - Ruby on-Rails

None of these answers quite fit the problem i had as the migration i wanted to delete was missing: I had created and run a migration in some other branch, which was then discarded. The problem is when a migration is run, rails adds the version into a schema_migrations table in the database. So even if it isn't listed in your db structure or schema, rails looks for it. You can reveal these orphaned migrations by running:

rails db:migrate:status

Note the versions of the missing migrations and head into the db console:

rails dbconsole

Now remove the versions from the migration table manually:

delete from schema_migrations where version='<version>';

You should now be good.

Solution 7 - Ruby on-Rails

Sometimes I found myself deleting the migration file and then deleting the corresponding entry on the table schema_migrations from the database. Not pretty but it works.

Solution 8 - Ruby on-Rails

This also works in Rails 5.

If the migration was the most recent one you can remove the database column(s) that the migration added by doing:

rake db:rollback

then remove the migration file itself by running:

rails d migration WhateverYourMigrationWasNamed.rb 

Solution 9 - Ruby on-Rails

Look at 4.1 Rolling Back

http://guides.rubyonrails.org/migrations.html

$ rake db:rollback

Solution 10 - Ruby on-Rails

I just had this same problem:

  1. rails d migration fuu -this deleted the migration with the last timestamp
  2. rails d migration fuu -this deleted the other migration
  3. use git status to check that is not on the untracked files anymore
  4. rails g migration fuu

That fixed it for me

Solution 11 - Ruby on-Rails

Side Note: Starting at rails 5.0.0 rake has been changed to rails So perform the following

rails db:migrate VERSION=0

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionalvincrespoView Question on Stackoverflow
Solution 1 - Ruby on-RailsFábio BatistaView Answer on Stackoverflow
Solution 2 - Ruby on-RailsGediminasView Answer on Stackoverflow
Solution 3 - Ruby on-RailsfOxView Answer on Stackoverflow
Solution 4 - Ruby on-RailsVickyView Answer on Stackoverflow
Solution 5 - Ruby on-RailsAgnesView Answer on Stackoverflow
Solution 6 - Ruby on-RailsstackPusherView Answer on Stackoverflow
Solution 7 - Ruby on-RailsfrenesimView Answer on Stackoverflow
Solution 8 - Ruby on-Railsrandom_user_0891View Answer on Stackoverflow
Solution 9 - Ruby on-Railsuser1781626View Answer on Stackoverflow
Solution 10 - Ruby on-RailsMiguel AlatorreView Answer on Stackoverflow
Solution 11 - Ruby on-RailsMohamed DawView Answer on Stackoverflow