如何在不删除数据库的情况下将我的数据库迁移到第一个版本? [英] How can I migrate my database with rails to the first revision without dropping the database first?
问题描述
我有一个数据库设置为我的Rails安装和一些迁移设置。我想能够将我的数据库重置为没有表/约束等,但是没有找到一个合理的方法来做这个,而不知道迁移的数量或第一次迁移的时间戳。这是我的选择,我看到他们:
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
其中20090701154839是与第一次迁移相关联的时间戳记 -
rake db:rollback STEP = 15
其中有15次迁移
rake db:migrate:reset
rake db:migrate:down VERSION=20090701154839
where 20090701154839 is the timestamp associated with the first migrationrake db:rollback STEP=15
where there have been 15 migrations
db:migrate:reset
的问题是它首先删除数据库(它 db:drop
, db:create
,然后 db:migrate
)。
The problem with db:migrate:reset
is that it drops the database first (it does db:drop
, db:create
, then db:migrate
).
db:migrate:down
的问题是我不想对开头的VERSION进行编码。
The problem with db:migrate:down
is that I don't want to encode the VERSION of the beginning.
db:rollback
的问题是我不知道回到开头的步骤数。
The problem with db:rollback
is that I don't know the number of steps it is back to the beginning.
我的选项是什么?
推荐答案
rake db:migrate VERSION=0
即使您使用较新的加有时间戳记的迁移文件,它也能正常工作。
It works even if you're using the newer timestamped migration files.
更新:我刚刚在Rails 3.2.1测试这个,它仍然可以工作。它运行 schema_migrations
已知的所有迁移的下部分。我不知道它是否工作在3.1或以下,但下面的注释表明这个功能在这段时间被打破。
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.
这篇关于如何在不删除数据库的情况下将我的数据库迁移到第一个版本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!