我有一个充满数据的Ruby on Rails开发数据库。我想删除所有东西,重建数据库。我正在考虑使用类似的东西:

rake db:recreate

这可能吗?


当前回答

在rails 4.2上,删除所有数据,但保留数据库

$ bin/rake db:purge && bin/rake db:schema:load

https://github.com/rails/rails/blob/4-2-stable/activerecord/CHANGELOG.md

其他回答

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。

根据你想要的,你可以使用…

rake db:创建

从config/database中从头构建数据库。yml,还是……

rake db:模式:负载

根据您的模式从头构建数据库。rb文件。

因为在开发过程中,您总是希望重新创建数据库,所以可以在lib/tasks文件夹中定义一个rake任务。

  namespace :db do
      task :all => [:environment, :drop, :create, :migrate] do
   end 
end

在终端中运行

rake db:all

它将重新构建数据库

我在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+

我知道两种方法:

这将重置你的数据库并重新加载你当前的模式:

rake db:reset db:migrate

这将破坏你的数据库,然后创建它,然后迁移你当前的模式:

rake db:drop db:create db:migrate

在这两种情况下,所有数据都会丢失。