Commit 44a7743b authored by Bob Van Landuyt's avatar Bob Van Landuyt

Check if the database exists for feature gates

Before calling out to the database to check if a feature is enabled or
not, first check if the database exists.

This allows us to use feature checks during the setup of the
application.
parent cc087b3f
......@@ -39,11 +39,7 @@ module Sha256Attribute
end
def database_exists?
ApplicationRecord.connection
true
rescue
false
Gitlab::Database.exists?
end
end
end
......@@ -39,11 +39,7 @@ module ShaAttribute
end
def database_exists?
ApplicationRecord.connection
true
rescue
false
Gitlab::Database.exists?
end
end
end
......
......@@ -52,6 +52,10 @@ class Feature
# use `default_enabled: true` to default the flag to being `enabled`
# unless set explicitly. The default is `disabled`
def enabled?(key, thing = nil, default_enabled: false)
# During setup the database does not exist yet. So we haven't stored a value
# for the feature yet and return the default.
return default_enabled unless Gitlab::Database.exists?
feature = Feature.get(key)
# If we're not default enabling the flag or the feature has been set, always evaluate.
......
......@@ -241,6 +241,14 @@ module Gitlab
row['version']
end
def self.exists?
connection
true
rescue
false
end
private_class_method :database_version
def self.add_post_migrate_path_to_rails(force: false)
......
......@@ -171,6 +171,13 @@ describe Feature do
end
end
it 'returns the default value when the database does not exist' do
fake_default = double('fake default')
expect(ActiveRecord::Base).to receive(:connection) { raise ActiveRecord::NoDatabaseError, "No database" }
expect(described_class.enabled?(:a_feature, default_enabled: fake_default)).to eq(fake_default)
end
context 'cached feature flag', :request_store do
let(:flag) { :some_feature_flag }
......
......@@ -396,6 +396,20 @@ describe Gitlab::Database do
end
end
describe '.exists?' do
it 'returns true if `ActiveRecord::Base.connection` succeeds' do
expect(ActiveRecord::Base).to receive(:connection)
expect(described_class.exists?).to be(true)
end
it 'returns false if `ActiveRecord::Base.connection` fails' do
expect(ActiveRecord::Base).to receive(:connection) { raise ActiveRecord::NoDatabaseError, 'broken' }
expect(described_class.exists?).to be(false)
end
end
describe '#true_value' do
it 'returns correct value' do
expect(described_class.true_value).to eq "'t'"
......
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