rake db:migrate db:reset和db:schema:load之間的區別

本文翻譯自:Difference between rake db:migrate db:reset and db:schema:load

The difference between rake db:migrate and rake db:reset is pretty clear in my head. rake db:migraterake db:reset之間的區別非常明顯。 The thing which I don't understand is how rake db:schema:load different from the former two. 我不明白的是rake db:schema:load與前兩個不同。

Just to be sure that I am on the same page: 只是爲了確保我在同一頁面上:

  • rake db:migrate - Runs the migrations which haven't been run yet. rake db:migrate - 運行尚未運行的遷移。
  • rake db:reset - Clears the database (presumably does a rake db:drop + rake db:create + rake db:migrate ) and runs migration on a fresh database. rake db:reset - 清除數據庫(可能是rake db:drop + rake db:create + rake db:migrate )並在新數據庫上運行遷移。

Please help to clarify, if my understanding has gone wrong. 如果我的理解出錯了,請幫助澄清一下。


#1樓

參考:https://stackoom.com/question/hDxy/rake-db-migrate-db-reset和db-schema-load之間的區別


#2樓

As far as I understand, it is going to drop your database and re-create it based on your db/schema.rb file. 據我所知,它將丟棄您的數據庫並根據您的db/schema.rb文件重新創建它。 That is why you need to make sure that your schema.rb file is always up to date and under version control. 這就是爲什麼您需要確保schema.rb文件始終是最新的並且受版本控制。


#3樓

  • db:migrate runs (single) migrations that have not run yet. db:migrate運行尚未運行的(單個)遷移。
  • db:create creates the database db:create創建數據庫
  • db:drop deletes the database db:drop刪除數據庫
  • db:schema:load creates tables and columns within the (existing) database following schema.rb db:schema:load在schema.rb之後的(現有)數據庫中創建表和列

  • db:setup does db:create, db:schema:load, db:seed db:setup確實db:create,db:schema:load,db:seed

  • db:reset does db:drop, db:setup db:reset執行db:drop,db:setup

Typically, you would use db:migrate after having made changes to the schema via new migration files (this makes sense only if there is already data in the database). 通常,在通過新的遷移文件對架構進行更改後,您將使用db:migrate(僅當數據庫中已存在數據時纔有意義)。 db:schema:load is used when you setup a new instance of your app. db:schema:在設置應用程序的新實例時使用load。

I hope that helps. 我希望有所幫助。


UPDATE for rails 3.2.12: 更新rails 3.2.12:

