Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor config to use rails 6.1 APIs #30

Merged
merged 10 commits into from
Jan 9, 2024
94 changes: 39 additions & 55 deletions lib/knockoff/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ def replica_env_keys

def update_replica_configs(new_configs)
if ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').present?
new_configs.symbolize_keys!.merge!(ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').first.configuration_hash)
updated_config = new_configs.deep_dup.merge!(ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas').first.configuration_hash)
end

@replicas_configurations.each do |key, _config|
update_replica_config(key, new_configs)
update_replica_config(key, updated_config)
end
end

Expand All @@ -52,9 +52,10 @@ def properly_configured?

private

def update_replica_config(key, new_configs)
@replicas_configurations[key].merge!(new_configs)
ActiveRecord::Base.configurations[key].merge!(new_configs)
def update_replica_config(key, updated_config)
merged_config = @replicas_configurations[key].configuration_hash.deep_dup.merge!(updated_config)
@replicas_configurations[key] = ActiveRecord::DatabaseConfigurations::HashConfig.new(key, key, merged_config)
ActiveRecord::Base.configurations.configurations << @replicas_configurations[key]
end

def set_replica_configs
Expand All @@ -66,58 +67,11 @@ def parse_knockoff_replica_envs_to_configs
# and don't add the uri to the final list if it can't be parsed
replica_env_keys.map.with_index(0) do |env_key, index|
begin
uri = URI.parse(ENV[env_key])

# Configure parameters such as prepared_statements, pool, reaping_frequency for all replicas.
replica_config = ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas')&.first || {}

adapter =
if uri.scheme == "postgres"
'postgresql'
else
uri.scheme
end

# Base config from the ENV uri. Sqlite is a special case
# and all others follow 'normal' config
uri_config =
if uri.scheme == 'sqlite3'
{
'adapter' => adapter,
'database' => uri.to_s.split(':')[1]
}
else
{
'adapter' => adapter,
'database' => (uri.path || "").split("/")[1],
'username' => uri.user,
'password' => uri.password,
'host' => uri.host,
'port' => uri.port
}
end

# Store the hash in configuration and use it when we establish the connection later.
# TODO: In ActiveRecord >= 6, this is a deprecated way to set a configuration. However
# there appears to be an issue when calling `ActiveRecord::Base.configurations.to_h` in
# version 6.0.4.8 where
# multi-database setup is being ignored / dropped. For example if a database.yml setup
# has something like..
#
# development:
# primary:
# ...
# other:
# ...
#
# then the 'other' database configuration is being dropped.
key = "knockoff_replica_#{index}"
config = replica_config.symbolize_keys.merge(uri_config)
env_name = ActiveRecord::Base.configurations.configurations.first.env_name
new_config = ActiveRecord::DatabaseConfigurations::HashConfig.new(env_name, key, config)
ActiveRecord::Base.configurations.configurations << new_config

@replicas_configurations[key] = config
to_copy = ActiveRecord::Base.configurations.configs_for(env_name: 'knockoff_replicas')&.first&.configuration_hash || {}
register_replica_copy(index, env_key, to_copy)

rescue URI::InvalidURIError
Rails.logger.info "LOG NOTIFIER: Invalid URL specified in follower_env_keys. Not including URI, which may result in no followers used." # URI is purposely not printed to logs
# Return a 'nil' which will be removed from
Expand All @@ -127,5 +81,35 @@ def parse_knockoff_replica_envs_to_configs
end
end.compact
end

def register_replica_copy(index, env_key, configuration_hash)
key = "knockoff_replica_#{index}"
new_config = create_replica_copy(env_key, key, configuration_hash.deep_dup)
ActiveRecord::Base.configurations.configurations << new_config
@replicas_configurations[key] = new_config
end

def create_replica_copy(env_key, key, replica_config_hash)
uri = URI.parse(ENV[env_key])

replica_config_hash[:adapter] =
if uri.scheme == "postgres"
'postgresql'
else
uri.scheme
end

if uri.scheme == 'sqlite3'
replica_config_hash[:database] = uri.to_s.split(':')[1]
else
replica_config_hash[:database] = (uri.path || "").split("/")[1]
replica_config_hash[:username] = uri.user
replica_config_hash[:password] = uri.password
replica_config_hash[:host] = uri.host
replica_config_hash[:port] = uri.port
end

ActiveRecord::DatabaseConfigurations::HashConfig.new(key, key, replica_config_hash)
end
end
end
Loading