Commit d1ac31ec authored by Thong Kuah's avatar Thong Kuah

Merge branch 'define_feature_flags_available_under_class' into 'master'

Move global method to Feature class

See merge request gitlab-org/gitlab!78023
parents f95deb3a 213716a4
...@@ -2,17 +2,8 @@ ...@@ -2,17 +2,8 @@
return unless Gitlab.com? || Gitlab.dev_or_test_env? return unless Gitlab.com? || Gitlab.dev_or_test_env?
def feature_flags_available?
# 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 && Feature::FlipperFeature.table_exists?
rescue ActiveRecord::NoDatabaseError
false
end
Gitlab::Application.configure do Gitlab::Application.configure do
if feature_flags_available? && ::Feature.enabled?(:active_record_transactions_tracking, type: :ops, default_enabled: :yaml) if Feature.feature_flags_available? && ::Feature.enabled?(:active_record_transactions_tracking, type: :ops, default_enabled: :yaml)
Gitlab::Database::Transaction::Observer.register! Gitlab::Database::Transaction::Observer.register!
end end
end end
...@@ -29,6 +29,15 @@ class Feature ...@@ -29,6 +29,15 @@ class Feature
class << self class << self
delegate :group, to: :flipper delegate :group, to: :flipper
def feature_flags_available?
# When the DBMS is not available, an exception (e.g. PG::ConnectionBad) is raised
active_db_connection = ActiveRecord::Base.connection.active? rescue false # rubocop:disable Database/MultipleDatabases
active_db_connection && Feature::FlipperFeature.table_exists?
rescue ActiveRecord::NoDatabaseError
false
end
def all def all
flipper.features.to_a flipper.features.to_a
end end
......
...@@ -11,6 +11,32 @@ RSpec.describe Feature, stub_feature_flags: false do ...@@ -11,6 +11,32 @@ RSpec.describe Feature, stub_feature_flags: false do
skip_feature_flags_yaml_validation skip_feature_flags_yaml_validation
end end
describe '.feature_flags_available?' do
it 'returns false on connection error' do
expect(ActiveRecord::Base.connection).to receive(:active?).and_raise(PG::ConnectionBad) # rubocop:disable Database/MultipleDatabases
expect(described_class.feature_flags_available?).to eq(false)
end
it 'returns false when connection is not active' do
expect(ActiveRecord::Base.connection).to receive(:active?).and_return(false) # rubocop:disable Database/MultipleDatabases
expect(described_class.feature_flags_available?).to eq(false)
end
it 'returns false when the flipper table does not exist' do
expect(Feature::FlipperFeature).to receive(:table_exists?).and_return(false)
expect(described_class.feature_flags_available?).to eq(false)
end
it 'returns false on NoDatabaseError' do
expect(Feature::FlipperFeature).to receive(:table_exists?).and_raise(ActiveRecord::NoDatabaseError)
expect(described_class.feature_flags_available?).to eq(false)
end
end
describe '.get' do describe '.get' do
let(:feature) { double(:feature) } let(:feature) { double(:feature) }
let(:key) { 'my_feature' } let(:key) { 'my_feature' }
......
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