我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
当前回答
3个选项,结果相同:
1. 所有的步骤:
$ rake db:drop # deletes the database for the current env
$ rake db:create # creates the database for the current env
$ rake db:schema:load # loads the schema already generated from schema.rb / erases data
$ rake db:seed # seed with initial data
2. 重置:
$ rake db:reset # drop / schema:load / seed
3.迁移:重置:
$ rake db:migrate:reset # drop / create / migrate
$ rake db:seed
注:
如果使用schema:load比执行所有迁移都快,但结果相同。 所有数据都将丢失。 您可以在一行中运行多个耙。 使用rails 3。
其他回答
因为在开发过程中,您总是希望重新创建数据库,所以可以在lib/tasks文件夹中定义一个rake任务。
namespace :db do
task :all => [:environment, :drop, :create, :migrate] do
end
end
在终端中运行
rake db:all
它将重新构建数据库
根据你想要的,你可以使用…
rake db:创建
从config/database中从头构建数据库。yml,还是……
rake db:模式:负载
根据您的模式从头构建数据库。rb文件。
更新:在Rails 5中,这个命令可以通过以下命令访问:
rails db:清除db:创建db:迁移RAILS_ENV=test
在最新的rails 4.2版本中,你现在可以运行:
rake db:purge
来源:提交
# 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
它可以像上面提到的那样一起使用:
rake db:purge db:create db:migrate RAILS_ENV=test
我使用:
删除数据库。 Rails db:create根据config/database.yml创建数据库
以上命令可能替换为rails db:reset。
不要忘记运行rails db:migrate来运行迁移。
您可以手动执行:
rake db:drop
rake db:create
rake db:migrate
或者只是rake db:reset,它将运行上述步骤,但也将运行您的db/seeds。rb文件。
一个额外的细微差别是rake db:reset直接从您的模式加载。Rb文件,而不是再次运行所有的迁移文件。
你的数据在任何情况下都会消失。