37

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?

Kush
  • 1,512
  • 2
  • 17
  • 32
  • I'm going to try `rake db:migrate` after changing the timestamp on the migrate and see what happens. – Kush Jun 07 '13 at 09:52

5 Answers5

98

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.

Jonatas Eduardo
  • 834
  • 9
  • 18
Sachin R
  • 11,606
  • 10
  • 35
  • 40
14

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
KazKazar
  • 121
  • 1
  • 4
  • 15
Kush
  • 1,512
  • 2
  • 17
  • 32
  • 4
    consider accepting the answer that worked for you instead of including the solution a second time in your own answer ;) – davegson Jun 07 '13 at 12:23
  • I did only `rails db:migrate` and it didn't erase my database :) just for the update – Alon Samuel Feb 05 '19 at 00:28
  • This is doing a lot more than what the OP asks for. This is reset of the database without any seed data. Basically starting over from scratch with migrations run. You would probably NEVER want to do this without a DB backup or on production. Also see [answers here](https://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaload) – lacostenycoder Oct 04 '19 at 12:53
10
rake db:migrate:up VERSION=20090408054532

this will migrate all file upto VERSION=20090408054532

checkout Run a single migration file

Community
  • 1
  • 1
bunty
  • 1,068
  • 6
  • 17
  • 2
    Do note that this silently fails to do anything if the migration has already been run, though. The only way I can see to re-`up` without running the down-migration beforehand is to comment everything out, `db:migrate:down`, uncomment it again, and then `db:migrate:up` as described. – Wolfgang Aug 26 '20 at 23:55
  • to avoid the query to silently fail you can remove it of the list of migration already done before `delete from schema_migrations where version = '20211020114124'` – bormat Sep 24 '21 at 15:20
9

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

Community
  • 1
  • 1
davegson
  • 8,205
  • 4
  • 51
  • 71
  • I thought a db:reset was the same as drop, create, migrate, but its not. Doing the steps individually worked for me and reran my modified migration on a local dev only greenfield app. – Daniel Nalbach Jul 03 '20 at 17:54
  • 3
    This is not the correct answer and is dramatically different than what OP asked for. – JP Silvashy Mar 25 '21 at 12:38
7

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.

woltob
  • 370
  • 4
  • 13