Skip to content

Commit

Permalink
check for already defined constant
Browse files Browse the repository at this point in the history
  • Loading branch information
katjacresanti committed May 13, 2024
1 parent c49bc97 commit f56e0db
Showing 1 changed file with 13 additions and 11 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

0 comments on commit f56e0db

Please sign in to comment.