summaryrefslogtreecommitdiff
path: root/app/models/operations/feature_flag_scope.rb
blob: 9068ca0f588664d44c1ce9d608998f4f0b1285df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

# All of the legacy flags have been removed in 14.1, including all of the
# `operations_feature_flag_scopes` rows.  Therefore, this model and the database
# table are unused and should be removed.

module Operations
  class FeatureFlagScope < ApplicationRecord
    prepend HasEnvironmentScope
    include Gitlab::Utils::StrongMemoize

    self.table_name = 'operations_feature_flag_scopes'

    belongs_to :feature_flag

    validates :environment_scope, uniqueness: {
      scope: :feature_flag,
      message: "(%{value}) has already been taken"
    }

    validates :environment_scope,
      if: :default_scope?, on: :update,
      inclusion: { in: %w(*), message: 'cannot be changed from default scope' }

    validates :strategies, feature_flag_strategies: true

    before_destroy :prevent_destroy_default_scope, if: :default_scope?

    scope :ordered, -> { order(:id) }
    scope :enabled, -> { where(active: true) }
    scope :disabled, -> { where(active: false) }

    def self.with_name_and_description
      joins(:feature_flag)
        .select(FeatureFlag.arel_table[:name], FeatureFlag.arel_table[:description])
    end

    def self.for_unleash_client(project, environment)
      select_columns = [
        'DISTINCT ON (operations_feature_flag_scopes.feature_flag_id) operations_feature_flag_scopes.id',
        '(operations_feature_flags.active AND operations_feature_flag_scopes.active) AS active',
        'operations_feature_flag_scopes.strategies',
        'operations_feature_flag_scopes.environment_scope',
        'operations_feature_flag_scopes.created_at',
        'operations_feature_flag_scopes.updated_at'
      ]

      select(select_columns)
        .with_name_and_description
        .where(feature_flag_id: project.operations_feature_flags.select(:id))
        .order(:feature_flag_id)
        .on_environment(environment)
        .reverse_order
    end

    private

    def default_scope?
      environment_scope_was == '*'
    end

    def prevent_destroy_default_scope
      raise ActiveRecord::ReadOnlyRecord, "default scope cannot be destroyed"
    end
  end
end