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?
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.
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.
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.
I usually:
rake db:migrate VERSION=XXX
on all environments, to the version before the one I want to delete.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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With