How to delete migration files in Rails 3
Asked Answered
E

11

116

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?

Elbertina answered 6/10, 2010 at 12:51 Comment(0)
L
154

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

Lemonade answered 6/10, 2010 at 16:56 Comment(7)
Would deleting the migration and then running a db:migrate wipe out the table that was created?Elbertina
No, since Rails would not know how to delete it. It needs to call the self.down method defined on your migration to "downgrade" your database.Troopship
I just edited my response with 2 good references, check that out.Troopship
Oh, I see. Ok, thanks, this is exactly what I was wondering about. Thanks Again!Elbertina
If you've already deleted the file, without realizing rails will not let go that easily, rake db:migrate:status will show you the ID of the missing file, which you can use to recreate it. Once it's back, you can follow this answer's advice to victory.Lynlyncean
It seems that for everyone 1. step is clear, but for me it's not:( What does it mean "...on all environments, to the version before the one I want to delete."? Thanks!Madeup
@Lucas, once the migration file is removed, it cannot be reversed anymore. That's why you must revert it on all environments it already ran (production, development, testing, staging, etc) before deleting its file. That's also why I wrote that it's safer to just create another migration to revert that old one, once it's already ran on production.Troopship
T
85

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

Therapy answered 20/4, 2013 at 12:54 Comment(2)
"If your application is already on production or staging, it's safer to just write another migration that destroys your table or columns." So changes do not stay in database foreverRefrigeration
By the way, "d" stands for "destroy".Boote
V
26

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)

Visby answered 12/3, 2014 at 3:11 Comment(3)
rake destroy migration AddFileToTable rake aborted! Don't know how to build task 'destroy' (See full trace by running task with --trace)Datha
For #2 the correct command is: rails d migration migration_nameUpward
Thanks for the "remove timestamp_" hint that's what I neededImpose
B
10

We can use,

$ rails d migration table_name  

Which will delete the migration.

Beam answered 29/3, 2013 at 6:49 Comment(1)
I think it should be model name rather than table_nameGorky
S
10

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.

Sore answered 28/8, 2014 at 13:43 Comment(1)
If you specify a versionnumber, does it decide whether to go up or down accordingly?Accompanyist
E
10

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.

Estus answered 19/7, 2019 at 16:14 Comment(1)
This is correct. Keep in mind you must also revert any changes to the db from the original branch before switching to your new branch. Deleting the record of the schema migration will not also delete these orphaned tables.Overage
B
3

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.

Berthaberthe answered 20/2, 2013 at 23:41 Comment(0)
D
3

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 
Deodorize answered 17/9, 2019 at 19:1 Comment(0)
N
1

Look at 4.1 Rolling Back

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

$ rake db:rollback

Newburg answered 16/7, 2013 at 15:10 Comment(0)
J
0

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

Jeepers answered 20/5, 2015 at 19:28 Comment(0)
I
0

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

rails db:migrate VERSION=0

Insolation answered 15/10, 2019 at 14:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.