Ruby-on-rails rake db:migrate db:reset 和 db:schema:load 的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10301794/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 03:18:25  来源:igfitidea点击:

Difference between rake db:migrate db:reset and db:schema:load

ruby-on-railsrubydatabaserake

提问by Gaurav Agarwal

The difference between rake db:migrateand rake db:resetis pretty clear in my head. The thing which I don't understand is how rake db:schema:loaddifferent from the former two.

rake db:migrate和之间的区别rake db:reset在我的脑海中非常清楚。我不明白的是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: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:migrate- 运行尚未运行的迁移。
  • rake db:reset- 清除数据库(大概是执行rake db:drop+ rake db:create+ rake db:migrate)并在新数据库上运行迁移。

Please help to clarify, if my understanding has gone wrong.

如果我的理解有误,请帮助澄清。

回答by moritz

  • db:migrateruns (single) migrations that have not run yet.
  • db:createcreates the database
  • db:dropdeletes the database
  • db:schema:loadcreates tables and columns within the (existing) database following schema.rb

  • db:setupdoes db:create, db:schema:load, db:seed

  • db:resetdoes db:drop, db:setup
  • db:migrate:resetdoes db:drop, db:create, db:migrate
  • db:migrate运行(单个)尚未运行的迁移。
  • db:create创建数据库
  • db:drop删除数据库
  • db:schema:load在 schema.rb 之后的(现有)数据库中创建表和列

  • db:setup执行 db:create、db:schema:load、db:seed

  • db:reset执行 db:drop, db:setup
  • db:migrate:reset执行 db:drop、db:create、db:migrate

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:schema:load is used when you setup a new instance of your app.

通常,您会在通过新的迁移文件对架构进行更改后使用 db:migrate(这仅在数据库中已有数据时才有意义)。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:createcreates the database for the current env
  • db:create:allcreates the databases for all envs
  • db:dropdrops the database for the current env
  • db:drop:alldrops the databases for all envs
  • db:migrateruns migrations for the current env that have not run yet
  • db:migrate:upruns one specific migration
  • db:migrate:downrolls back one specific migration
  • db:migrate:statusshows current migration status
  • db:rollbackrolls back the last migration
  • db:forwardadvances the current schema version to the next one
  • db:seed(only) runs the db/seed.rb file
  • db:schema:loadloads the schema into the current env's database
  • db:schema:dumpdumps the current env's schema (and seems to create the db as well)

  • db:setupruns db:schema:load, db:seed

  • db:resetruns db:drop db:setup
  • db:migrate:redoruns (db:migrate:down db:migrate:up) or (db:rollback db:migrate) depending on the specified migration
  • db:migrate:resetruns db:drop db:create db:migrate
  • db:create为当前环境创建数据库
  • db:create:all为所有环境创建数据库
  • db:drop删除当前环境的数据库
  • db:drop:all删除所有环境的数据库
  • db:migrate为尚未运行的当前环境运行迁移
  • db:migrate:up运行一个特定的迁移
  • db:migrate:down回滚一个特定的迁移
  • db:migrate:status显示当前迁移状态
  • db:rollback 回滚上次迁移
  • db:forward将当前模式版本推进到下一个
  • db:seed(仅)运行 db/seed.rb 文件
  • db:schema:load将模式加载到当前环境的数据库中
  • db:schema:dump转储当前环境的模式(并且似乎也创建了数据库)

  • db:setup运行 db:schema:load, db:seed

  • db:reset运行 db:drop db:setup
  • db:migrate:redo运行 (db:migrate:down db:migrate:up) 或 (db:rollback 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)

回答by sudo bangbang

TLDR

TLDR

Use

  • rake db:migrateIf you wanna make changes to the schema
  • rake db:resetIf you wanna drop the database, reload the schema from schema.rb, and reseed the database
  • rake db:schema:loadIf you wanna reset database to schema as provided in schema.rb(This will delete all data)
  • rake db:migrate如果您想更改架构
  • rake db:reset如果您想删除数据库,请从 重新加载架构schema.rb,然后重新设定数据库的种子
  • rake db:schema:load如果您想按照schema.rb(这将删除所有数据)中提供的方式将数据库重置为架构

Explanations

说明

rake db:schema:loadwill set up the schema as provided in schema.rbfile. This is useful for a fresh install of app as it doesn't take as much time as db:migrate

rake db:schema:load将设置schema.rb文件中提供的架构。这对于全新安装应用程序很有用,因为它不会花费太多时间db:migrate

Important note, db:schema:loadwill deletedata on server.

重要提示,db:schema:load删除服务器上的数据。

rake db:migratemakes changes to the existing schema. Its like creating versions of schema. db:migratewill look in db/migrate/for any ruby files and execute the migrations that aren't run yet starting with the oldest. Rails knows which file is the oldest by looking at the timestamp at the beginning of the migration filename. db:migratecomes with a benefit that data can also be put in the database. This is actually not a good practice. Its better to use rake db:seedto add data.

rake db:migrate对现有架构进行更改。这就像创建模式的版本。db:migrate将查找db/migrate/任何 ruby​​ 文件并从最旧的开始执行尚未运行的迁移。Rails 通过查看迁移文件名开头的时间戳来知道哪个文件是最旧的。db:migrate有一个好处是数据也可以放在数据库中。这实际上不是一个好的做法。最好用来rake db:seed添加数据。

rake db:migrateprovides tasks up, downetc which enables commands like rake db:rollbackand makes it the most useful command.

rake db:migrate提供任务updown等,这些任务可以启用诸如此类的命令rake db:rollback并使其成为最有用的命令。

rake db:resetdoes a db:dropand db:setup
It drops the database, create it again, loads the schema, and initializes with the seed data

rake db:reset执行 adb:dropdb:setup
删除数据库,再次创建它,加载模式,并使用种子数据进行初始化

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

回答by Simon Bagreev

As far as I understand, it is going to drop your database and re-create it based on your db/schema.rbfile. That is why you need to make sure that your schema.rbfile is always up to date and under version control.

据我了解,它将删除您的数据库并根据您的db/schema.rb文件重新创建它。这就是为什么您需要确保您的schema.rb文件始终是最新的并处于版本控制之下。

回答by Douglas G. Allen

You could simply look in the Active Record Rake tasks as that is where I believe they live as in this file. https://github.com/rails/rails/blob/fe1f4b2ad56f010a4e9b93d547d63a15953d9dc2/activerecord/lib/active_record/tasks/database_tasks.rb

您可以简单地查看 Active Record Rake 任务,因为我认为它们在此文件中存在。 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. As they do a pretty good job of helping you use rails they don't always document the code that well. We could all help there if we know what it is supposed to do.

这可能无法回答您的问题,但可以让您深入了解继续查看源代码,尤其是 rake 文件和任务。因为他们在帮助您使用 Rails 方面做得非常好,所以他们并不总是很好地记录代码。如果我们知道它应该做什么,我们都可以在那里提供帮助。

回答by Ваня Скоцик

UPDATED for Rails 5:

Rails 5 更新:

db:create- Creates the database for the current RAILS_ENVenvironment. If RAILS_ENVis not specified it defaults to the development and test databases.

db:create- 为当前RAILS_ENV环境创建数据库。如果未指定RAILS_ENV,则默认为开发和测试数据库。

db:create:all- Creates the database for all environments.

db:create:all- 为所有环境创建数据库。

db:drop- Drops the database for the current RAILS_ENVenvironment. If RAILS_ENVis not specified it defaults to the development and test databases.

db:drop- 删除当前RAILS_ENV环境的数据库。如果未指定RAILS_ENV,则默认为开发和测试数据库。

db:drop:all- Drops the database for all environments.

db:drop:all- 删除所有环境的数据库。

db:migrate- Runs migrations for the current environment that have not run yet. By default it will run migrations only in the development environment.

db:migrate- 为尚未运行的当前环境运行迁移。默认情况下,它只会在开发环境中运行迁移。

db:migrate:redo- Runs db:migrate:downand db:migrate:upor db:migrate:rollbackand db:migrate:updepending on the specified migration.

db:migrate:redo-根据指定的迁移运行db:migrate:downdb:migrate:updb:migrate:rollbackdb:migrate:up

db:migrate:up- Runs the up for the given migration VERSION.

db:migrate:up- 运行给定的迁移版本。

db:migrate:down- Runs the down for the given migration VERSION.

db:migrate:down- 运行给定的迁移版本。

db:migrate:status- Displays the current migration status.

db:migrate:status- 显示当前的迁移状态。

db:migrate:rollback- Rolls back the last migration.

db:migrate:rollback- 回滚上次迁移。

db:version- Prints the current schema version.

db:version- 打印当前模式版本。

db:forward- Pushes the schema to the next version.

db:forward- 将架构推送到下一个版本。

db:seed- Runs the db/seeds.rbfile.

db:seed- 运行db/seeds.rb文件。

db:schema:loadRecreates the database from the schema.rbfile.

db:schema:loadschema.rb文件重新创建数据库。

db:schema:dumpDumps the current environment's schema to db/schema.rb.

db:schema:dump将当前环境的模式转储到db/schema.rb

db:structure:load- Recreates the database from the structure.sqlfile.

db:structure:load- 从structure.sql文件重新创建数据库。

db:structure:dump- Dumps the current environment's schema to db/structure.sql. (You can specify another file with SCHEMA=db/my_structure.sql)

db:structure:dump- 将当前环境的模式转储到db/structure.sql。(您可以使用 指定另一个文件SCHEMA=db/my_structure.sql

db:setupRuns db:create, db:schema:loadand db:seed.

db:setup运行db:createdb:schema:loaddb:seed

db:resetRuns db:dropand db:setup. db:migrate:reset- Runs db:drop, db:createand db:migrate.

db:reset运行db:dropdb:setupdb:migrate:reset- 运行db:dropdb:createdb:migrate

db:test:prepare- Check for pending migrations and load the test schema. (If you run rake without any arguments it will do this by default.)

db:test:prepare- 检查挂起的迁移并加载测试模式。(如果你在没有任何参数的情况下运行 rake,它会默认执行此操作。)

db:test:clone- Recreate the test database from the current environment's database schema.

db:test:clone- 从当前环境的数据库模式重新创建测试数据库。

db:test:clone_structure- Similar to db:test:clone, but it will ensure that your test database has the same structure, including charsets and collations, as your current environment's database.

db:test:clone_structure- 类似于db:test:clone,但它将确保您的测试数据库与您当前环境的数据库具有相同的结构,包括字符集和排序规则。

db:environment:set- Set the current RAILS_ENVenvironment in the ar_internal_metadatatable. (Used as part of the protected environment check.)

db:environment:set-在ar_internal_metadata表中设置当前的RAILS_ENV环境。(用作受保护环境检查的一部分。)

db:check_protected_environments- Checks if a destructive action can be performed in the current RAILS_ENVenvironment. Used internally when running a destructive action such as db:dropor db:schema:load.

db:check_protected_environments- 检查是否可以在当前RAILS_ENV环境中执行破坏性操作。在运行破坏性操作(例如db:dropdb:schema:load )时在内部使用。