Skip to content

Commit

Permalink
Merge pull request #52 from kickstarter/address-already-initialized-c…
Browse files Browse the repository at this point in the history
…onstant-errors

[INF-1923] add check for already defined constant
  • Loading branch information
katjacresanti committed May 15, 2024
2 parents c49bc97 + 767a60a commit 9673ab9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
24 changes: 13 additions & 11 deletions lib/replica_pools/pools.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,22 +53,24 @@ def config_hash
# generates a unique ActiveRecord::Base subclass for a single replica
def connection_class(pool_name, replica_name, connection_name)
class_name = "#{pool_name.camelize}#{replica_name.camelize}"
connection_config_method_name = ReplicaPools::ConnectionProxy.get_connection_config_method_name
ReplicaPools.const_set(class_name, Class.new(ActiveRecord::Base) do |c|
c.abstract_class = true
c.define_singleton_method(connection_config_method_name) do
if ActiveRecord::VERSION::MAJOR == 6
configurations.configs_for(env_name: connection_name.to_s, include_replicas: true)
elsif ActiveRecord::VERSION::MAJOR == 7
configurations.configs_for(env_name: connection_name.to_s, include_hidden: true)

unless ReplicaPools.const_defined?(class_name)
connection_config_method_name = ReplicaPools::ConnectionProxy.get_connection_config_method_name
ReplicaPools.const_set(class_name, Class.new(ActiveRecord::Base) do |c|
c.abstract_class = true
c.define_singleton_method(connection_config_method_name) do
if ActiveRecord::VERSION::MAJOR == 6
configurations.configs_for(env_name: connection_name.to_s, include_replicas: true)
elsif ActiveRecord::VERSION::MAJOR == 7
configurations.configs_for(env_name: connection_name.to_s, include_hidden: true)
end
end
end
end)
end)
end

ReplicaPools.const_get(class_name).tap do |c|
c.establish_connection(connection_name.to_sym)
end
end

end
end
2 changes: 1 addition & 1 deletion lib/replica_pools/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module ReplicaPools
VERSION = "2.6.6"
VERSION = "2.6.7"
end

0 comments on commit 9673ab9

Please sign in to comment.