Nat*_*pec 72 database unit-testing ruby-on-rails
你不得不承认,对于rails和数据库的新手,rubyonrails.org上的官方解释使得所有这四个任务听起来完全一样.引用:
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
Run Code Online (Sandbox Code Playgroud)
我甚至不知道结构和架构之间的区别.加载当前环境的架构和加载schema.rb之间有什么区别?
这些任务有多相似(或不同)?
bri*_*ker 67
非常好的问题.让我难过,所以我潜入铁轨源并拉起来database.rake.现在更清楚了:
db:test:clone仅仅是一个的组合db:schema:dump和db:test:load:
task :clone => %w(db:schema:dump db:test:load)
Run Code Online (Sandbox Code Playgroud)db:test:clone_structure使用{rails_env}_structure.sql文件:
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
Run Code Online (Sandbox Code Playgroud)db:test:load是相同的db:schema:load,但在测试数据库上调用它:
task :load => 'db:test:purge' do
ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations['test'])
# ...
db_namespace['schema:load'].invoke
end
Run Code Online (Sandbox Code Playgroud)db:test:prepare如果有任何迁移待处理,则提醒您,如果没有,则运行db:test:clone_structure(使用该{rails_env}_structure.sql文件)或db:test:load(使用该schema.rb文件),具体取决于架构格式(这对我来说有点混乱,也许其他人可以扩展它):
task :prepare => 'db:abort_if_pending_migrations' do
# ...
db_namespace[{ :sql => 'test:clone_structure', :ruby => 'test:load' }[ActiveRecord::Base.schema_format]].invoke
end
Run Code Online (Sandbox Code Playgroud)希望这清除它!再次,通过database.rake文件很容易,并将清除您可能有的任何其他问题.该链接转到:test命名空间的开头.
小智 21
它们实际上并不完全相同.任何包含"schema"一词的任务都会作用于.../db/schema.rb文件.schema.rb实际上是应用所有迁移后的架构状态.可以执行它来恢复您的模式,而不是运行所有数据库迁移(如果您有大量迁移,这可能需要很长时间).
任何带有"结构"一词的任务都会作用于{Rails.env} _structure.sql文件.当架构包含无法在schema.rb文件中表示的构造时,将使用此文件.例如,如果您使用特定于特定RDBMS的功能.在封面下,rails使用适合您的RDBMS的任何模式转储实用程序生成此文件.要恢复模式,它将读取文件并使用特定于RDBMS的工具再次执行SQL语句.
Rails根据你是否设置了解是否要去schema.rb路由或structure.sql路由
config.active_record.schema_format =:sql
在你的.../config/application.rb中
| 归档时间: |
|
| 查看次数: |
18082 次 |
| 最近记录: |