views:

631

answers:

3

I have a database set up for my Rails installation and some migrations set up. I would like to be able to reset my database back down to having no tables/constraints/etc., but can't find a reasonable way to do this without knowing the number of migrations or the timestamp of the first migration. Here are my options as I see them:

  • rake db:migrate:reset
  • rake db:migrate:down VERSION=20090701154839 where 20090701154839 is the timestamp associated with the first migration
  • rake db:migrate:rollback STEP=15 where there have been 15 migrations

The problem with db:migrate:reset is that it drops the database first (it does db:drop, db:create, then db:migrate).

The problem with db:migrate:down is that I don't want to encode the VERSION of the beginning.

The problem with db:migrate:rollback is that I don't know the number of steps it is back to the beginning.

What are my options?

A: 

It's moderately hackish, but you could do a query to find the first VERSION in schema_migrations and then call rake db:migrate:down to get that VERSION (I'm assuming you want to package up a "reset" script for your app).

That will, of course, require that the down methods on all your migrations work properly.

edebill
+4  A: 
rake db:migrate VERSION=0

It works even if you're using the newer timestamped migration files.

jdl
Thank you; this is what I was looking for. I had tried rake:db:migrate:down VERSION=0, but down looks for particular versions, whereas db:migrate doesn't.
Rudd Zwolinski
A: 

In addition to jdl's (correct) solution above, another hack-y way to acheive this is to do rake db:rollback STEP=1000000, where 1000000 is a large number, larger than the number of migrations you'll ever have. It will rollback as far as it can up to 1000000 times, or however many times you put as the STEP.

Rudd Zwolinski