如何在Ruby on Rails 3中一次性还原所有迁移?

16

我尝试运行:

rake db:migrate VERSION=0

它会还原所有迁移,除了最后一个

然后我尝试运行:

rake db:migrate:down VERSION=<timestamp_of_last_migration>

但是它也没有回滚。为什么?

是否有一条命令可以同时运行所有的 down 方法?


1
在搜索如何回滚所有迁移时,我偶然发现了这个问题。我只想说现在(Rails 3.2.9)rake db:migrate VERSION=0似乎可以正常工作,撤销所有迁移。 - janosrusiczki
这个问题似乎是错误的或过时的。rake db:migrate VERSION=0会回滚每一个迁移,包括第一个迁移 - Claudio Floreani
4个回答

11
如果你的数据库只与此项目相关,并且你想要撤销所有迁移,我建议你直接删除数据库,然后运行 rake db:create。
这样你就会得到一个空的数据库。
或者你有其他原因需要运行 down 脚本吗?

这就是我最终做的事情。但是,我仍然想知道为什么上面的命令不起作用... - Misha Moroshko
2
浏览了一下Rails的rake任务,看起来rake db:drop db:create是唯一真正的选择。我想说迁移不允许你回到空状态的原因是因为它们旨在在迁移中进行移动,而不是重置回零。 - boymc

10

你可以查看这个列表。

也许这能帮助你。

rake db:create[:all]: If :all not specified then create the database defined in config/database.yml for the current RAILS_ENV. If :all is specified then create all of the databases defined in config/database.yml.
rake db:fixtures:load: Load fixtures into the current environment's database. Load specific fixtures using FIXTURES=x,y
rake db:migrate [VERSION=n]: Migrate the database through scripts in db/migrate. Target specific version with VERSION=n
rake db:migrate:redo [STEP=n]: (2.0.2) Revert the database by rolling back "STEP" number of VERSIONS and re-applying migrations.
rake db:migrate:reset: (2.0.2) Drop the database, create it and then re-apply all migrations. The considerations outlined in the note to rake db:create apply.
rake db:reset: Drop and re-create database using db/schema.rb. The considerations outlined in the note to rake db:create apply.
rake db:rollback [STEP=N]: (2.0.2) Revert migration 1 or n STEPs back.
rake db:schema:dump: Create a db/schema.rb file that can be portably used against any DB supported by AR
rake db:schema:load: Load a schema.rb file into the database
rake db:sessions:clear: Clear the sessions table
rake db:sessions:create: Creates a sessions table for use with CGI::Session::ActiveRecordStore
rake db:structure:dump: Dump the database structure to a SQL file
rake db:test:clone: Recreate the test database from the current environment's database schema
rake db:test:clone_structure: Recreate the test databases from the development structure
rake db:test:prepare: Prepare the test database and load the schema
rake db:test:purge: Empty the test database

7

2

尝试:

rake db:migrate:down VERSION=<timestamp_of_first_migration>

这将运行self.down以进行第一次迁移,基本上会清除所有内容。至少对我来说是这样!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接