gpt4 book ai didi

ruby-on-rails - db :test:clone, db :test:clone_structure, db :test:load, 和 db :test:prepare? 有什么区别

转载 作者:太空狗 更新时间:2023-10-30 01:37:29 31 4
gpt4 key购买 nike

您必须承认,对于 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

我什至不知道结构和模式之间的区别。加载当前环境的模式和只加载 schema.rb 有什么区别?

这些任务有多相似(或不同)?

最佳答案

很好的问题。让我难住了,所以我潜入了 Rails 源并拉起 database.rake .现在更清楚了:

  • db:test:clone 只是 db:schema:dumpdb: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:loaddb: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文件很简单,可以解决您可能遇到的任何其他问题。该链接转到 :test 命名空间开头的行。

关于ruby-on-rails - db :test:clone, db :test:clone_structure, db :test:load, 和 db :test:prepare? 有什么区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7693365/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com