Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete migration files in Rails 3

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?

like image 300
alvincrespo Avatar asked Oct 06 '10 12:10

alvincrespo


People also ask

Should I delete old migrations?

You don't need to keep around your old migration files in a Rails app, because your database schema should be captured either in schema. rb or an equivalent SQL file that can be used to regenerate your schema. Migrations are not the authoritative source for your database schema. That role falls to either db/schema.

Can you edit a migration file Rails?

Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate.

How do I rollback migration in Rails?

You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.


2 Answers

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

like image 73
Fábio Batista Avatar answered Sep 22 '22 15:09

Fábio Batista


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

like image 38
Gediminas Avatar answered Sep 24 '22 15:09

Gediminas