如何在Ruby on Rails 3中立即恢复所有迁移?

Mis*_*hko 16 migration rake ruby-on-rails database-migration ruby-on-rails-3

我试着跑:

rake db:migrate VERSION=0
Run Code Online (Sandbox Code Playgroud)

它还原除了最后一个之外的所有迁移.

然后我试着跑:

rake db:migrate:down VERSION=<timestamp_of_last_migration>
Run Code Online (Sandbox Code Playgroud)

但它也没有恢复.为什么?

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

boy*_*ymc 11

如果您的数据库仅与此项目相关,并且您尝试撤消迁移中的所有内容,则只需删除数据库,然后运行rake db:create.

然后你准备好了一个空的数据库.

或者你是否有另一个原因试图运行下脚本?

  • 通过rails rake任务拖拽,看起来像rake db:drop db:create是唯一真正的选择.我会说迁移的原因不会让你无所事事,因为它们是为了移动INSIDE迁移.不重置为零. (2认同)

Ark*_*kan 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
Run Code Online (Sandbox Code Playgroud)


小智 7

还有另一种方式:

rake db:rollback STEP=100
Run Code Online (Sandbox Code Playgroud)

这将恢复最近100次迁移

取自http://guides.rubyonrails.org/migrations.html#rolling-back