I just checked the source and the dependencies are like this now: 我剛檢查了源代碼,依賴關係現在是這樣的:

  • db:create creates the database for the current env db:create爲當前環境創建數據庫
  • db:create:all creates the databases for all envs db:create:all爲所有env創建數據庫
  • db:drop drops the database for the current env db:drop刪除當前env的數據庫
  • db:drop:all drops the databases for all envs db:drop:all刪除所有envs的數據庫
  • db:migrate runs migrations for the current env that have not run yet db:migrate爲尚未運行的當前env運行遷移
  • db:migrate:up runs one specific migration db:migrate:up運行一個特定的遷移
  • db:migrate:down rolls back one specific migration db:migrate:down回滾一個特定的遷移
  • db:migrate:status shows current migration status db:migrate:status顯示當前的遷移狀態
  • db:rollback rolls back the last migration db:rollback回滾上次遷移
  • db:forward advances the current schema version to the next one db:forward將當前架構版本推進到下一個版本
  • db:seed (only) runs the db/seed.rb file db:seed (only)運行db / seed.rb文件
  • db:schema:load loads the schema into the current env's database db:schema:load將架構加載到當前env的數據庫中
  • db:schema:dump dumps the current env's schema (and seems to create the db as well) db:schema:dump轉儲當前的env架構(並且似乎也創建了db)

  • db:setup runs db:schema:load, db:seed db:setup運行db:schema:load,db:seed

  • db:reset runs db:drop db:setup db:reset運行db:drop db:setup
  • db:migrate:redo runs (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration db:migrate:redo運行(db:migrate:down db:migrate:up)或(db:rollback db:migrate),具體取決於指定的遷移
  • db:migrate:reset runs db:drop db:create db:migrate db:migrate:reset運行db:drop db:create db:migrate

For further information please have a look at https://github.com/rails/rails/blob/v3.2.12/activerecord/lib/active_record/railties/databases.rake (for Rails 3.2.x) and https://github.com/rails/rails/blob/v4.0.5/activerecord/lib/active_record/railties/databases.rake (for Rails 4.0.x) 有關詳細信息,請查看https://github.com/rails/rails/blob/v3.2.12/activerecord/lib/active_record/railties/databases.rake (對於Rails 3.2.x)和https:// github .com / rails / rails / blob / v4.0.5 / activerecord / lib / active_record / railties / databases.rake (對於Rails 4.0.x)


#4樓

You could simply look in the Active Record Rake tasks as that is where I believe they live as in this file. 您可以簡單地查看Active Record Rake任務,因爲我認爲它們就像在此文件中一樣存在。 https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/lib/active_record/tasks/database_tasks.rb https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/lib/active_record/tasks/database_tasks.rb

What they do is your question right? 他們做的是你的問題嗎?

That depends on where they come from and this is just and example to show that they vary depending upon the task. 這取決於它們來自何處,這只是表明它們根據任務而變化的例子。 Here we have a different file full of tasks. 在這裏,我們有一個完整的任務文件。

https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/Rakefile https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/Rakefile

which has these tasks. 有這些任務。

namespace :db do
  task create: ["db:mysql:build", "db:postgresql:build"]
  task drop: ["db:mysql:drop", "db:postgresql:drop"]
end

This may not answer your question but could give you some insight into go ahead and look the source over especially the rake files and tasks. 這可能無法解答您的問題,但可以讓您深入瞭解並查看源代碼,尤其是rake文件和任務。 As they do a pretty good job of helping you use rails they don't always document the code that well. 由於他們在幫助您使用rails方面做得非常好,因此並不總能很好地記錄代碼。 We could all help there if we know what it is supposed to do. 如果我們知道它應該做什麼,我們都可以幫助那裏。


#5樓

TLDR TLDR

Use 使用

  • rake db:migrate If you wanna make changes to the schema rake db:migrate如果您想對架構進行更改
  • rake db:reset If you wanna drop the database, reload the schema from schema.rb , and reseed the database rake db:reset如果要刪除數據庫,請從schema.rb重新加載模式,然後重新設置數據庫
  • rake db:schema:load If you wanna reset database to schema as provided in schema.rb (This will delete all data) rake db:schema:load如果你想按照schema.rb規定將數據庫重置爲模式(這將刪除所有數據)

Explanations 說明

rake db:schema:load will set up the schema as provided in schema.rb file. rake db:schema:load將設置schema.rb文件中提供的模式。 This is useful for a fresh install of app as it doesn't take as much time as db:migrate 這對於全新安裝app非常有用,因爲它不需要像db:migrate那樣多的時間

Important note, db:schema:load will delete data on server. 重要提示, db:schema:load刪除服務器上的數據。

rake db:migrate makes changes to the existing schema. rake db:migrate對現有模式進行更改。 Its like creating versions of schema. 它就像創建架構版本一樣。 db:migrate will look in db/migrate/ for any ruby files and execute the migrations that aren't run yet starting with the oldest. db:migrate將在db/migrate/查找任何ruby文件,並執行從最舊的文件開始尚未運行的遷移。 Rails knows which file is the oldest by looking at the timestamp at the beginning of the migration filename. 通過查看遷移文件名開頭的時間戳,Rails知道哪個文件是最舊的。 db:migrate comes with a benefit that data can also be put in the database. db:migrate帶來的好處是數據也可以放在數據庫中。 This is actually not a good practice. 這實際上不是一個好習慣。 Its better to use rake db:seed to add data. 最好使用rake db:seed來添加數據。

rake db:migrate provides tasks up , down etc which enables commands like rake db:rollback and makes it the most useful command. rake db:migrate提供了updown等任務,可以啓用rake db:rollback等命令,並使其成爲最有用的命令。

rake db:reset does a db:drop and db:setup rake db:reset執行db:dropdb:setup
It drops the database, create it again, loads the schema, and initializes with the seed data 它刪除數據庫,再次創建它,加載模式,並使用種子數據進行初始化

Relevant part of the commands from databases.rake 來自databases.rake的相關命令部分


namespace :schema do
  desc 'Creates a db/schema.rb file that is portable against any DB supported by Active Record'
  task :dump => [:environment, :load_config] do
    require 'active_record/schema_dumper'
    filename = ENV['SCHEMA'] || File.join(ActiveRecord::Tasks::DatabaseTasks.db_dir, 'schema.rb')
    File.open(filename, "w:utf-8") do |file|
      ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
    end
    db_namespace['schema:dump'].reenable
  end

  desc 'Loads a schema.rb file into the database'
  task :load => [:environment, :load_config, :check_protected_environments] do
    ActiveRecord::Tasks::DatabaseTasks.load_schema_current(:ruby, ENV['SCHEMA'])
  end

  # desc 'Drops and recreates the database from db/schema.rb for the current environment and loads the seeds.'
  task :reset => [ 'db:drop', 'db:setup' ]

namespace :migrate do
  # desc  'Rollbacks the database one migration and re migrate up (options: STEP=x, VERSION=x).'
  task :redo => [:environment, :load_config] do
    if ENV['VERSION']
      db_namespace['migrate:down'].invoke
      db_namespace['migrate:up'].invoke
    else
      db_namespace['rollback'].invoke
      db_namespace['migrate'].invoke
    end
  end
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章