Commit 1e536581 authored by Igor Drozdov's avatar Igor Drozdov Committed by Thong Kuah

Use symbols to access db config

DB configs keys are now represented as symbols, not strings
parent 74da7a21
...@@ -19,9 +19,7 @@ module EE ...@@ -19,9 +19,7 @@ module EE
# Disables prepared statements for the current database connection. # Disables prepared statements for the current database connection.
def disable_prepared_statements def disable_prepared_statements
config = ActiveRecord::Base.configurations[Rails.env] config = ::Gitlab::Database.config.merge(prepared_statements: false)
config['prepared_statements'] = false
ActiveRecord::Base.establish_connection(config) ActiveRecord::Base.establish_connection(config)
end end
......
...@@ -36,7 +36,7 @@ module Gitlab ...@@ -36,7 +36,7 @@ module Gitlab
# Returns a Hash containing the load balancing configuration. # Returns a Hash containing the load balancing configuration.
def self.configuration def self.configuration
ActiveRecord::Base.configurations[Rails.env]['load_balancing'] || {} Gitlab::Database.config[:load_balancing] || {}
end end
# Returns the maximum replica lag size in bytes. # Returns the maximum replica lag size in bytes.
...@@ -79,7 +79,7 @@ module Gitlab ...@@ -79,7 +79,7 @@ module Gitlab
end end
def self.pool_size def self.pool_size
ActiveRecord::Base.configurations[Rails.env]['pool'] Gitlab::Database.config[:pool]
end end
# Returns true if load balancing is to be enabled. # Returns true if load balancing is to be enabled.
......
...@@ -34,14 +34,13 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -34,14 +34,13 @@ RSpec.describe Gitlab::Database::LoadBalancing do
describe '.configuration' do describe '.configuration' do
it 'returns a Hash' do it 'returns a Hash' do
config = { 'hosts' => %w(foo) } lb_config = { 'hosts' => %w(foo) }
allow(ActiveRecord::Base.configurations[Rails.env]) original_db_config = Gitlab::Database.config
.to receive(:[]) modified_db_config = original_db_config.merge(load_balancing: lb_config)
.with('load_balancing') expect(Gitlab::Database).to receive(:config).and_return(modified_db_config)
.and_return(config)
expect(described_class.configuration).to eq(config) expect(described_class.configuration).to eq(lb_config)
end end
end end
...@@ -436,7 +435,8 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -436,7 +435,8 @@ RSpec.describe Gitlab::Database::LoadBalancing do
end end
shared_context 'LoadBalancing setup' do shared_context 'LoadBalancing setup' do
let(:hosts) { [ActiveRecord::Base.configurations["development"]['host']] } let(:development_db_config) { ActiveRecord::Base.configurations.default_hash("development").with_indifferent_access }
let(:hosts) { [development_db_config[:host]] }
let(:model) do let(:model) do
Class.new(ApplicationRecord) do Class.new(ApplicationRecord) do
self.table_name = "load_balancing_test" self.table_name = "load_balancing_test"
...@@ -452,10 +452,11 @@ RSpec.describe Gitlab::Database::LoadBalancing do ...@@ -452,10 +452,11 @@ RSpec.describe Gitlab::Database::LoadBalancing do
clear_load_balancing_configuration clear_load_balancing_configuration
allow(ActiveRecord::Base.singleton_class).to receive(:prepend) allow(ActiveRecord::Base.singleton_class).to receive(:prepend)
subject.configure_proxy(::Gitlab::Database::LoadBalancing::ConnectionProxy.new(hosts)) subject.configure_proxy(::Gitlab::Database::LoadBalancing::ConnectionProxy.new(hosts))
allow(ActiveRecord::Base.configurations[Rails.env])
.to receive(:[]) original_db_config = Gitlab::Database.config
.with('load_balancing') modified_db_config = original_db_config.merge(load_balancing: { hosts: hosts })
.and_return('hosts' => hosts) allow(Gitlab::Database).to receive(:config).and_return(modified_db_config)
::Gitlab::Database::LoadBalancing::Session.clear_session ::Gitlab::Database::LoadBalancing::Session.clear_session
end end
end end
......
...@@ -44,7 +44,9 @@ module Gitlab ...@@ -44,7 +44,9 @@ module Gitlab
EXTRA_SCHEMAS = [DYNAMIC_PARTITIONS_SCHEMA, STATIC_PARTITIONS_SCHEMA].freeze EXTRA_SCHEMAS = [DYNAMIC_PARTITIONS_SCHEMA, STATIC_PARTITIONS_SCHEMA].freeze
def self.config def self.config
ActiveRecord::Base.configurations[Rails.env] default_config_hash = ActiveRecord::Base.configurations.find_db_config(Rails.env)&.config || {}
default_config_hash.with_indifferent_access
end end
def self.username def self.username
...@@ -225,9 +227,9 @@ module Gitlab ...@@ -225,9 +227,9 @@ module Gitlab
env = Rails.env env = Rails.env
original_config = ActiveRecord::Base.configurations.to_h original_config = ActiveRecord::Base.configurations.to_h
env_config = original_config[env].merge('pool' => pool_size) env_config = original_config[env].with_indifferent_access.merge(pool: pool_size)
env_config['host'] = host if host env_config[:host] = host if host
env_config['port'] = port if port env_config[:port] = port if port
config = ActiveRecord::DatabaseConfigurations.new( config = ActiveRecord::DatabaseConfigurations.new(
original_config.merge(env => env_config) original_config.merge(env => env_config)
......
...@@ -16,8 +16,8 @@ RSpec.describe Gitlab::Database do ...@@ -16,8 +16,8 @@ RSpec.describe Gitlab::Database do
end end
describe '.config' do describe '.config' do
it 'returns a Hash' do it 'returns a HashWithIndifferentAccess' do
expect(described_class.config).to be_an_instance_of(Hash) expect(described_class.config).to be_an_instance_of(HashWithIndifferentAccess)
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment