flipper-active_record 1.4.2 → 1.5.0.rc2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/flipper/adapters/active_record.rb +88 -16
- data/lib/flipper/version.rb +1 -1
- data/lib/generators/flipper/active_record_generator.rb +4 -1
- data/lib/generators/flipper/templates/migration.erb +13 -7
- metadata +5 -17
- data/benchmark/active_record_adapter_ips.rb +0 -18
- data/benchmark/active_record_ips.rb +0 -17
- data/benchmark/active_record_setup.rb +0 -31
- data/examples/active_record/ar_setup.rb +0 -45
- data/examples/active_record/basic.rb +0 -20
- data/examples/active_record/cached.rb +0 -23
- data/examples/active_record/group_migration.rb +0 -68
- data/examples/active_record/internals.rb +0 -107
- data/flipper-active_record.gemspec +0 -36
- data/spec/flipper/adapters/active_record_spec.rb +0 -381
- data/test/adapters/active_record_test.rb +0 -79
- data/test_rails/generators/flipper/active_record_generator_test.rb +0 -41
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dfd80b6d928281b7f12e3763b8a10e72af60598d93971d4f86fb71c193d87f39
|
|
4
|
+
data.tar.gz: 3e31135bfc174634cb496aaa0998e61472e5d2052d62001adb4bdcef52d27b82
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: b71075de60991478bc8a58f674dac791c0af2ef72474f5e41dd5f8915a92b8213b765597527e0cf56d9130112d0d1b59def5e0d710fd37fee3a2b717c59b8532
|
|
7
|
+
data.tar.gz: c2cbec4820b4136fcbf557c86c9807404a17f93b65d0f7a7c174f5aff4f23d28f031ee79c92b0fab9fd6b5d7e0e75aee10da5dcced43081485f6a071c2ec37ca
|
|
@@ -2,9 +2,12 @@ require 'set'
|
|
|
2
2
|
require 'securerandom'
|
|
3
3
|
require 'flipper'
|
|
4
4
|
require 'active_record'
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
require_relative 'active_record/
|
|
5
|
+
|
|
6
|
+
ActiveSupport.on_load(:active_record) do
|
|
7
|
+
require_relative 'active_record/model'
|
|
8
|
+
require_relative 'active_record/feature'
|
|
9
|
+
require_relative 'active_record/gate'
|
|
10
|
+
end
|
|
8
11
|
|
|
9
12
|
module Flipper
|
|
10
13
|
module Adapters
|
|
@@ -16,11 +19,17 @@ module Flipper
|
|
|
16
19
|
Run `rails generate flipper:update` and `rails db:migrate`.
|
|
17
20
|
EOS
|
|
18
21
|
|
|
22
|
+
# Private: The sync version lives in the gates table under the reserved
|
|
23
|
+
# feature key SyncVersion::KEY, which has no row in the features table, so
|
|
24
|
+
# features and get_all never see it.
|
|
25
|
+
SYNC_VERSION_GATE_KEY = "sync_version".freeze
|
|
26
|
+
|
|
19
27
|
# Public: Initialize a new ActiveRecord adapter instance.
|
|
20
28
|
#
|
|
21
29
|
# name - The Symbol name for this adapter. Optional (default :active_record)
|
|
22
30
|
# feature_class - The AR class responsible for the features table.
|
|
23
31
|
# gate_class - The AR class responsible for the gates table.
|
|
32
|
+
# table_prefix - The prefix for the default features and gates tables.
|
|
24
33
|
#
|
|
25
34
|
# Allowing the overriding of name is so you can differentiate multiple
|
|
26
35
|
# instances of this adapter from each other, if, for some reason, that is
|
|
@@ -29,9 +38,19 @@ module Flipper
|
|
|
29
38
|
# Allowing the overriding of the default feature/gate classes means you
|
|
30
39
|
# can roll your own tables and what not, if you so desire.
|
|
31
40
|
def initialize(options = {})
|
|
41
|
+
# The default models are defined when ActiveRecord::Base loads (see
|
|
42
|
+
# on_load above). Load it now in case nothing else has yet, like in
|
|
43
|
+
# the flipper CLI or a rake task.
|
|
44
|
+
require "active_record/base"
|
|
45
|
+
|
|
32
46
|
@name = options.fetch(:name, :active_record)
|
|
33
|
-
|
|
34
|
-
@
|
|
47
|
+
table_prefix = options[:table_prefix]
|
|
48
|
+
@feature_class = options.fetch(:feature_class) do
|
|
49
|
+
model_class(Flipper::Adapters::ActiveRecord::Feature, table_prefix, "flipper_features")
|
|
50
|
+
end
|
|
51
|
+
@gate_class = options.fetch(:gate_class) do
|
|
52
|
+
model_class(Flipper::Adapters::ActiveRecord::Gate, table_prefix, "flipper_gates")
|
|
53
|
+
end
|
|
35
54
|
end
|
|
36
55
|
|
|
37
56
|
# Public: The set of known features.
|
|
@@ -41,6 +60,7 @@ module Flipper
|
|
|
41
60
|
|
|
42
61
|
# Public: Adds a feature to the set of known features.
|
|
43
62
|
def add(feature)
|
|
63
|
+
ensure_not_reserved_key!(feature)
|
|
44
64
|
with_write_connection(@feature_class) do
|
|
45
65
|
@feature_class.transaction(requires_new: true) do
|
|
46
66
|
begin
|
|
@@ -63,7 +83,8 @@ module Flipper
|
|
|
63
83
|
with_write_connection(@feature_class) do
|
|
64
84
|
@feature_class.transaction do
|
|
65
85
|
@feature_class.where(key: feature.key).destroy_all
|
|
66
|
-
|
|
86
|
+
# Removing the reserved name must not delete the sync version.
|
|
87
|
+
clear(feature) unless Flipper.reserved_key?(feature.key)
|
|
67
88
|
end
|
|
68
89
|
end
|
|
69
90
|
true
|
|
@@ -71,6 +92,7 @@ module Flipper
|
|
|
71
92
|
|
|
72
93
|
# Public: Clears the gate values for a feature.
|
|
73
94
|
def clear(feature)
|
|
95
|
+
ensure_not_reserved_key!(feature)
|
|
74
96
|
with_write_connection(@gate_class) { @gate_class.where(feature_key: feature.key).destroy_all }
|
|
75
97
|
true
|
|
76
98
|
end
|
|
@@ -103,8 +125,8 @@ module Flipper
|
|
|
103
125
|
def get_all(**kwargs)
|
|
104
126
|
with_connection(@feature_class) do |connection|
|
|
105
127
|
# query the gates from the db in a single query
|
|
106
|
-
features =
|
|
107
|
-
gates =
|
|
128
|
+
features = @feature_class.arel_table
|
|
129
|
+
gates = @gate_class.arel_table
|
|
108
130
|
rows_query = features.join(gates, ::Arel::Nodes::OuterJoin)
|
|
109
131
|
.on(features[:key].eq(gates[:feature_key]))
|
|
110
132
|
.project(features[:key].as('feature_key'), gates[:key], gates[:value])
|
|
@@ -128,6 +150,33 @@ module Flipper
|
|
|
128
150
|
end
|
|
129
151
|
end
|
|
130
152
|
|
|
153
|
+
# Public: The Flipper::SyncVersion of the flags last synced into this
|
|
154
|
+
# adapter, shared by every process using the same tables.
|
|
155
|
+
def sync_version
|
|
156
|
+
value = with_connection(@gate_class) do
|
|
157
|
+
sync_version_scope.pluck(:value).first
|
|
158
|
+
end
|
|
159
|
+
SyncVersion.parse(value)
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
# Public: Records the Flipper::SyncVersion of the flags just synced.
|
|
163
|
+
def sync_version=(version)
|
|
164
|
+
with_write_connection(@gate_class) do
|
|
165
|
+
@gate_class.transaction(requires_new: true) do
|
|
166
|
+
sync_version_scope.delete_all
|
|
167
|
+
begin
|
|
168
|
+
@gate_class.create! do |g|
|
|
169
|
+
g.feature_key = SyncVersion::KEY
|
|
170
|
+
g.key = SYNC_VERSION_GATE_KEY
|
|
171
|
+
g.value = version.to_s
|
|
172
|
+
end
|
|
173
|
+
rescue ::ActiveRecord::RecordNotUnique
|
|
174
|
+
# another process recorded the same version at the same time
|
|
175
|
+
end
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
|
|
131
180
|
# Public: Enables a gate for a given thing.
|
|
132
181
|
#
|
|
133
182
|
# feature - The Flipper::Feature for the gate.
|
|
@@ -136,6 +185,7 @@ module Flipper
|
|
|
136
185
|
#
|
|
137
186
|
# Returns true.
|
|
138
187
|
def enable(feature, gate, thing)
|
|
188
|
+
ensure_not_reserved_key!(feature)
|
|
139
189
|
case gate.data_type
|
|
140
190
|
when :boolean
|
|
141
191
|
set(feature, gate, thing, clear: true)
|
|
@@ -160,6 +210,7 @@ module Flipper
|
|
|
160
210
|
#
|
|
161
211
|
# Returns true.
|
|
162
212
|
def disable(feature, gate, thing)
|
|
213
|
+
ensure_not_reserved_key!(feature)
|
|
163
214
|
case gate.data_type
|
|
164
215
|
when :boolean
|
|
165
216
|
clear(feature)
|
|
@@ -187,6 +238,18 @@ module Flipper
|
|
|
187
238
|
|
|
188
239
|
private
|
|
189
240
|
|
|
241
|
+
def sync_version_scope
|
|
242
|
+
@gate_class.where(feature_key: SyncVersion::KEY, key: SYNC_VERSION_GATE_KEY)
|
|
243
|
+
end
|
|
244
|
+
|
|
245
|
+
def model_class(default_class, table_prefix, table_name)
|
|
246
|
+
return default_class if table_prefix.nil?
|
|
247
|
+
|
|
248
|
+
Class.new(default_class) do
|
|
249
|
+
self.table_name = "#{table_prefix}#{table_name}"
|
|
250
|
+
end
|
|
251
|
+
end
|
|
252
|
+
|
|
190
253
|
def set(feature, gate, thing, options = {})
|
|
191
254
|
clear_feature = options.fetch(:clear, false)
|
|
192
255
|
json_feature = options.fetch(:json, false)
|
|
@@ -236,21 +299,28 @@ module Flipper
|
|
|
236
299
|
end
|
|
237
300
|
|
|
238
301
|
def result_for_gates(feature, gates)
|
|
302
|
+
# Bucket rows by gate key in one pass instead of re-scanning all rows
|
|
303
|
+
# for each of the feature's gates. Features with many enabled actors
|
|
304
|
+
# can have thousands of rows. Rows with a nil key come from the outer
|
|
305
|
+
# join in get_all for features with no gate values.
|
|
306
|
+
values_by_key = nil
|
|
307
|
+
gates&.each do |key, value|
|
|
308
|
+
next if key.nil?
|
|
309
|
+
values_by_key ||= {}
|
|
310
|
+
(values_by_key[key.to_sym] ||= []) << value
|
|
311
|
+
end
|
|
312
|
+
|
|
239
313
|
result = {}
|
|
240
|
-
gates ||= []
|
|
241
314
|
feature.gates.each do |gate|
|
|
315
|
+
values = values_by_key && values_by_key[gate.key]
|
|
242
316
|
result[gate.key] =
|
|
243
317
|
case gate.data_type
|
|
244
318
|
when :boolean, :integer
|
|
245
|
-
|
|
246
|
-
row.last
|
|
247
|
-
end
|
|
319
|
+
values&.first
|
|
248
320
|
when :json
|
|
249
|
-
|
|
250
|
-
Typecast.from_json(row.last)
|
|
251
|
-
end
|
|
321
|
+
Typecast.from_json(values.first) if values
|
|
252
322
|
when :set
|
|
253
|
-
|
|
323
|
+
values ? values.to_set : Set.new
|
|
254
324
|
else
|
|
255
325
|
unsupported_data_type gate.data_type
|
|
256
326
|
end
|
|
@@ -296,7 +366,9 @@ module Flipper
|
|
|
296
366
|
|
|
297
367
|
def warned_about_value_not_text?
|
|
298
368
|
return @warned_about_value_not_text if defined?(@warned_about_value_not_text)
|
|
369
|
+
|
|
299
370
|
@warned_about_value_not_text = true
|
|
371
|
+
false
|
|
300
372
|
end
|
|
301
373
|
end
|
|
302
374
|
end
|
data/lib/flipper/version.rb
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
require 'rails/generators/active_record'
|
|
2
|
+
require 'generators/flipper/table_prefix'
|
|
2
3
|
|
|
3
4
|
module Flipper
|
|
4
5
|
module Generators
|
|
5
6
|
class ActiveRecordGenerator < ::Rails::Generators::Base
|
|
6
7
|
include ::Rails::Generators::Migration
|
|
8
|
+
include Flipper::Generators::TablePrefix
|
|
7
9
|
desc 'Generates migration for flipper tables'
|
|
8
10
|
|
|
9
11
|
source_paths << File.join(File.dirname(__FILE__), 'templates')
|
|
@@ -24,7 +26,8 @@ module Flipper
|
|
|
24
26
|
options = {
|
|
25
27
|
migration_version: migration_version,
|
|
26
28
|
}
|
|
27
|
-
|
|
29
|
+
destination = File.join('db/migrate', prefixed_migration_file_name('create_flipper_tables.rb'))
|
|
30
|
+
migration_template 'migration.erb', destination, options
|
|
28
31
|
end
|
|
29
32
|
|
|
30
33
|
def migration_version
|
|
@@ -1,22 +1,28 @@
|
|
|
1
|
-
|
|
1
|
+
<%
|
|
2
|
+
migration_class = respond_to?(:migration_class_name, true) ? migration_class_name('CreateFlipperTables') : 'CreateFlipperTables'
|
|
3
|
+
feature_table = respond_to?(:feature_table_name, true) ? feature_table_name : :flipper_features
|
|
4
|
+
gate_table = respond_to?(:gate_table_name, true) ? gate_table_name : :flipper_gates
|
|
5
|
+
feature_index = respond_to?(:feature_index_name, true) ? feature_index_name : nil
|
|
6
|
+
gate_index = respond_to?(:gate_index_name, true) ? gate_index_name : nil
|
|
7
|
+
%>class <%= migration_class %> < ActiveRecord::Migration<%= migration_version %>
|
|
2
8
|
def up
|
|
3
|
-
create_table
|
|
9
|
+
create_table <%= feature_table.inspect %> do |t|
|
|
4
10
|
t.string :key, null: false
|
|
5
11
|
t.timestamps null: false
|
|
6
12
|
end
|
|
7
|
-
add_index
|
|
13
|
+
add_index <%= feature_table.inspect %>, :key, unique: true<%= ", name: #{feature_index.inspect}" if feature_index %>
|
|
8
14
|
|
|
9
|
-
create_table
|
|
15
|
+
create_table <%= gate_table.inspect %> do |t|
|
|
10
16
|
t.string :feature_key, null: false
|
|
11
17
|
t.string :key, null: false
|
|
12
18
|
t.text :value
|
|
13
19
|
t.timestamps null: false
|
|
14
20
|
end
|
|
15
|
-
add_index
|
|
21
|
+
add_index <%= gate_table.inspect %>, [:feature_key, :key, :value], unique: true, length: { value: 255 }<%= ", name: #{gate_index.inspect}" if gate_index %>
|
|
16
22
|
end
|
|
17
23
|
|
|
18
24
|
def down
|
|
19
|
-
drop_table
|
|
20
|
-
drop_table
|
|
25
|
+
drop_table <%= gate_table.inspect %>
|
|
26
|
+
drop_table <%= feature_table.inspect %>
|
|
21
27
|
end
|
|
22
28
|
end
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: flipper-active_record
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.5.0.rc2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- John Nunemaker
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date: 2026-
|
|
11
|
+
date: 2026-11-24 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: flipper
|
|
@@ -16,14 +16,14 @@ dependencies:
|
|
|
16
16
|
requirements:
|
|
17
17
|
- - "~>"
|
|
18
18
|
- !ruby/object:Gem::Version
|
|
19
|
-
version: 1.
|
|
19
|
+
version: 1.5.0.rc2
|
|
20
20
|
type: :runtime
|
|
21
21
|
prerelease: false
|
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
|
23
23
|
requirements:
|
|
24
24
|
- - "~>"
|
|
25
25
|
- !ruby/object:Gem::Version
|
|
26
|
-
version: 1.
|
|
26
|
+
version: 1.5.0.rc2
|
|
27
27
|
- !ruby/object:Gem::Dependency
|
|
28
28
|
name: activerecord
|
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -50,15 +50,6 @@ executables: []
|
|
|
50
50
|
extensions: []
|
|
51
51
|
extra_rdoc_files: []
|
|
52
52
|
files:
|
|
53
|
-
- benchmark/active_record_adapter_ips.rb
|
|
54
|
-
- benchmark/active_record_ips.rb
|
|
55
|
-
- benchmark/active_record_setup.rb
|
|
56
|
-
- examples/active_record/ar_setup.rb
|
|
57
|
-
- examples/active_record/basic.rb
|
|
58
|
-
- examples/active_record/cached.rb
|
|
59
|
-
- examples/active_record/group_migration.rb
|
|
60
|
-
- examples/active_record/internals.rb
|
|
61
|
-
- flipper-active_record.gemspec
|
|
62
53
|
- lib/flipper-active_record.rb
|
|
63
54
|
- lib/flipper/adapters/active_record.rb
|
|
64
55
|
- lib/flipper/adapters/active_record/feature.rb
|
|
@@ -67,9 +58,6 @@ files:
|
|
|
67
58
|
- lib/flipper/version.rb
|
|
68
59
|
- lib/generators/flipper/active_record_generator.rb
|
|
69
60
|
- lib/generators/flipper/templates/migration.erb
|
|
70
|
-
- spec/flipper/adapters/active_record_spec.rb
|
|
71
|
-
- test/adapters/active_record_test.rb
|
|
72
|
-
- test_rails/generators/flipper/active_record_generator_test.rb
|
|
73
61
|
homepage: https://www.flippercloud.io/docs/adapters/active-record
|
|
74
62
|
licenses:
|
|
75
63
|
- MIT
|
|
@@ -78,7 +66,7 @@ metadata:
|
|
|
78
66
|
homepage_uri: https://www.flippercloud.io
|
|
79
67
|
source_code_uri: https://github.com/flippercloud/flipper
|
|
80
68
|
bug_tracker_uri: https://github.com/flippercloud/flipper/issues
|
|
81
|
-
changelog_uri: https://github.com/flippercloud/flipper/releases/tag/v1.
|
|
69
|
+
changelog_uri: https://github.com/flippercloud/flipper/releases/tag/v1.5.0.rc2
|
|
82
70
|
funding_uri: https://github.com/sponsors/flippercloud
|
|
83
71
|
post_install_message:
|
|
84
72
|
rdoc_options: []
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
require 'bundler/setup'
|
|
2
|
-
require_relative './active_record_setup'
|
|
3
|
-
require 'flipper'
|
|
4
|
-
require 'flipper/adapters/active_record'
|
|
5
|
-
require 'benchmark/ips'
|
|
6
|
-
|
|
7
|
-
flipper = Flipper.new(Flipper::Adapters::ActiveRecord.new)
|
|
8
|
-
|
|
9
|
-
10.times do |n|
|
|
10
|
-
2000.times do |i|
|
|
11
|
-
flipper.enable_actor 'feature' + n.to_s, Flipper::Actor.new("User;#{i}")
|
|
12
|
-
end
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
Benchmark.ips do |x|
|
|
16
|
-
x.report("get_all") { flipper.preload_all }
|
|
17
|
-
x.report("features") { flipper.features }
|
|
18
|
-
end
|
|
@@ -1,17 +0,0 @@
|
|
|
1
|
-
require 'bundler/setup'
|
|
2
|
-
require_relative './active_record_setup'
|
|
3
|
-
require 'flipper'
|
|
4
|
-
require 'flipper/adapters/active_record'
|
|
5
|
-
require 'benchmark/ips'
|
|
6
|
-
|
|
7
|
-
flipper = Flipper.new(Flipper::Adapters::ActiveRecord.new)
|
|
8
|
-
|
|
9
|
-
2000.times do |i|
|
|
10
|
-
flipper.enable_actor :foo, Flipper::Actor.new("User;#{i}")
|
|
11
|
-
end
|
|
12
|
-
|
|
13
|
-
Benchmark.ips do |x|
|
|
14
|
-
x.report("all") { Flipper::Adapters::ActiveRecord::Gate.where(feature_key: "foo".freeze).load }
|
|
15
|
-
x.report("pluck") { Flipper::Adapters::ActiveRecord::Gate.where(feature_key: "foo".freeze).pluck(:key, :value) }
|
|
16
|
-
x.compare!
|
|
17
|
-
end
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
require 'bundler/setup'
|
|
2
|
-
require 'active_record'
|
|
3
|
-
|
|
4
|
-
ActiveRecord::Base.establish_connection({
|
|
5
|
-
adapter: 'sqlite3',
|
|
6
|
-
database: ':memory:',
|
|
7
|
-
})
|
|
8
|
-
|
|
9
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
10
|
-
CREATE TABLE flipper_features (
|
|
11
|
-
id integer PRIMARY KEY,
|
|
12
|
-
key text NOT NULL UNIQUE,
|
|
13
|
-
created_at datetime NOT NULL,
|
|
14
|
-
updated_at datetime NOT NULL
|
|
15
|
-
)
|
|
16
|
-
SQL
|
|
17
|
-
|
|
18
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
19
|
-
CREATE TABLE flipper_gates (
|
|
20
|
-
id integer PRIMARY KEY,
|
|
21
|
-
feature_key text NOT NULL,
|
|
22
|
-
key text NOT NULL,
|
|
23
|
-
value text DEFAULT NULL,
|
|
24
|
-
created_at datetime NOT NULL,
|
|
25
|
-
updated_at datetime NOT NULL
|
|
26
|
-
)
|
|
27
|
-
SQL
|
|
28
|
-
|
|
29
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
30
|
-
CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value)
|
|
31
|
-
SQL
|
|
@@ -1,45 +0,0 @@
|
|
|
1
|
-
require 'bundler/setup'
|
|
2
|
-
require 'active_record'
|
|
3
|
-
ActiveRecord::Base.establish_connection({
|
|
4
|
-
adapter: 'sqlite3',
|
|
5
|
-
database: ':memory:',
|
|
6
|
-
})
|
|
7
|
-
|
|
8
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
9
|
-
CREATE TABLE flipper_features (
|
|
10
|
-
id integer PRIMARY KEY,
|
|
11
|
-
key text NOT NULL UNIQUE,
|
|
12
|
-
created_at datetime NOT NULL,
|
|
13
|
-
updated_at datetime NOT NULL
|
|
14
|
-
)
|
|
15
|
-
SQL
|
|
16
|
-
|
|
17
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
18
|
-
CREATE TABLE flipper_gates (
|
|
19
|
-
id integer PRIMARY KEY,
|
|
20
|
-
feature_key text NOT NULL,
|
|
21
|
-
key text NOT NULL,
|
|
22
|
-
value text DEFAULT NULL,
|
|
23
|
-
created_at datetime NOT NULL,
|
|
24
|
-
updated_at datetime NOT NULL
|
|
25
|
-
)
|
|
26
|
-
SQL
|
|
27
|
-
|
|
28
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
29
|
-
CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value)
|
|
30
|
-
SQL
|
|
31
|
-
|
|
32
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
33
|
-
CREATE TABLE users (
|
|
34
|
-
id integer PRIMARY KEY,
|
|
35
|
-
name string NOT NULL,
|
|
36
|
-
influencer boolean,
|
|
37
|
-
created_at datetime NOT NULL,
|
|
38
|
-
updated_at datetime NOT NULL
|
|
39
|
-
)
|
|
40
|
-
SQL
|
|
41
|
-
|
|
42
|
-
require 'flipper/model/active_record'
|
|
43
|
-
class User < ActiveRecord::Base
|
|
44
|
-
include Flipper::Model::ActiveRecord
|
|
45
|
-
end
|
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
require_relative "./ar_setup"
|
|
2
|
-
|
|
3
|
-
# Requires the flipper-active_record gem to be installed.
|
|
4
|
-
require 'flipper/adapters/active_record'
|
|
5
|
-
|
|
6
|
-
Flipper[:stats].enable
|
|
7
|
-
|
|
8
|
-
if Flipper[:stats].enabled?
|
|
9
|
-
puts "Enabled!"
|
|
10
|
-
else
|
|
11
|
-
puts "Disabled!"
|
|
12
|
-
end
|
|
13
|
-
|
|
14
|
-
Flipper[:stats].disable
|
|
15
|
-
|
|
16
|
-
if Flipper[:stats].enabled?
|
|
17
|
-
puts "Enabled!"
|
|
18
|
-
else
|
|
19
|
-
puts "Disabled!"
|
|
20
|
-
end
|
|
@@ -1,23 +0,0 @@
|
|
|
1
|
-
require_relative "./ar_setup"
|
|
2
|
-
|
|
3
|
-
# Requires the flipper-active_record gem to be installed.
|
|
4
|
-
require 'flipper/adapters/active_record'
|
|
5
|
-
require 'flipper/adapters/active_support_cache_store'
|
|
6
|
-
|
|
7
|
-
Flipper.configure do |config|
|
|
8
|
-
config.adapter do
|
|
9
|
-
Flipper::Adapters::ActiveSupportCacheStore.new(
|
|
10
|
-
Flipper::Adapters::ActiveRecord.new,
|
|
11
|
-
ActiveSupport::Cache::MemoryStore.new,
|
|
12
|
-
expires_in: 2.seconds
|
|
13
|
-
)
|
|
14
|
-
end
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
ActiveRecord::Base.logger = Logger.new(STDOUT)
|
|
18
|
-
|
|
19
|
-
puts "You should see 5 or 6 queries."
|
|
20
|
-
10.times do |i|
|
|
21
|
-
Flipper.enabled?(:foo)
|
|
22
|
-
sleep 1
|
|
23
|
-
end
|
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
# This is an example script that shows how to migrate from a bunch of individual
|
|
2
|
-
# actors to a group. Should be useful for those who have ended up with large
|
|
3
|
-
# actor sets and want to slim them down for performance reasons.
|
|
4
|
-
|
|
5
|
-
require_relative "./ar_setup"
|
|
6
|
-
require 'flipper/adapters/active_record'
|
|
7
|
-
require 'active_support/all'
|
|
8
|
-
|
|
9
|
-
# 1. enable feature for 100 actors, make 80 influencers
|
|
10
|
-
users = 100.times.map do |n|
|
|
11
|
-
influencer = n < 80 ? true : false
|
|
12
|
-
user = User.create(name: n, influencer: influencer)
|
|
13
|
-
Flipper.enable :stats, user
|
|
14
|
-
user
|
|
15
|
-
end
|
|
16
|
-
|
|
17
|
-
# check enabled, should all be because individual actors are enabled
|
|
18
|
-
print 'Should be [[true, 100]]: '
|
|
19
|
-
print users.group_by { |user| Flipper.enabled?(:stats, user) }.map { |result, users| [result, users.size]}
|
|
20
|
-
puts
|
|
21
|
-
|
|
22
|
-
# 2. register a group so flipper knows what to do with it
|
|
23
|
-
Flipper.register(:influencers) do |actor, context|
|
|
24
|
-
actor.respond_to?(:influencer) && actor.influencer
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
# 3. enable group for feature, THIS IS IMPORTANT
|
|
28
|
-
Flipper.enable :stats, :influencers
|
|
29
|
-
|
|
30
|
-
# check enabled again, should all still be true because individual actors are
|
|
31
|
-
# enabled, but also the group gate would return true for 80 influencers. At this
|
|
32
|
-
# point, it's kind of double true but flipper just cares if any gate returns true.
|
|
33
|
-
print 'Should be [[true, 100]]: '
|
|
34
|
-
print users.group_by { |user| Flipper.enabled?(:stats, user) }.map { |result, users| [result, users.size]}
|
|
35
|
-
puts
|
|
36
|
-
|
|
37
|
-
# 4. now we want to clean up the actors that are covered by the group to slim down
|
|
38
|
-
# the actor set size. So we loop through actors and remove them if group returns
|
|
39
|
-
# true for the provided actor and context.
|
|
40
|
-
Flipper[:stats].actors_value.each do |flipper_id|
|
|
41
|
-
# Hydrate the flipper_id into an active record object. Modify this based on
|
|
42
|
-
# your flipper_id's if you use anything other than active record models and
|
|
43
|
-
# the default flipper_id provided by flipper.
|
|
44
|
-
class_name, id = flipper_id.split(';')
|
|
45
|
-
klass = class_name.constantize
|
|
46
|
-
user = klass.find(id)
|
|
47
|
-
|
|
48
|
-
# if user is in group then disable for actor because they'll still get the feature
|
|
49
|
-
context = Flipper::FeatureCheckContext.new(
|
|
50
|
-
feature_name: :stats,
|
|
51
|
-
values: Flipper[:stats].gate_values,
|
|
52
|
-
actors: [Flipper::Types::Actor.wrap(user)]
|
|
53
|
-
)
|
|
54
|
-
|
|
55
|
-
if Flipper::Gates::Group.new.open?(context)
|
|
56
|
-
Flipper.disable(:stats, user)
|
|
57
|
-
end
|
|
58
|
-
end
|
|
59
|
-
|
|
60
|
-
# check enabled again, should be the same result as previous checks
|
|
61
|
-
print 'Should be [[true, 100]]: '
|
|
62
|
-
print users.group_by { |user| Flipper.enabled?(:stats, user) }.map { |result, users| [result, users.size]}
|
|
63
|
-
puts
|
|
64
|
-
|
|
65
|
-
puts "Actors enabled: #{Flipper[:stats].actors_value.size}"
|
|
66
|
-
puts "Groups enabled: #{Flipper[:stats].groups_value.size}"
|
|
67
|
-
|
|
68
|
-
puts "All actors that could be migrated to groups were migrated. Yay!"
|
|
@@ -1,107 +0,0 @@
|
|
|
1
|
-
require_relative "./ar_setup"
|
|
2
|
-
|
|
3
|
-
# Requires the flipper-active_record gem to be installed.
|
|
4
|
-
require 'flipper/adapters/active_record'
|
|
5
|
-
|
|
6
|
-
# Register a few groups.
|
|
7
|
-
Flipper.register(:admins) { |actor| actor.admin? }
|
|
8
|
-
Flipper.register(:early_access) { |actor| actor.early_access? }
|
|
9
|
-
|
|
10
|
-
# Create a user class that has flipper_id instance method.
|
|
11
|
-
User = Struct.new(:flipper_id)
|
|
12
|
-
|
|
13
|
-
Flipper[:stats].enable
|
|
14
|
-
Flipper[:stats].enable_group :admins
|
|
15
|
-
Flipper[:stats].enable_group :early_access
|
|
16
|
-
Flipper[:stats].enable_actor User.new('25')
|
|
17
|
-
Flipper[:stats].enable_actor User.new('90')
|
|
18
|
-
Flipper[:stats].enable_actor User.new('180')
|
|
19
|
-
Flipper[:stats].enable_percentage_of_time 15
|
|
20
|
-
Flipper[:stats].enable_percentage_of_actors 45
|
|
21
|
-
|
|
22
|
-
Flipper[:search].enable
|
|
23
|
-
|
|
24
|
-
puts 'all rows in features table'
|
|
25
|
-
pp Flipper::Adapters::ActiveRecord::Feature.all
|
|
26
|
-
# [#<Flipper::Adapters::ActiveRecord::Feature:0x007fd259b47110
|
|
27
|
-
# id: 1,
|
|
28
|
-
# key: "stats",
|
|
29
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
30
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
31
|
-
# #<Flipper::Adapters::ActiveRecord::Feature:0x007fd259b46cd8
|
|
32
|
-
# id: 2,
|
|
33
|
-
# key: "search",
|
|
34
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
35
|
-
# updated_at: 2015-12-21 16:26:29 UTC>]
|
|
36
|
-
puts
|
|
37
|
-
|
|
38
|
-
puts 'all rows in gates table'
|
|
39
|
-
pp Flipper::Adapters::ActiveRecord::Gate.all
|
|
40
|
-
# [#<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0f0f8
|
|
41
|
-
# id: 1,
|
|
42
|
-
# feature_key: "stats",
|
|
43
|
-
# key: "boolean",
|
|
44
|
-
# value: "true",
|
|
45
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
46
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
47
|
-
# #<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0ebd0
|
|
48
|
-
# id: 2,
|
|
49
|
-
# feature_key: "stats",
|
|
50
|
-
# key: "groups",
|
|
51
|
-
# value: "admins",
|
|
52
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
53
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
54
|
-
# #<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0e748
|
|
55
|
-
# id: 3,
|
|
56
|
-
# feature_key: "stats",
|
|
57
|
-
# key: "groups",
|
|
58
|
-
# value: "early_access",
|
|
59
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
60
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
61
|
-
# #<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0e568
|
|
62
|
-
# id: 4,
|
|
63
|
-
# feature_key: "stats",
|
|
64
|
-
# key: "actors",
|
|
65
|
-
# value: "25",
|
|
66
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
67
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
68
|
-
# #<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0e0b8
|
|
69
|
-
# id: 5,
|
|
70
|
-
# feature_key: "stats",
|
|
71
|
-
# key: "actors",
|
|
72
|
-
# value: "90",
|
|
73
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
74
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
75
|
-
# #<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0da50
|
|
76
|
-
# id: 6,
|
|
77
|
-
# feature_key: "stats",
|
|
78
|
-
# key: "actors",
|
|
79
|
-
# value: "180",
|
|
80
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
81
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
82
|
-
# #<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0d3c0
|
|
83
|
-
# id: 7,
|
|
84
|
-
# feature_key: "stats",
|
|
85
|
-
# key: "percentage_of_time",
|
|
86
|
-
# value: "15",
|
|
87
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
88
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
89
|
-
# #<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0cdf8
|
|
90
|
-
# id: 8,
|
|
91
|
-
# feature_key: "stats",
|
|
92
|
-
# key: "percentage_of_actors",
|
|
93
|
-
# value: "45",
|
|
94
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
95
|
-
# updated_at: 2015-12-21 16:26:29 UTC>,
|
|
96
|
-
# #<Flipper::Adapters::ActiveRecord::Gate:0x007fd259b0cbf0
|
|
97
|
-
# id: 9,
|
|
98
|
-
# feature_key: "search",
|
|
99
|
-
# key: "boolean",
|
|
100
|
-
# value: "true",
|
|
101
|
-
# created_at: 2015-12-21 16:26:29 UTC,
|
|
102
|
-
# updated_at: 2015-12-21 16:26:29 UTC>]
|
|
103
|
-
puts
|
|
104
|
-
|
|
105
|
-
puts 'flipper get of feature'
|
|
106
|
-
pp Flipper.adapter.get(Flipper[:stats])
|
|
107
|
-
# flipper get of feature
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
|
2
|
-
require File.expand_path('../lib/flipper/version', __FILE__)
|
|
3
|
-
require File.expand_path('../lib/flipper/metadata', __FILE__)
|
|
4
|
-
require "set"
|
|
5
|
-
|
|
6
|
-
# Files that should exist in main flipper gem.
|
|
7
|
-
main_flipper_active_record_files = Set[
|
|
8
|
-
"lib/flipper/model/active_record.rb",
|
|
9
|
-
"spec/flipper/model/active_record_spec.rb",
|
|
10
|
-
]
|
|
11
|
-
|
|
12
|
-
flipper_active_record_files = lambda do |file|
|
|
13
|
-
file =~ /active_record/ && !main_flipper_active_record_files.include?(file)
|
|
14
|
-
end
|
|
15
|
-
|
|
16
|
-
Gem::Specification.new do |gem|
|
|
17
|
-
gem.authors = ['John Nunemaker']
|
|
18
|
-
gem.email = 'support@flippercloud.io'
|
|
19
|
-
gem.summary = 'ActiveRecord feature flag adapter for Flipper'
|
|
20
|
-
gem.license = 'MIT'
|
|
21
|
-
gem.homepage = 'https://www.flippercloud.io/docs/adapters/active-record'
|
|
22
|
-
|
|
23
|
-
extra_files = [
|
|
24
|
-
'lib/generators/flipper/templates/migration.erb',
|
|
25
|
-
'lib/flipper/version.rb',
|
|
26
|
-
]
|
|
27
|
-
gem.files = `git ls-files`.split("\n").select(&flipper_active_record_files) + extra_files
|
|
28
|
-
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n").select(&flipper_active_record_files)
|
|
29
|
-
gem.name = 'flipper-active_record'
|
|
30
|
-
gem.require_paths = ['lib']
|
|
31
|
-
gem.version = Flipper::VERSION
|
|
32
|
-
gem.metadata = Flipper::METADATA
|
|
33
|
-
|
|
34
|
-
gem.add_dependency 'flipper', "~> #{Flipper::VERSION}"
|
|
35
|
-
gem.add_dependency 'activerecord', '>= 4.2', '< 9'
|
|
36
|
-
end
|
|
@@ -1,381 +0,0 @@
|
|
|
1
|
-
SpecHelpers.silence { require 'flipper/adapters/active_record' }
|
|
2
|
-
|
|
3
|
-
# Turn off migration logging for specs
|
|
4
|
-
ActiveRecord::Migration.verbose = false
|
|
5
|
-
ActiveRecord::Tasks::DatabaseTasks.root = File.dirname(__FILE__)
|
|
6
|
-
|
|
7
|
-
RSpec.describe Flipper::Adapters::ActiveRecord do
|
|
8
|
-
subject { described_class.new }
|
|
9
|
-
|
|
10
|
-
before(:all) do
|
|
11
|
-
# Eval migration template so we can run migration against each database
|
|
12
|
-
template_path = File.join(File.dirname(__FILE__), '../../../lib/generators/flipper/templates/migration.erb')
|
|
13
|
-
migration = ERB.new(File.read(template_path))
|
|
14
|
-
migration_version = "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
|
|
15
|
-
eval migration.result_with_hash(migration_version: migration_version) # defines CreateFlipperTables
|
|
16
|
-
end
|
|
17
|
-
|
|
18
|
-
[
|
|
19
|
-
{
|
|
20
|
-
"adapter" => "sqlite3",
|
|
21
|
-
"database" => ":memory:"
|
|
22
|
-
},
|
|
23
|
-
|
|
24
|
-
{
|
|
25
|
-
"adapter" => "mysql2",
|
|
26
|
-
"encoding" => "utf8mb4",
|
|
27
|
-
"host" => ENV["MYSQL_HOST"],
|
|
28
|
-
"username" => ENV["MYSQL_USER"] || "root",
|
|
29
|
-
"password" => ENV["MYSQL_PASSWORD"] || "",
|
|
30
|
-
"database" => ENV["MYSQL_DATABASE"] || "flipper_test",
|
|
31
|
-
"port" => ENV["DB_PORT"] || 3306
|
|
32
|
-
},
|
|
33
|
-
|
|
34
|
-
{
|
|
35
|
-
"adapter" => "postgresql",
|
|
36
|
-
"encoding" => "unicode",
|
|
37
|
-
"host" => "127.0.0.1",
|
|
38
|
-
"username" => ENV["POSTGRES_USER"] || "",
|
|
39
|
-
"password" => ENV["POSTGRES_PASSWORD"] || "",
|
|
40
|
-
"database" => ENV["POSTGRES_DATABASE"] || "flipper_test",
|
|
41
|
-
}
|
|
42
|
-
].each do |config|
|
|
43
|
-
context "with #{config['adapter']}" do
|
|
44
|
-
context "with tables created" do
|
|
45
|
-
before(:all) do
|
|
46
|
-
skip_on_error(ActiveRecord::ConnectionNotEstablished, "#{config['adapter']} not available") do
|
|
47
|
-
silence do
|
|
48
|
-
ActiveRecord::Tasks::DatabaseTasks.create(config)
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
|
-
Flipper.configuration = nil
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
before(:each) do
|
|
56
|
-
silence do
|
|
57
|
-
ActiveRecord::Tasks::DatabaseTasks.purge(config)
|
|
58
|
-
CreateFlipperTables.migrate(:up)
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
after(:all) do
|
|
63
|
-
silence { ActiveRecord::Tasks::DatabaseTasks.drop(config) } unless $skip
|
|
64
|
-
end
|
|
65
|
-
|
|
66
|
-
it_should_behave_like 'a flipper adapter'
|
|
67
|
-
|
|
68
|
-
it "should load actor ids fine" do
|
|
69
|
-
flipper.enable_percentage_of_time(:foo, 1)
|
|
70
|
-
|
|
71
|
-
Flipper::Adapters::ActiveRecord::Gate.create!(
|
|
72
|
-
feature_key: "foo",
|
|
73
|
-
key: "actors",
|
|
74
|
-
value: "Organization;4",
|
|
75
|
-
)
|
|
76
|
-
|
|
77
|
-
flipper = Flipper.new(subject)
|
|
78
|
-
flipper.preload([:foo])
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
it 'should not poison wrapping transactions' do
|
|
82
|
-
flipper = Flipper.new(subject)
|
|
83
|
-
|
|
84
|
-
actor = Struct.new(:flipper_id).new('flipper-id-123')
|
|
85
|
-
flipper.enable_actor(:foo, actor)
|
|
86
|
-
|
|
87
|
-
ActiveRecord::Base.transaction do
|
|
88
|
-
flipper.enable_actor(:foo, actor)
|
|
89
|
-
# any read on the next line is fine, just need to ensure that
|
|
90
|
-
# poisoned transaction isn't raised
|
|
91
|
-
expect(Flipper::Adapters::ActiveRecord::Gate.count).to eq(1)
|
|
92
|
-
end
|
|
93
|
-
end
|
|
94
|
-
|
|
95
|
-
context "ActiveRecord connection_pool" do
|
|
96
|
-
before do
|
|
97
|
-
clear_active_connections!
|
|
98
|
-
end
|
|
99
|
-
|
|
100
|
-
context "#features" do
|
|
101
|
-
it "does not hold onto connections" do
|
|
102
|
-
expect(active_connections?).to be(false)
|
|
103
|
-
subject.features
|
|
104
|
-
expect(active_connections?).to be(false)
|
|
105
|
-
end
|
|
106
|
-
|
|
107
|
-
it "does not release previously held connection" do
|
|
108
|
-
ActiveRecord::Base.connection # establish a new connection
|
|
109
|
-
expect(active_connections?).to be(true)
|
|
110
|
-
subject.features
|
|
111
|
-
expect(active_connections?).to be(true)
|
|
112
|
-
end
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
context "#get_all" do
|
|
116
|
-
it "does not hold onto connections" do
|
|
117
|
-
expect(active_connections?).to be(false)
|
|
118
|
-
subject.get_all
|
|
119
|
-
expect(active_connections?).to be(false)
|
|
120
|
-
end
|
|
121
|
-
|
|
122
|
-
it "does not release previously held connection" do
|
|
123
|
-
ActiveRecord::Base.connection # establish a new connection
|
|
124
|
-
expect(active_connections?).to be(true)
|
|
125
|
-
subject.get_all
|
|
126
|
-
expect(active_connections?).to be(true)
|
|
127
|
-
end
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
context "#add / #remove / #clear" do
|
|
131
|
-
let(:feature) { Flipper::Feature.new(:search, subject) }
|
|
132
|
-
|
|
133
|
-
it "does not hold onto connections" do
|
|
134
|
-
expect(active_connections?).to be(false)
|
|
135
|
-
subject.add(feature)
|
|
136
|
-
expect(active_connections?).to be(false)
|
|
137
|
-
subject.remove(feature)
|
|
138
|
-
expect(active_connections?).to be(false)
|
|
139
|
-
subject.clear(feature)
|
|
140
|
-
expect(active_connections?).to be(false)
|
|
141
|
-
end
|
|
142
|
-
|
|
143
|
-
it "does not release previously held connection" do
|
|
144
|
-
ActiveRecord::Base.connection # establish a new connection
|
|
145
|
-
expect(active_connections?).to be(true)
|
|
146
|
-
subject.add(feature)
|
|
147
|
-
expect(active_connections?).to be(true)
|
|
148
|
-
subject.remove(feature)
|
|
149
|
-
expect(active_connections?).to be(true)
|
|
150
|
-
subject.clear(feature)
|
|
151
|
-
expect(active_connections?).to be(true)
|
|
152
|
-
end
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
context "#get_multi" do
|
|
156
|
-
let(:feature) { Flipper::Feature.new(:search, subject) }
|
|
157
|
-
|
|
158
|
-
it "does not hold onto connections" do
|
|
159
|
-
expect(active_connections?).to be(false)
|
|
160
|
-
subject.get_multi([feature])
|
|
161
|
-
expect(active_connections?).to be(false)
|
|
162
|
-
end
|
|
163
|
-
|
|
164
|
-
it "does not release previously held connection" do
|
|
165
|
-
ActiveRecord::Base.connection # establish a new connection
|
|
166
|
-
expect(active_connections?).to be(true)
|
|
167
|
-
subject.get_multi([feature])
|
|
168
|
-
expect(active_connections?).to be(true)
|
|
169
|
-
end
|
|
170
|
-
end
|
|
171
|
-
|
|
172
|
-
context "#enable/#disable boolean" do
|
|
173
|
-
let(:feature) { Flipper::Feature.new(:search, subject) }
|
|
174
|
-
let(:gate) { feature.gate(:boolean)}
|
|
175
|
-
|
|
176
|
-
it "does not hold onto connections" do
|
|
177
|
-
expect(active_connections?).to be(false)
|
|
178
|
-
subject.enable(feature, gate, gate.wrap(true))
|
|
179
|
-
expect(active_connections?).to be(false)
|
|
180
|
-
subject.disable(feature, gate, gate.wrap(false))
|
|
181
|
-
expect(active_connections?).to be(false)
|
|
182
|
-
end
|
|
183
|
-
|
|
184
|
-
it "does not release previously held connection" do
|
|
185
|
-
ActiveRecord::Base.connection # establish a new connection
|
|
186
|
-
expect(active_connections?).to be(true)
|
|
187
|
-
subject.enable(feature, gate, gate.wrap(true))
|
|
188
|
-
expect(active_connections?).to be(true)
|
|
189
|
-
subject.disable(feature, gate, gate.wrap(false))
|
|
190
|
-
expect(active_connections?).to be(true)
|
|
191
|
-
end
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
context "#enable/#disable set" do
|
|
195
|
-
let(:feature) { Flipper::Feature.new(:search, subject) }
|
|
196
|
-
let(:gate) { feature.gate(:group) }
|
|
197
|
-
|
|
198
|
-
it "does not hold onto connections" do
|
|
199
|
-
expect(active_connections?).to be(false)
|
|
200
|
-
subject.enable(feature, gate, gate.wrap(:admin))
|
|
201
|
-
expect(active_connections?).to be(false)
|
|
202
|
-
subject.disable(feature, gate, gate.wrap(:admin))
|
|
203
|
-
expect(active_connections?).to be(false)
|
|
204
|
-
end
|
|
205
|
-
|
|
206
|
-
it "does not release previously held connection" do
|
|
207
|
-
ActiveRecord::Base.connection # establish a new connection
|
|
208
|
-
expect(active_connections?).to be(true)
|
|
209
|
-
subject.enable(feature, gate, gate.wrap(:admin))
|
|
210
|
-
expect(active_connections?).to be(true)
|
|
211
|
-
subject.disable(feature, gate, gate.wrap(:admin))
|
|
212
|
-
expect(active_connections?).to be(true)
|
|
213
|
-
end
|
|
214
|
-
end
|
|
215
|
-
end
|
|
216
|
-
|
|
217
|
-
if ActiveRecord.version >= Gem::Version.new('7.1')
|
|
218
|
-
context 'with read/write roles' do
|
|
219
|
-
before do
|
|
220
|
-
skip "connected_to with roles is not supported on #{config['adapter']}" if config["adapter"] == "sqlite3"
|
|
221
|
-
end
|
|
222
|
-
|
|
223
|
-
let(:abstract_class) do
|
|
224
|
-
# Create a named abstract class (Rails requires names for connects_to)
|
|
225
|
-
klass = Class.new(ActiveRecord::Base) do
|
|
226
|
-
self.abstract_class = true
|
|
227
|
-
end
|
|
228
|
-
stub_const('TestApplicationRecord', klass)
|
|
229
|
-
|
|
230
|
-
# Now configure connects_to with the same database for both roles
|
|
231
|
-
# In production, these would be different (primary/replica)
|
|
232
|
-
klass.connects_to database: {
|
|
233
|
-
writing: config,
|
|
234
|
-
reading: config
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
klass
|
|
238
|
-
end
|
|
239
|
-
|
|
240
|
-
after do
|
|
241
|
-
# Disconnect role-based connections to avoid interfering with database cleanup
|
|
242
|
-
clear_all_connections!
|
|
243
|
-
end
|
|
244
|
-
|
|
245
|
-
let(:feature_class) do
|
|
246
|
-
klass = Class.new(abstract_class) do
|
|
247
|
-
self.table_name = 'flipper_features'
|
|
248
|
-
validates :key, presence: true
|
|
249
|
-
end
|
|
250
|
-
stub_const('TestFeature', klass)
|
|
251
|
-
klass
|
|
252
|
-
end
|
|
253
|
-
|
|
254
|
-
let(:gate_class) do
|
|
255
|
-
klass = Class.new(abstract_class) do
|
|
256
|
-
self.table_name = 'flipper_gates'
|
|
257
|
-
end
|
|
258
|
-
stub_const('TestGate', klass)
|
|
259
|
-
klass
|
|
260
|
-
end
|
|
261
|
-
|
|
262
|
-
let(:adapter_with_roles) do
|
|
263
|
-
described_class.new(
|
|
264
|
-
feature_class: feature_class,
|
|
265
|
-
gate_class: gate_class
|
|
266
|
-
)
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
it 'can perform write operations when forced to reading role' do
|
|
270
|
-
abstract_class.connected_to(role: :reading) do
|
|
271
|
-
flipper = Flipper.new(adapter_with_roles)
|
|
272
|
-
|
|
273
|
-
feature = flipper[:test_feature]
|
|
274
|
-
expect { feature.enable }.not_to raise_error
|
|
275
|
-
expect(feature.enabled?).to be(true)
|
|
276
|
-
expect { feature.disable }.not_to raise_error
|
|
277
|
-
expect(feature.enabled?).to be(false)
|
|
278
|
-
|
|
279
|
-
feature = flipper[:actor_test]
|
|
280
|
-
actor = Struct.new(:flipper_id).new(123)
|
|
281
|
-
expect { feature.enable_actor(actor) }.not_to raise_error
|
|
282
|
-
expect(feature.enabled?(actor)).to be(true)
|
|
283
|
-
expect { feature.disable_actor(actor) }.not_to raise_error
|
|
284
|
-
expect(feature.enabled?(actor)).to be(false)
|
|
285
|
-
|
|
286
|
-
feature = flipper[:gate_test]
|
|
287
|
-
expect { feature.enable_percentage_of_time(50) }.not_to raise_error
|
|
288
|
-
expect { feature.disable_percentage_of_time }.not_to raise_error
|
|
289
|
-
feature.enable
|
|
290
|
-
expect { feature.remove }.not_to raise_error
|
|
291
|
-
|
|
292
|
-
feature = flipper[:expression_test]
|
|
293
|
-
expression = Flipper.property(:plan).eq("premium")
|
|
294
|
-
expect { feature.enable_expression(expression) }.not_to raise_error
|
|
295
|
-
expect(feature.expression).to eq(expression)
|
|
296
|
-
expect { feature.disable_expression }.not_to raise_error
|
|
297
|
-
expect(feature.expression).to be_nil
|
|
298
|
-
end
|
|
299
|
-
end
|
|
300
|
-
|
|
301
|
-
it 'does not hold onto connections during write operations' do
|
|
302
|
-
clear_active_connections!
|
|
303
|
-
|
|
304
|
-
abstract_class.connected_to(role: :reading) do
|
|
305
|
-
flipper = Flipper.new(adapter_with_roles)
|
|
306
|
-
feature = flipper[:connection_test]
|
|
307
|
-
|
|
308
|
-
feature.enable
|
|
309
|
-
expect(active_connections?).to be(false)
|
|
310
|
-
end
|
|
311
|
-
end
|
|
312
|
-
end
|
|
313
|
-
end
|
|
314
|
-
|
|
315
|
-
context 'requiring "flipper-active_record"' do
|
|
316
|
-
before do
|
|
317
|
-
Flipper.configuration = nil
|
|
318
|
-
Flipper.instance = nil
|
|
319
|
-
|
|
320
|
-
silence { load 'flipper/adapters/active_record.rb' }
|
|
321
|
-
end
|
|
322
|
-
|
|
323
|
-
it 'configures itself' do
|
|
324
|
-
expect(Flipper.adapter.adapter).to be_a(Flipper::Adapters::ActiveRecord)
|
|
325
|
-
end
|
|
326
|
-
end
|
|
327
|
-
end
|
|
328
|
-
|
|
329
|
-
context "without tables created" do
|
|
330
|
-
before(:all) do
|
|
331
|
-
skip_on_error(ActiveRecord::ConnectionNotEstablished, "#{config['adapter']} not available") do
|
|
332
|
-
silence do
|
|
333
|
-
ActiveRecord::Tasks::DatabaseTasks.create(config)
|
|
334
|
-
end
|
|
335
|
-
end
|
|
336
|
-
|
|
337
|
-
Flipper.configuration = nil
|
|
338
|
-
end
|
|
339
|
-
|
|
340
|
-
before(:each) do
|
|
341
|
-
ActiveRecord::Base.establish_connection(config)
|
|
342
|
-
end
|
|
343
|
-
|
|
344
|
-
after(:each) do
|
|
345
|
-
ActiveRecord::Base.connection.close
|
|
346
|
-
end
|
|
347
|
-
|
|
348
|
-
after(:all) do
|
|
349
|
-
silence { ActiveRecord::Tasks::DatabaseTasks.drop(config) } unless $skip
|
|
350
|
-
end
|
|
351
|
-
|
|
352
|
-
it "does not raise an error" do
|
|
353
|
-
Flipper.configuration = nil
|
|
354
|
-
Flipper.instance = nil
|
|
355
|
-
|
|
356
|
-
silence do
|
|
357
|
-
expect {
|
|
358
|
-
load 'flipper/adapters/active_record.rb'
|
|
359
|
-
Flipper::Adapters::ActiveRecord.new
|
|
360
|
-
}.not_to raise_error
|
|
361
|
-
end
|
|
362
|
-
end
|
|
363
|
-
end
|
|
364
|
-
end
|
|
365
|
-
end
|
|
366
|
-
|
|
367
|
-
def active_connections?
|
|
368
|
-
method = ActiveRecord::Base.connection_handler.method(:active_connections?)
|
|
369
|
-
method.arity == 0 ? method.call : method.call(:all)
|
|
370
|
-
end
|
|
371
|
-
|
|
372
|
-
def clear_active_connections!
|
|
373
|
-
method = ActiveRecord::Base.connection_handler.method(:clear_active_connections!)
|
|
374
|
-
method.arity == 0 ? method.call : method.call(:all)
|
|
375
|
-
end
|
|
376
|
-
|
|
377
|
-
def clear_all_connections!
|
|
378
|
-
method = ActiveRecord::Base.connection_handler.method(:clear_all_connections!)
|
|
379
|
-
method.arity == 0 ? method.call : method.call(:all)
|
|
380
|
-
end
|
|
381
|
-
end
|
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
require 'test_helper'
|
|
2
|
-
require 'flipper/adapters/active_record'
|
|
3
|
-
|
|
4
|
-
# Turn off migration logging for specs
|
|
5
|
-
ActiveRecord::Migration.verbose = false
|
|
6
|
-
|
|
7
|
-
class ActiveRecordTest < MiniTest::Test
|
|
8
|
-
prepend Flipper::Test::SharedAdapterTests
|
|
9
|
-
|
|
10
|
-
ActiveRecord::Base.establish_connection(adapter: 'sqlite3',
|
|
11
|
-
database: ':memory:')
|
|
12
|
-
|
|
13
|
-
def setup
|
|
14
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
15
|
-
CREATE TABLE flipper_features (
|
|
16
|
-
id integer PRIMARY KEY,
|
|
17
|
-
key text NOT NULL UNIQUE,
|
|
18
|
-
created_at datetime NOT NULL,
|
|
19
|
-
updated_at datetime NOT NULL
|
|
20
|
-
)
|
|
21
|
-
SQL
|
|
22
|
-
|
|
23
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
24
|
-
CREATE TABLE flipper_gates (
|
|
25
|
-
id integer PRIMARY KEY,
|
|
26
|
-
feature_key text NOT NULL,
|
|
27
|
-
key text NOT NULL,
|
|
28
|
-
value text DEFAULT NULL,
|
|
29
|
-
created_at datetime NOT NULL,
|
|
30
|
-
updated_at datetime NOT NULL
|
|
31
|
-
)
|
|
32
|
-
SQL
|
|
33
|
-
|
|
34
|
-
ActiveRecord::Base.connection.execute <<-SQL
|
|
35
|
-
CREATE UNIQUE INDEX index_gates_on_keys_and_value on flipper_gates (feature_key, key, value)
|
|
36
|
-
SQL
|
|
37
|
-
|
|
38
|
-
@adapter = Flipper::Adapters::ActiveRecord.new
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
def teardown
|
|
42
|
-
ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_features`")
|
|
43
|
-
ActiveRecord::Base.connection.execute("DROP table IF EXISTS `flipper_gates`")
|
|
44
|
-
end
|
|
45
|
-
|
|
46
|
-
def test_models_honor_table_name_prefixes_and_suffixes
|
|
47
|
-
ActiveRecord::Base.table_name_prefix = :foo_
|
|
48
|
-
ActiveRecord::Base.table_name_suffix = :_bar
|
|
49
|
-
|
|
50
|
-
# Remove constants so they get redefined with new prefix/suffix
|
|
51
|
-
Flipper::Adapters::ActiveRecord.send(:remove_const, :Model) if Flipper::Adapters::ActiveRecord.const_defined?(:Model)
|
|
52
|
-
Flipper::Adapters::ActiveRecord.send(:remove_const, :Feature) if Flipper::Adapters::ActiveRecord.const_defined?(:Feature)
|
|
53
|
-
Flipper::Adapters::ActiveRecord.send(:remove_const, :Gate) if Flipper::Adapters::ActiveRecord.const_defined?(:Gate)
|
|
54
|
-
Flipper::Adapters.send(:remove_const, :ActiveRecord)
|
|
55
|
-
|
|
56
|
-
load("flipper/adapters/active_record.rb")
|
|
57
|
-
load("flipper/adapters/active_record/model.rb")
|
|
58
|
-
load("flipper/adapters/active_record/feature.rb")
|
|
59
|
-
load("flipper/adapters/active_record/gate.rb")
|
|
60
|
-
|
|
61
|
-
assert_equal "foo_flipper_features_bar", Flipper::Adapters::ActiveRecord::Feature.table_name
|
|
62
|
-
assert_equal "foo_flipper_gates_bar", Flipper::Adapters::ActiveRecord::Gate.table_name
|
|
63
|
-
|
|
64
|
-
ensure
|
|
65
|
-
ActiveRecord::Base.table_name_prefix = ""
|
|
66
|
-
ActiveRecord::Base.table_name_suffix = ""
|
|
67
|
-
|
|
68
|
-
# Remove constants so they get redefined with reset prefix/suffix
|
|
69
|
-
Flipper::Adapters::ActiveRecord.send(:remove_const, :Model) if Flipper::Adapters::ActiveRecord.const_defined?(:Model)
|
|
70
|
-
Flipper::Adapters::ActiveRecord.send(:remove_const, :Feature) if Flipper::Adapters::ActiveRecord.const_defined?(:Feature)
|
|
71
|
-
Flipper::Adapters::ActiveRecord.send(:remove_const, :Gate) if Flipper::Adapters::ActiveRecord.const_defined?(:Gate)
|
|
72
|
-
Flipper::Adapters.send(:remove_const, :ActiveRecord)
|
|
73
|
-
|
|
74
|
-
load("flipper/adapters/active_record.rb")
|
|
75
|
-
load("flipper/adapters/active_record/model.rb")
|
|
76
|
-
load("flipper/adapters/active_record/feature.rb")
|
|
77
|
-
load("flipper/adapters/active_record/gate.rb")
|
|
78
|
-
end
|
|
79
|
-
end
|
|
@@ -1,41 +0,0 @@
|
|
|
1
|
-
require 'helper'
|
|
2
|
-
require 'generators/flipper/active_record_generator'
|
|
3
|
-
|
|
4
|
-
class FlipperActiveRecordGeneratorTest < Rails::Generators::TestCase
|
|
5
|
-
tests Flipper::Generators::ActiveRecordGenerator
|
|
6
|
-
destination File.expand_path('../../../../tmp', __FILE__)
|
|
7
|
-
setup :prepare_destination
|
|
8
|
-
|
|
9
|
-
def test_generates_migration
|
|
10
|
-
run_generator
|
|
11
|
-
migration_version = if Rails::VERSION::MAJOR.to_i < 5
|
|
12
|
-
""
|
|
13
|
-
else
|
|
14
|
-
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
|
|
15
|
-
end
|
|
16
|
-
assert_migration 'db/migrate/create_flipper_tables.rb', <<~MIGRATION
|
|
17
|
-
class CreateFlipperTables < ActiveRecord::Migration#{migration_version}
|
|
18
|
-
def up
|
|
19
|
-
create_table :flipper_features do |t|
|
|
20
|
-
t.string :key, null: false
|
|
21
|
-
t.timestamps null: false
|
|
22
|
-
end
|
|
23
|
-
add_index :flipper_features, :key, unique: true
|
|
24
|
-
|
|
25
|
-
create_table :flipper_gates do |t|
|
|
26
|
-
t.string :feature_key, null: false
|
|
27
|
-
t.string :key, null: false
|
|
28
|
-
t.text :value
|
|
29
|
-
t.timestamps null: false
|
|
30
|
-
end
|
|
31
|
-
add_index :flipper_gates, [:feature_key, :key, :value], unique: true, length: { value: 255 }
|
|
32
|
-
end
|
|
33
|
-
|
|
34
|
-
def down
|
|
35
|
-
drop_table :flipper_gates
|
|
36
|
-
drop_table :flipper_features
|
|
37
|
-
end
|
|
38
|
-
end
|
|
39
|
-
MIGRATION
|
|
40
|
-
end
|
|
41
|
-
end
|