Commit b6f43fc9 authored by Matt Kasa's avatar Matt Kasa

Fix use of ActiveRecord::Base in CurrentSettings

Relates to https://gitlab.com/gitlab-org/gitlab/-/issues/350651
parent 32a93d55
...@@ -11,7 +11,6 @@ Database/MultipleDatabases: ...@@ -11,7 +11,6 @@ Database/MultipleDatabases:
- ee/spec/services/ee/merge_requests/update_service_spec.rb - ee/spec/services/ee/merge_requests/update_service_spec.rb
- lib/backup/database.rb - lib/backup/database.rb
- lib/backup/manager.rb - lib/backup/manager.rb
- lib/gitlab/current_settings.rb
- lib/gitlab/database/load_balancing/load_balancer.rb - lib/gitlab/database/load_balancing/load_balancer.rb
- lib/gitlab/database/load_balancing.rb - lib/gitlab/database/load_balancing.rb
- lib/gitlab/database/load_balancing/sticking.rb - lib/gitlab/database/load_balancing/sticking.rb
...@@ -29,7 +28,6 @@ Database/MultipleDatabases: ...@@ -29,7 +28,6 @@ Database/MultipleDatabases:
- spec/db/schema_spec.rb - spec/db/schema_spec.rb
- spec/initializers/database_config_spec.rb - spec/initializers/database_config_spec.rb
- spec/lib/backup/manager_spec.rb - spec/lib/backup/manager_spec.rb
- spec/lib/gitlab/current_settings_spec.rb
- spec/lib/gitlab/database_spec.rb - spec/lib/gitlab/database_spec.rb
- spec/lib/gitlab/metrics/subscribers/active_record_spec.rb - spec/lib/gitlab/metrics/subscribers/active_record_spec.rb
- spec/lib/gitlab/profiler_spec.rb - spec/lib/gitlab/profiler_spec.rb
......
...@@ -62,7 +62,7 @@ module Gitlab ...@@ -62,7 +62,7 @@ module Gitlab
# need to be added to the application settings. To prevent Rake tasks # need to be added to the application settings. To prevent Rake tasks
# and other callers from failing, use any loaded settings and return # and other callers from failing, use any loaded settings and return
# defaults for missing columns. # defaults for missing columns.
if Gitlab::Runtime.rake? && ActiveRecord::Base.connection.migration_context.needs_migration? if Gitlab::Runtime.rake? && ::ApplicationSetting.connection.migration_context.needs_migration?
db_attributes = current_settings&.attributes || {} db_attributes = current_settings&.attributes || {}
fake_application_settings(db_attributes) fake_application_settings(db_attributes)
elsif current_settings.present? elsif current_settings.present?
...@@ -82,7 +82,7 @@ module Gitlab ...@@ -82,7 +82,7 @@ module Gitlab
def connect_to_db? def connect_to_db?
# When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised # When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised
active_db_connection = ActiveRecord::Base.connection.active? rescue false active_db_connection = ::ApplicationSetting.connection.active? rescue false
active_db_connection && active_db_connection &&
ApplicationSetting.database.cached_table_exists? ApplicationSetting.database.cached_table_exists?
......
...@@ -118,7 +118,7 @@ RSpec.describe Gitlab::CurrentSettings do ...@@ -118,7 +118,7 @@ RSpec.describe Gitlab::CurrentSettings do
allow(Gitlab::Runtime).to receive(:rake?).and_return(true) allow(Gitlab::Runtime).to receive(:rake?).and_return(true)
# For some reason, `allow(described_class).to receive(:connect_to_db?).and_return(false)` causes issues # For some reason, `allow(described_class).to receive(:connect_to_db?).and_return(false)` causes issues
# during the initialization phase of the test suite, so instead let's mock the internals of it # during the initialization phase of the test suite, so instead let's mock the internals of it
allow(ActiveRecord::Base.connection).to receive(:active?).and_return(false) allow(ApplicationSetting.connection).to receive(:active?).and_return(false)
end end
context 'and no settings in cache' do context 'and no settings in cache' do
...@@ -150,8 +150,8 @@ RSpec.describe Gitlab::CurrentSettings do ...@@ -150,8 +150,8 @@ RSpec.describe Gitlab::CurrentSettings do
it 'fetches the settings from cache' do it 'fetches the settings from cache' do
# For some reason, `allow(described_class).to receive(:connect_to_db?).and_return(true)` causes issues # For some reason, `allow(described_class).to receive(:connect_to_db?).and_return(true)` causes issues
# during the initialization phase of the test suite, so instead let's mock the internals of it # during the initialization phase of the test suite, so instead let's mock the internals of it
expect(ActiveRecord::Base.connection).not_to receive(:active?) expect(ApplicationSetting.connection).not_to receive(:active?)
expect(ActiveRecord::Base.connection).not_to receive(:cached_table_exists?) expect(ApplicationSetting.connection).not_to receive(:cached_table_exists?)
expect_any_instance_of(ActiveRecord::MigrationContext).not_to receive(:needs_migration?) expect_any_instance_of(ActiveRecord::MigrationContext).not_to receive(:needs_migration?)
expect(ActiveRecord::QueryRecorder.new { described_class.current_application_settings }.count).to eq(0) expect(ActiveRecord::QueryRecorder.new { described_class.current_application_settings }.count).to eq(0)
end end
...@@ -159,8 +159,8 @@ RSpec.describe Gitlab::CurrentSettings do ...@@ -159,8 +159,8 @@ RSpec.describe Gitlab::CurrentSettings do
context 'and no settings in cache' do context 'and no settings in cache' do
before do before do
allow(ActiveRecord::Base.connection).to receive(:active?).and_return(true) allow(ApplicationSetting.connection).to receive(:active?).and_return(true)
allow(ActiveRecord::Base.connection).to receive(:cached_table_exists?).with('application_settings').and_return(true) allow(ApplicationSetting.connection).to receive(:cached_table_exists?).with('application_settings').and_return(true)
end end
context 'with RequestStore enabled', :request_store do context 'with RequestStore enabled', :request_store do
...@@ -181,7 +181,7 @@ RSpec.describe Gitlab::CurrentSettings do ...@@ -181,7 +181,7 @@ RSpec.describe Gitlab::CurrentSettings do
context 'when ApplicationSettings does not have a primary key' do context 'when ApplicationSettings does not have a primary key' do
before do before do
allow(ActiveRecord::Base.connection).to receive(:primary_key).with('application_settings').and_return(nil) allow(ApplicationSetting.connection).to receive(:primary_key).with('application_settings').and_return(nil)
end end
it 'raises an exception if ApplicationSettings does not have a primary key' do it 'raises an exception if ApplicationSettings does not have a primary key' do
......
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