简体   繁体   English

Rails 5.2 多数据库连接问题

[英]Rails 5.2 multiple database connections issue

We have found a few patterns for creating a Rails app with multiple database connections (For example: https://www.thegreatcodeadventure.com/managing-multiple-databases-in-a-single-rails-application/我们发现了一些用于创建具有多个数据库连接的 Rails 应用程序的模式(例如: https://www.thegreatcodeadventure.com/managing-multiple-databases-in-a-single-rails-application/

The pattern we use is as follows (Actual example):我们使用的模式如下(实际示例):

# models/icm_db_base.rb
class IcmDbBase < ActiveRecord::Base
  self.abstract_class = true
  establish_connection :icm_production
end

Then, for a model that I want to implement this connection, I use:然后,对于我要实现此连接的 model,我使用:

# models/icm_task.rb
class IcmTask < IcmDbBase
... model code here
end

While this does all load up, we get the following oddity when I try to run a test inside rails console (This behavior is replicable on dev and prod):虽然这确实加载了所有内容,但当我尝试在 Rails 控制台中运行测试时,我们会遇到以下奇怪情况(此行为可在开发和生产环境中复制):

Loading development environment (Rails 5.2.3)
2.6.4 :001 > IcmTask.connection_config
=> DATABASE CONNECTION CONFIG FOR ActiveRecord::Base (DB #1)

2.6.4 :002 > IcmTask.superclass
 => IcmDbBase(abstract) 

2.6.4 :003 > IcmDbBase.connection_config
 => DATABASE CONNECTION CONFIG FOR IcmDbBase (DB #2)

It becomes clear that this output (While not expected) is actually what has happened, and IcmTask is inheriting its connection from ActiveRecord::Base and NOT inheriting from IcmDbBase as desired/expected.很明显,这个 output(虽然不是预期的)实际上已经发生了,并且 IcmTask 从 ActiveRecord::Base 继承了它的连接,而不是像期望/预期的那样从 IcmDbBase 继承。

We are running Rails 5.2.3 and Ruby 2.6.4.我们正在运行 Rails 5.2.3 和 Ruby 2.6.4。

Even more strange is that if I put a log into our web server (puma 4.3.0) the connection config for IcmTask DOES match IcmDbBase as expected and everything works as desired.更奇怪的是,如果我将日志放入我们的 web 服务器(puma 4.3.0),IcmTask 的连接配置确实与预期的 IcmDbBase 匹配,并且一切都按预期工作。

Anything obvious here?这里有什么明显的吗? We cannot get this to inherit the connection as expected for this model no matter what we've tried while inside rails console.无论我们在 Rails 控制台内尝试了什么,我们都无法让它继承 model 的预期连接。

原来 innkeeper gem 开箱即用地覆盖了此连接继承行为(只需安装它),因此当我删除 gem 时,它的行为符合预期。

Try尝试

IcmTask.connection_db_config

instead of代替

IcmTask.connection_config

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM