How can I migrate my database with rails to the first revision without dropping the database first?

前端 未结 3 1889
深忆病人
深忆病人 2020-12-14 21:04

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., b

相关标签:
3条回答
  • 2020-12-14 21:21

    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.

    0 讨论(0)
  • 2020-12-14 21:22
    rake db:migrate VERSION=0
    

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

    Update: I just tested this on Rails 3.2.1, and it still works. It runs the "down" part of all the migrations known to schema_migrations. I have no idea if it worked on 3.1 or not, but the comment below indicates that this feature was broken during that time.

    0 讨论(0)
  • 2020-12-14 21:23

    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.

    0 讨论(0)
提交回复
热议问题