我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:
rake db:recreate
这可能吗?
当前回答
使用像
rake db:drop db:create db:migrate db:seed
都在一行里。这更快,因为环境不会一次又一次地重新加载。
drop -删除数据库。
数据库:create -将创建数据库(主机/ Db /密码将从config/database.yml中获取)
Db:migrate -将从目录(Db /migration/.rb)*运行现有的迁移。
Db:seed -将在目录(Db /migration/seed.rb)中运行种子数据。
我通常喜欢:
rake db:reset
一下子做所有的事。
干杯!
其他回答
我使用:
删除数据库。 Rails db:create根据config/database.yml创建数据库
以上命令可能替换为rails db:reset。
不要忘记运行rails db:migrate来运行迁移。
更新:在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
你可以简单地跑
rake db:设置
如果你用一些数据创建了种子文件,它会删除数据库,创建新的数据库并从种子中填充db。
在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/
我在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+