问题描述
你必须承认,对于rails和数据库的新手,关于rubyonrails.org的官方解释使得这四个任务听起来完全一样。
You'll have to admit, to a newbie to rails and databases, the official explanation on rubyonrails.org makes all four of these tasks sound exactly the same. Quote:
rake db:test:clone Recreate the test database from
the current environment’s database schema
rake db:test:clone_structure Recreate the test database from the
development structure
rake db:test:load Recreate the test database from the current schema.rb
rake db:test:prepare Check for pending migrations and load the test schema
甚至知道结构和模式之间的区别。加载当前环境的模式和加载schema.rb有什么区别?
I don't even know the difference between structure and schema. And what's the difference between loading the current environment's schema and just loading schema.rb?
这些任务有多相似(或不同)?
Just how similar (or different) are these tasks?
推荐答案
很好的问题。如果我陷入困境,所以我进入rails源,并拉起。现在更清楚:
Very good question. Had me stumped so I dove into the rails source and pulled up database.rake
. Now it's more clear:
db:test:clone
只是 db :schema:dump
和 db:test:load
:
task :clone => %w(db:schema:dump db:test:load)
db :test:clone_structure
使用{rails_env} _structure.sql文件:
db:test:clone_structure
uses the {rails_env}_structure.sql file:
task :clone_structure => [ 'db:structure:dump', 'db:test:purge' ] do
# skipped some code, here's what happens for MySQL:
ActiveRecord::Base.establish_connection(:test)
# ...
IO.readlines("#{Rails.root}/db/#{Rails.env}_structure.sql").join.split("\n\n").each do |table|
ActiveRecord::Base.connection.execute(table)
end
end
$ b b
db:test:load
与 db:schema:load
相同,测试数据库:
db:test:load
is the same as db:schema:load
, but invokes it on the test database:
task :load => 'db:test:purge' do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
# ...
db_namespace['schema:load'].invoke
end
db:test:prepare
提示您是否有任何迁移正在等待,如果没有,则运行
(使用schema.rb文件),具体取决于模式格式(这有点混乱,可能有人可以扩展它) / p> db:test:clone_structure
(使用{rails_env} _structure.sql文件) code> db:test:load
db:test:prepare
alerts you if any migrations are pending, and if not, either runs db:test:clone_structure
(using the {rails_env}_structure.sql file) or db:test:load
(using the schema.rb file), depending on the schema format (this is a little confusing to me, maybe someone else can expand on it):
task :prepare => 'db:abort_if_pending_migrations' do
# ...
db_namespace[{ :sql => 'test:clone_structure', :ruby => 'test:load' }[ActiveRecord::Base.schema_format]].invoke
end
希望这个清除!同样,通过文件很容易,并会清除您可能有的任何其他问题。该链接指向的是:test命名空间的开头。
Hope this clears it up! Again, going through the database.rake file is easy and will clear up any other questions you might have. That link goes to the line that is the beginning of the :test namespace.
这篇关于db:test:clone,db:test:clone_structure,db:test:load和db:test:prepare之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!