我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
当前回答
你可以使用 Db:reset -用于运行Db:drop和Db:setup或 Db:migrate:reset -运行Db:drop, Db:create和Db:migrate。
依赖于你想要使用现有的模式
其他回答
我认为运行这个命令的最好方法是:
**rake db:reset** it does db:drop, db:setup
rake db:setup does db:create, db:schema:load, db:seed
在Rails 6中,有一种方便的方法来重置DB并再次播种:
rails db:seed:replant # Truncates tables of each database for current environment and loads the seeds
https://weblog.rubyonrails.org/2019/3/15/this-week-in-rails-security-fixes-bulk-insert-and-upsert-seeds-replanting/
更新:在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 4.2上,删除所有数据,但保留数据库
$ bin/rake db:purge && bin/rake db:schema:load
https://github.com/rails/rails/blob/4-2-stable/activerecord/CHANGELOG.md
您可以手动执行:
rake db:drop
rake db:create
rake db:migrate
或者只是rake db:reset,它将运行上述步骤,但也将运行您的db/seeds。rb文件。
一个额外的细微差别是rake db:reset直接从您的模式加载。Rb文件,而不是再次运行所有的迁移文件。
你的数据在任何情况下都会消失。