【问题标题】:Rails: Loading schema into secondary databaseRails:将模式加载到辅助数据库中
【发布时间】:2014-01-30 02:18:24
【问题描述】:

如何将架构加载到辅助数据库中? Rails 似乎不支持设置辅助数据库连接同时维护主要ActiveRecord::Base.connection 的能力。

域定义

我们有使用辅助数据库的模型。我们的主数据库是 MySQL,辅助数据库是 PostgreSQL。要使用 ActiveRecord 文档的示例:

|
+-- Book
|    |
|    +-- ScaryBook
|    +-- GoodBook
+-- Author
+-- BankAccount

Book 是抽象的,使用establish_connection 连接到 Postgres。

在处理数据库时,我们可以使用ActiveRecord::Base.connectionBook.connection

架构转储

也就是说:Rails database tasks in the schema namespace 允许我们像这样转储架构:

ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)

这将允许我执行以下操作:

ActiveRecord::SchemaDumper.dump(Book.connection, file)

问题:架构加载

但是,load 任务没有这种能力。它只是对整个架构文件进行评估:

desc 'Load a schema.rb file into the database'
task :load => :environment do
  file = ENV['SCHEMA'] || "#{Rails.root}/db/schema.rb"
  if File.exists?(file)
    load(file)
  else
    abort %{#{file} doesn't exist yet. Run "rake db:migrate" to create it then try again. If you do not intend to use a database, you should instead alter #{Rails.root}/config/application.rb to limit the frameworks that will be loaded}
  end
end

架构文件在没有连接定义的情况下运行ActiveRecord::Schema.define。 (注意the define method 在“当前连接适配器”的上下文中运行)。

如何在不执行临时ActiveRecord::Base.establish_connection 的情况下更改“当前连接适配器”,这不是我想要做的?我基本上想在Book.connection 的上下文中运行ActiveRecord::Schema.define

编辑:我会注意到我需要一个程序化解决方案在运行 rake 任务之外,这就是为什么我在 rake 任务中查看它们的原因'实际上在做。

【问题讨论】:

标签: ruby-on-rails activerecord database-schema


【解决方案1】:

如何转储架构:

ActiveRecord::Base.establish_connection "custom_db_#{Rails.env}".to_sym 
File.open Rails.root.join('db/schema_custom_db.rb'), 'w:utf-8' do |file| 
  ActiveRecord::SchemaDumper.dump ActiveRecord::Base.connection, file
end

如何加载架构:

ActiveRecord::Tasks::DatabaseTasks.load_schema_current :ruby, Rails.root.join('db/schema_custom_db.rb'), "custom_db_#{Rails.env}"

【讨论】:

    【解决方案2】:

    我最近一直在努力尝试仅使用 ActiveRecord。我可以加载迁移和转储模式,但是在加载模式时我遇到了类似的问题,它不是 Rails 或 Rake 的。仅供参考。 我知道您不想单独执行此操作,但我正在尝试。

    请注意看这篇文章: rake db:schema:load vs. migrations

    我仍然痴迷于尝试在没有 Rails 或 Rake 帮助的情况下自行完成,以便更好地了解 ActiveRecord 的工作原理。

    我制作了一个非常小的 Rails 结构来生成迁移。

    文件结构:

    Rakefile
    Gemfile
    /bin
      bundle
      rake
      rails
    /config
      application.rb
      boot.rb
      database.yml
      environment.rb
    /db
      /migrate
      development.sqlite3
      schema.rb
    
      bin/rails generate migration CreateSystemSettings
    

    我在 db/migrate 文件夹中得到一个文件 20150207170924_create_system_settings.rb。

      bin/rake db:migrate
    
    == 20150204070000 DropArticles: migrating 
    == 20150204070000 DropArticles: migrated (0.0001s)
    == 20150207163123 AddPartNumberToProducts: migrating 
    == 20150207163123 AddPartNumberToProducts: migrated (0.0000s) 
    == 20150207163909 ChangeSystemSettings: migrating 
    == 20150207163909 ChangeSystemSettings: migrated (0.0000s) 
    == 20150207170924 CreateSystemSettings: migrating 
    -- create_table(:system_settings)
    -> 0.0085s
    == 20150207170924 CreateSystemSettings: migrated (0.0199s) 
    

    这样就可以了

    更新待定

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-10
      • 1970-01-01
      相关资源
      最近更新 更多