问题描述
您必须承认,要对Rails和Databases的新手进行,RubyonRails.org上的官方解释使所有这四个任务听起来都完全相同. 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?
有什么区别这些任务有多相似(或不同)?
推荐答案
非常好的问题.我陷入困境了,所以我跳进了轨道来源,然后拉起 database.rake .现在更清楚:
-
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文件:
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
-
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
-
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
希望这能清除!同样,通过 database.rake.rake.rake 文件很容易,将清除您可能遇到的任何其他问题.该链接进入了:test名称空间的开始的行.
其他推荐答案
它们实际上不是完全相同的事情.在.../db/schema.rb文件上包含"架构"一词的任何任务中的任何一个.应用所有迁移后,schema.rb实际上是架构的状态.可以执行它以还原模式而不是运行所有数据库迁移(如果您有很多迁移,可能需要很长时间).
任何具有"结构"一词的任务,在{rails.env} _structure.sql文件上作用.当您的架构包含在schema.rb文件中无法表示的构造时,使用此文件.例如,如果您使用特定于特定RDBMS的功能.在封面下,Rails使用适用于您的RDBM的任何模式转储实用程序生产此文件.为了恢复模式,它使用RDBMS特定的工具读取文件并再次执行SQL语句.
rails知道是基于您设置的
是否可以使用schema.rb路线或结构.config.active_record.schema_format =:sql
在您的.../config/application.rb
中问题描述
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
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?
推荐答案
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 is just a combination of db:schema:dump and db:test:load:
task :clone => %w(db:schema:dump db:test:load)
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
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 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
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.
其他推荐答案
They are actually not quite the same thing. Any of those tasks that contain the word 'schema' act on the .../db/schema.rb file. schema.rb is effectively the state of your schema after applying all migrations. It can be executed to restore your schema rather than running all of the db migrations (which can take a long time if you have lots of migrations).
Any of the tasks with the word 'structure', act on the {Rails.env}_structure.sql file. This file is used when your schema contains constructs that can't be expressed in the schema.rb file. For example, if you use features specific to a particular RDBMS. Under the covers, rails produces this file using whatever schema dump utility it appropriate for your RDBMS. To restore the schema, it reads the file in and executes the SQL statements agains using an RDBMS-specific tool.
Rails knows whether to go the schema.rb route or the structure.sql route based on whether or not you've set
config.active_record.schema_format = :sql
in your .../config/application.rb