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 ...@@ -6,7 +6,7 @@ namespace :gitlab do
namespace :db do namespace :db do
desc 'Validates `config/database.yml` to ensure a correct behavior is configured' desc 'Validates `config/database.yml` to ensure a correct behavior is configured'
task validate_config: :environment do 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) # 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` # Once we upgrade to Rails 7.x this should be changed to `include_hidden: true`
...@@ -97,9 +97,7 @@ namespace :gitlab do ...@@ -97,9 +97,7 @@ namespace :gitlab do
end end
ensure ensure
if should_reconnect ActiveRecord::Base.establish_connection(original_db_config) # rubocop: disable Database/EstablishConnection
ActiveRecord::Base.establish_connection(ActiveRecord::Tasks::DatabaseTasks.env.to_sym) # rubocop: disable Database/EstablishConnection
end
end end
Rake::Task['db:migrate'].enhance(['gitlab:db:validate_config']) Rake::Task['db:migrate'].enhance(['gitlab:db:validate_config'])
......
...@@ -46,6 +46,12 @@ RSpec.describe 'gitlab:db:validate_config', :silence_stdout do ...@@ -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 expect { run_rake_task('gitlab:db:validate_config') }.not_to raise_error
end 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 it 'if GITLAB_VALIDATE_DATABASE_CONFIG is set' do
stub_env('GITLAB_VALIDATE_DATABASE_CONFIG', '1') stub_env('GITLAB_VALIDATE_DATABASE_CONFIG', '1')
allow(Gitlab).to receive(:dev_or_test_env?).and_return(false) allow(Gitlab).to receive(:dev_or_test_env?).and_return(false)
...@@ -78,6 +84,12 @@ RSpec.describe 'gitlab:db:validate_config', :silence_stdout do ...@@ -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) expect { run_rake_task('gitlab:db:validate_config') }.to raise_error(match)
end 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 it 'if GITLAB_VALIDATE_DATABASE_CONFIG=1' do
stub_env('GITLAB_VALIDATE_DATABASE_CONFIG', '1') 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