清除或重新创建 Ruby on Rails 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4116067/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Purge or recreate a Ruby on Rails database
提问by AnApprentice
I have a dev Ruby on Rails database full of data. I want to delete everything and rebuild the database. I'm thinking of using something like:
我有一个充满数据的开发 Ruby on Rails 数据库。我想删除所有内容并重建数据库。我正在考虑使用类似的东西:
rake db:recreate
Is this possible?
这可能吗?
回答by thenengah
I know two ways to do this:
我知道有两种方法可以做到这一点:
This will reset your database and reload your current schema with all:
这将重置您的数据库并重新加载您当前的架构:
rake db:reset db:migrate
This will destroy your db and then create it and then migrate your current schema:
这将破坏您的数据库,然后创建它,然后迁移您当前的架构:
rake db:drop db:create db:migrate
All data will be lost in both scenarios.
在这两种情况下,所有数据都将丢失。
回答by Eneko Alonso
On Rails 4, all needed is
在 Rails 4 上,所有需要的是
$ rake db:schema:load
That would delete the entire contents on your DB and recreate the schema from your schema.rb file, without having to apply all migrations one by one.
这将删除您数据库上的全部内容并从您的 schema.rb 文件重新创建架构,而不必一一应用所有迁移。
回答by TK.
I use the following one liner in Terminal.
我在终端中使用以下一个班轮。
$ rake db:drop && rake db:create && rake db:migrate && rake db:schema:dump && rake db:test:prepare
I put this as a shell alias and named it remigrate
我把它作为一个 shell 别名并命名它 remigrate
By now, you can easily "chain" Rails tasks:
现在,您可以轻松地“链接”Rails 任务:
$ rake db:drop db:create db:migrate db:schema:dump db:test:prepare # db:test:prepare no longer available since Rails 4.1.0.rc1+
回答by Robbie Guilfoyle
Update: In Rails 5, this command will be accessible through this command:
更新:在 Rails 5 中,可以通过以下命令访问此命令:
rails db:purge db:create db:migrate RAILS_ENV=test
rails db:purge db:create db:migrate RAILS_ENV=test
As of the newest rails 4.2 release you can now run:
从最新的 rails 4.2 版本开始,您现在可以运行:
rake db:purge
Source: commit
来源:提交
# desc "Empty the database from DATABASE_URL or config/database.yml for the current RAILS_ENV (use db:drop:all to drop all databases in the config). Without RAILS_ENV it defaults to purging the development and test databases."
task :purge => [:load_config] do
ActiveRecord::Tasks::DatabaseTasks.purge_current
end
It can be used together like mentioned above:
它可以像上面提到的那样一起使用:
rake db:purge db:create db:migrate RAILS_ENV=test
回答by coreyward
Depending on what you're wanting, you can use…
根据您的需要,您可以使用...
rake db:create
rake db:create
…to build the database from scratch from config/database.yml, or…
...从头开始构建数据库config/database.yml,或者...
rake db:schema:load
rake db:schema:load
…to build the database from scratch from your schema.rbfile.
...从您的schema.rb文件从头开始构建数据库。
回答by user2747051
From the command line run
从命令行运行
rake db:migrate:reset
回答by Manish Shrivastava
Use like
使用喜欢
rake db:drop db:create db:migrate db:seed
All in one line. This is faster since the environment doesn't get reloaded again and again.
全部在一行中。这更快,因为环境不会一次又一次地重新加载。
db:drop- will drop database.
db:drop- 将删除数据库。
db:create- will create database (host/db/password will be taken from config/database.yml)
db:create- 将创建数据库(主机/db/密码将从 config/database.yml 中获取)
db:migrate- will run existing migrations from directory (db/migration/.rb)*.
db:migrate- 将从目录(db/migration/.rb)*运行现有迁移。
db:seed- will run seed data possible from directory (db/migration/seed.rb)..
db:seed- 将运行可能来自目录(db/migration/seed.rb) 的种子数据..
I usually prefer:
我通常更喜欢:
rake db:reset
to do all at once.
一次性完成。
Cheers!
干杯!
回答by Малъ Скрылевъ
Just issue the sequence of the steps: drop the database, then re-create it again, migrate data, and if you have seeds, sow the database:
只需发出步骤的顺序:删除数据库,然后重新创建它,迁移数据,如果您有种子,则播种数据库:
rake db:drop db:create db:migrate db:seed
Since the default environment for rakeis development, in case if you see the exception in spec tests, you should re-create db for the testenvironment as follows:
由于默认环境rake是development,如果您在规范测试中看到异常,您应该为测试环境重新创建 db ,如下所示:
RAILS_ENV=test rake db:drop db:create db:migrate
In most cases the test database is being sowed during the test procedures, so db:seedtask action isn't required to be passed. Otherwise, you shall to prepare the database:
在大多数情况下,测试数据库是在测试过程中播种的,因此db:seed不需要通过任务操作。否则,您应准备数据库:
rake db:test:prepare
or
或者
RAILS_ENV=test rake db:seed
Additionally, to use the recreatetask you can add into Rakefilethe following code:
此外,要使用重新创建任务,您可以将以下代码添加到Rakefile中:
namespace :db do
task :recreate => [ :drop, :create, :migrate ] do
if ENV[ 'RAILS_ENV' ] !~ /test|cucumber/
Rake::Task[ 'db:seed' ].invoke
end
end
end
Then issue:
然后发出:
rake db:recreate
回答by Erik Trautman
You can manually do:
您可以手动执行以下操作:
rake db:drop
rake db:create
rake db:migrate
Or just rake db:reset, which will run the above steps but will also run your db/seeds.rbfile.
或者只是rake db:reset,它将运行上述步骤,但也会运行您的db/seeds.rb文件。
An added nuance is that rake db:resetloads directly from your schema.rbfile as opposed to running all the migrations files again.
一个额外的细微差别是rake db:reset直接从您的schema.rb文件加载,而不是再次运行所有迁移文件。
You data gets blown away in all cases.
在所有情况下,您的数据都会被吹走。
回答by user1358180
You can use this following command line:
您可以使用以下命令行:
rake db:drop db:create db:migrate db:seed db:test:clone

