【问题标题】:Need help creating structure.sql for my Rails app需要帮助为我的 Rails 应用程序创建 structure.sql
【发布时间】:2013-02-15 09:34:46
【问题描述】:

我正在尝试手动创建/更新 db/structure.sql 文件以进行测试。我在 application.rb 中设置了“config.active_record.schema_format = :sql”。我想知道是否有一个等效于“ActiveRecord::SchemaDumper.dump”的命令将创建或更新结构.sql。我试过 structure_dump,但输出为零:

> ActiveRecord::Base.connection.structure_dump
=> nil

我也尝试将文件传递给该方法:

> File.open( File.join(Rails.root, 'db/structure.sql')) { |f| ActiveRecord::Base.connection.structure_dump() }
 => nil 

到目前为止,我能够生成或更新 db/structure.sql 的唯一方法是运行 rake db:migrate。有什么选择吗?还是我在尝试运行 ActiveRecord::Base.connection.structure_dump 时遗漏了什么?

【问题讨论】:

    标签: ruby-on-rails activerecord


    【解决方案1】:

    尝试rake db:structure:dump,它将从数据库中重新创建db/structure.sql

    【讨论】:

    • rake db:schema:dump 只是重新生成 schema.rb。我正在尝试获取 structure.sql。
    • 我的错。忘记了您使用的是sql格式。查看更新的答案
    • 感谢您的更新。我实际上想知道是否有一种非 rake 任务方式来执行它,只是因为我将从测试中调用它,并且调用 rake 任务似乎没有必要(尽管我对Rails,所以我可能假设太多)。我在想如果 ActiveRecord::SchemaDumper.dump 是 rake db:schema:dump 的命令等价物,也许有一个类似的单行等价于 rake db:structure:dump...?
    • 你为什么在测试中调用它?
    • 我们有一个迁移规范来验证架构是最新的并且已经提交。我们正在切换到使用 structure.sql 而不是 schema.rb,所以我正在尝试修复此方法:def schema_dump File.open( File.join(Rails.root, 'db/schema.rb'), 'w' ) do |stream| ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) end end
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 2014-05-27
    相关资源
    最近更新 更多