我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
当前回答
在Rails 4中,所需要的是
$ rake db:schema:load
这将删除您的DB上的全部内容,并根据您的模式重新创建模式。Rb文件,而不必逐一应用所有迁移。
其他回答
根据你想要的,你可以使用…
rake db:创建
从config/database中从头构建数据库。yml,还是……
rake db:模式:负载
根据您的模式从头构建数据库。rb文件。
我在Terminal中使用了以下一行。
$ rake db:drop && rake db:create && rake db:migrate && rake db:schema:dump && rake db:test:prepare
我将其作为shell别名,并将其命名为remigrate
到现在为止,你可以很容易地“链接”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+
我使用:
删除数据库。 Rails db:create根据config/database.yml创建数据库
以上命令可能替换为rails db:reset。
不要忘记运行rails db:migrate来运行迁移。
在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 4中,所需要的是
$ rake db:schema:load
这将删除您的DB上的全部内容,并根据您的模式重新创建模式。Rb文件,而不必逐一应用所有迁移。