Commit 7f00d2cc authored by Thong Kuah's avatar Thong Kuah

The validate_config task should reconnect to the main database

It should not keep the geo as "ActiveRecord::Base" connection
parent 200f0d90
......@@ -6,7 +6,7 @@ namespace :gitlab do
namespace :db do
desc 'Validates `config/database.yml` to ensure a correct behavior is configured'
task validate_config: :environment do
should_reconnect = ActiveRecord::Base.connection_pool.active_connection?
original_db_config = ActiveRecord::Base.connection_db_config
# The include_replicas: is a legacy name to fetch all hidden entries (replica: true or database_tasks: false)
# Once we upgrade to Rails 7.x this should be changed to `include_hidden: true`
......@@ -97,9 +97,7 @@ namespace :gitlab do
end
ensure
if should_reconnect
ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym) # rubocop: disable Database/EstablishConnection
end
ActiveRecord::Base.establish_connection(original_db_config) # rubocop: disable Database/EstablishConnection
end
Rake::Task['db:migrate'].enhance(['gitlab:db:validate_config'])
......
......@@ -46,6 +46,12 @@ RSpec.describe 'gitlab:db:validate_config', :silence_stdout do
expect { run_rake_task('gitlab:db:validate_config') }.not_to raise_error
end
it 'always re-establishes ActiveRecord::Base connection to main config' do
run_rake_task('gitlab:db:validate_config')
expect(ActiveRecord::Base.connection_db_config.configuration_hash).to include(main_database_config) # rubocop: disable Database/MultipleDatabases
end
it 'if GITLAB_VALIDATE_DATABASE_CONFIG is set' do
stub_env('GITLAB_VALIDATE_DATABASE_CONFIG', '1')
allow(Gitlab).to receive(:dev_or_test_env?).and_return(false)
......@@ -78,6 +84,12 @@ RSpec.describe 'gitlab:db:validate_config', :silence_stdout do
expect { run_rake_task('gitlab:db:validate_config') }.to raise_error(match)
end
it 'always re-establishes ActiveRecord::Base connection to main config' do
expect { run_rake_task('gitlab:db:validate_config') }.to raise_error(match)
expect(ActiveRecord::Base.connection_db_config.configuration_hash).to include(main_database_config) # rubocop: disable Database/MultipleDatabases
end
it 'if GITLAB_VALIDATE_DATABASE_CONFIG=1' do
stub_env('GITLAB_VALIDATE_DATABASE_CONFIG', '1')
......
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