本文翻译自: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:migrate
和rake 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. 如果我的理解出错了,请帮助澄清一下。
参考:https://stackoom.com/question/hDxy/rake-db-migrate-db-reset和db-schema-load之间的区别
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
文件始终是最新的并且受版本控制。
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
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: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
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)
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. 如果我们知道它应该做什么,我们都可以帮助那里。
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
规定将数据库重置为模式(这将删除所有数据) 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
提供了up , down等任务,可以启用rake db:rollback
等命令,并使其成为最有用的命令。
rake db:reset
does a db:drop
and db:setup
rake db:reset
执行db:drop
和db:setup
It drops the database, create it again, loads the schema, and initializes with the seed data 它删除数据库,再次创建它,加载模式,并使用种子数据进行初始化
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