File: active_record.rb

package info (click to toggle)
ruby-flipper 0.26.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,288 kB
  • sloc: ruby: 16,377; sh: 61; javascript: 24; makefile: 14
file content (267 lines) | stat: -rw-r--r-- 8,544 bytes parent folder | download
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
require 'set'
require 'flipper'
require 'active_record'

module Flipper
  module Adapters
    class ActiveRecord
      include ::Flipper::Adapter

      # Abstract base class for internal models
      class Model < ::ActiveRecord::Base
        self.abstract_class = true
      end

      # Private: Do not use outside of this adapter.
      class Feature < Model
        self.table_name = [
          Model.table_name_prefix,
          "flipper_features",
          Model.table_name_suffix,
        ].join
      end

      # Private: Do not use outside of this adapter.
      class Gate < Model
        self.table_name = [
          Model.table_name_prefix,
          "flipper_gates",
          Model.table_name_suffix,
        ].join
      end

      # Public: The name of the adapter.
      attr_reader :name

      # Public: Initialize a new ActiveRecord adapter instance.
      #
      # name - The Symbol name for this adapter. Optional (default :active_record)
      # feature_class - The AR class responsible for the features table.
      # gate_class - The AR class responsible for the gates table.
      #
      # Allowing the overriding of name is so you can differentiate multiple
      # instances of this adapter from each other, if, for some reason, that is
      # a thing you do.
      #
      # Allowing the overriding of the default feature/gate classes means you
      # can roll your own tables and what not, if you so desire.
      def initialize(options = {})
        @name = options.fetch(:name, :active_record)
        @feature_class = options.fetch(:feature_class) { Feature }
        @gate_class = options.fetch(:gate_class) { Gate }
      end

      # Public: The set of known features.
      def features
        with_connection(@feature_class) { @feature_class.all.map(&:key).to_set }
      end

      # Public: Adds a feature to the set of known features.
      def add(feature)
        with_connection(@feature_class) do
          # race condition, but add is only used by enable/disable which happen
          # super rarely, so it shouldn't matter in practice
          @feature_class.transaction do
            unless @feature_class.where(key: feature.key).first
              begin
                @feature_class.create! { |f| f.key = feature.key }
              rescue ::ActiveRecord::RecordNotUnique
              end
            end
          end
        end

        true
      end

      # Public: Removes a feature from the set of known features.
      def remove(feature)
        with_connection(@feature_class) do
          @feature_class.transaction do
            @feature_class.where(key: feature.key).destroy_all
            clear(feature)
          end
        end
        true
      end

      # Public: Clears the gate values for a feature.
      def clear(feature)
        with_connection(@gate_class) { @gate_class.where(feature_key: feature.key).destroy_all }
        true
      end

      # Public: Gets the values for all gates for a given feature.
      #
      # Returns a Hash of Flipper::Gate#key => value.
      def get(feature)
        gates = with_connection(@gate_class) { @gate_class.where(feature_key: feature.key).pluck(:key, :value) }
        result_for_gates(feature, gates)
      end

      def get_multi(features)
        with_connection(@gate_class) do
          gates = @gate_class.where(feature_key: features.map(&:key)).pluck(:feature_key, :key, :value)
          grouped_gates = gates.inject({}) do |hash, (feature_key, key, value)|
            hash[feature_key] ||= []
            hash[feature_key] << [key, value]
            hash
          end

          result = {}
          features.each do |feature|
            result[feature.key] = result_for_gates(feature, grouped_gates[feature.key])
          end
          result
        end
      end

      def get_all
        with_connection(@feature_class) do
          # query the gates from the db in a single query
          features = ::Arel::Table.new(@feature_class.table_name.to_sym)
          gates = ::Arel::Table.new(@gate_class.table_name.to_sym)
          rows_query = features.join(gates, ::Arel::Nodes::OuterJoin)
            .on(features[:key].eq(gates[:feature_key]))
            .project(features[:key].as('feature_key'), gates[:key], gates[:value])
          gates = @feature_class.connection.select_rows(rows_query)

          # group the gates by feature key
          grouped_gates = gates.inject({}) do |hash, (feature_key, key, value)|
            hash[feature_key] ||= []
            hash[feature_key] << [key, value]
            hash
          end

          # build up the result hash
          result = Hash.new { |hash, key| hash[key] = default_config }
          features = grouped_gates.keys.map { |key| Flipper::Feature.new(key, self) }
          features.each do |feature|
            result[feature.key] = result_for_gates(feature, grouped_gates[feature.key])
          end
          result
        end
      end

      # Public: Enables a gate for a given thing.
      #
      # feature - The Flipper::Feature for the gate.
      # gate - The Flipper::Gate to disable.
      # thing - The Flipper::Type being enabled for the gate.
      #
      # Returns true.
      def enable(feature, gate, thing)
        case gate.data_type
        when :boolean
          set(feature, gate, thing, clear: true)
        when :integer
          set(feature, gate, thing)
        when :set
          enable_multi(feature, gate, thing)
        else
          unsupported_data_type gate.data_type
        end

        true
      end

      # Public: Disables a gate for a given thing.
      #
      # feature - The Flipper::Feature for the gate.
      # gate - The Flipper::Gate to disable.
      # thing - The Flipper::Type being disabled for the gate.
      #
      # Returns true.
      def disable(feature, gate, thing)
        case gate.data_type
        when :boolean
          clear(feature)
        when :integer
          set(feature, gate, thing)
        when :set
          with_connection(@gate_class) do
            @gate_class.where(feature_key: feature.key, key: gate.key, value: thing.value).destroy_all
          end
        else
          unsupported_data_type gate.data_type
        end

        true
      end

      # Private
      def unsupported_data_type(data_type)
        raise "#{data_type} is not supported by this adapter"
      end

      private

      def set(feature, gate, thing, options = {})
        clear_feature = options.fetch(:clear, false)
        with_connection(@gate_class) do
          @gate_class.transaction do
            clear(feature) if clear_feature
            @gate_class.where(feature_key: feature.key, key: gate.key).destroy_all
            begin
              @gate_class.create! do |g|
                g.feature_key = feature.key
                g.key = gate.key
                g.value = thing.value.to_s
              end
            rescue ::ActiveRecord::RecordNotUnique
              # assume this happened concurrently with the same thing and its fine
              # see https://github.com/jnunemaker/flipper/issues/544
            end
          end
        end

        nil
      end

      def enable_multi(feature, gate, thing)
        with_connection(@gate_class) do
          @gate_class.create! do |g|
            g.feature_key = feature.key
            g.key = gate.key
            g.value = thing.value.to_s
          end
        end

        nil
      rescue ::ActiveRecord::RecordNotUnique
        # already added so no need move on with life
      end

      def result_for_gates(feature, gates)
        result = {}
        gates ||= []
        feature.gates.each do |gate|
          result[gate.key] =
            case gate.data_type
            when :boolean
              if row = gates.detect { |key, value| !key.nil? && key.to_sym == gate.key }
                row.last
              end
            when :integer
              if row = gates.detect { |key, value| !key.nil? && key.to_sym == gate.key }
                row.last
              end
            when :set
              gates.select { |key, value| !key.nil? && key.to_sym == gate.key }.map(&:last).to_set
            else
              unsupported_data_type gate.data_type
            end
        end
        result
      end

      def with_connection(model = @feature_class, &block)
        model.connection_pool.with_connection(&block)
      end
    end
  end
end

Flipper.configure do |config|
  config.adapter { Flipper::Adapters::ActiveRecord.new }
end