Kush
Kush

Reputation: 1522

How do I run a migration again, without deleting all the newer migrations?

I had just installed devise so the table didn't have any data on it except one user (me).

I was re-doing the database all over again so I dropped it all. I did rails g scaffold to generate 6 new models and controllers and did rake db:migrate

In my /db/migrate directory I have the devise file with the filename 20130603211907_devise_create_users.rb

Here is the issue: If I do rake db:migrate:down VERSION=20130603211907 it will delete all the new migrations.

How do I run a migration again, without deleting all the newer migrations?

Upvotes: 38

Views: 61245

Answers (5)

Sachin R
Sachin R

Reputation: 11876

It will run the down and then the up step (This command can drop your table!):

rake db:migrate:redo VERSION=xxxxxxx

To prevent your table from being deleted you could do this in conjunction with commenting out the down step temporarily.

Upvotes: 103

woltob
woltob

Reputation: 380

You can call rake db:migrate:redo VERSION=20130603211907 which will rerun the specified version.

Also, if you have migrations that should only run when going up the migration chain (e.g. data transformation, copying, etc.), you can specify a

def up 
  do_something
end

and def down (going down), def change (both ways) method respectively.

To temporarily disable migrations, just, for example, rename the method def up to def up_ and it will be ignored when processing the migration chain.

Upvotes: 8

davegson
davegson

Reputation: 8331

If you are developing locally and it wouldn't hurt to remove all data from your models you could simply drop your db and then create & migrate from scratch:

Purge or recreate a Ruby on Rails database

Upvotes: 9

Kush
Kush

Reputation: 1522

Thanks for the help everyone. This is what worked for me:

WARNING: these commands will delete all data in your database!

rake db:drop
rake db:create
rake db:migrate

Upvotes: 14

bunty
bunty

Reputation: 1098

rake db:migrate:up VERSION=20090408054532

this will migrate all file upto VERSION=20090408054532

checkout Run a single migration file

Upvotes: 10

Related Questions