Rails 应用程序中的猴子修补数据库适配器



我正在使用PosgreSQL适配器连接到Vertica数据库,该数据库主要与PostgreSQL兼容,但不支持client_min_messages等选项(尽管database.yml中不存在,但仍传递给PGconn.connect)。我已经为ActiveRecord::ConnectionAdapters::PostgreSQLAdapter制作了一个快速而肮脏的猴子补丁,但问题是我猜 AR 中的所有内容都在延迟加载,并且在我的补丁后读取原始文件。

如果我在猴子补丁的顶部添加require 'active_record/connection_adapters/postgresql_adapter',则 ActiveRecord 会尝试建立连接并失败。是否可以更改此行为以使猴子补丁正常工作,或者我应该只编写一个成熟的连接适配器?

您可以将代码挂接到 railties 初始化。包括我的宝石multi_config样品:

module <YourModule>
# Railtie subclass for the gem/plugin
class Railtie < Rails::Railtie
# Railtie initializer method
initializer '<your_plugin>.active_record' do
# When active_record is loaded, only then run this.
ActiveSupport.on_load :active_record do
# Hook your code here. For .e.g. 
ActiveRecord::Base.send(:include, <YourPluginModule>)
end
end
end
end

最新更新