Skip to content

Commit

Permalink
Clear gate values on enable
Browse files Browse the repository at this point in the history
We already did this on disable so it makes sense to do so on enable as well. Technically the only way to revert enable is to disable which would clear gates so there was no harm, but why leave extras hanging around.
  • Loading branch information
jnunemaker committed Jan 4, 2020
1 parent ad442d1 commit 40e57f3
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 8 deletions.
8 changes: 6 additions & 2 deletions lib/flipper/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ def get_all
# Returns true.
def enable(feature, gate, thing)
case gate.data_type
when :boolean, :integer
when :boolean
set(feature, gate, thing, clear: true)
when :integer
set(feature, gate, thing)
when :set
enable_multi(feature, gate, thing)
Expand Down Expand Up @@ -165,8 +167,10 @@ def unsupported_data_type(data_type)

private

def set(feature, gate, thing)
def set(feature, gate, thing, options = {})
clear_feature = options.fetch(:clear, false)
@gate_class.transaction do
clear(feature) if clear_feature
@gate_class.where(feature_key: feature.key, key: gate.key).destroy_all
@gate_class.create! do |g|
g.feature_key = feature.key
Expand Down
5 changes: 4 additions & 1 deletion lib/flipper/adapters/memory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,10 @@ def get_all
# Public
def enable(feature, gate, thing)
case gate.data_type
when :boolean, :integer
when :boolean
clear(feature)
write key(feature, gate), thing.value.to_s
when :integer
write key(feature, gate), thing.value.to_s
when :set
set_add key(feature, gate), thing.value.to_s
Expand Down
7 changes: 6 additions & 1 deletion lib/flipper/adapters/moneta.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,12 @@ def get(feature)
# Returns true.
def enable(feature, gate, thing)
case gate.data_type
when :boolean, :integer
when :boolean
clear(feature)
result = get(feature)
result[gate.key] = thing.value.to_s
moneta[key(feature.key)] = result
when :integer
result = get(feature)
result[gate.key] = thing.value.to_s
moneta[key(feature.key)] = result
Expand Down
7 changes: 6 additions & 1 deletion lib/flipper/adapters/mongo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ def get_all
# Returns true.
def enable(feature, gate, thing)
case gate.data_type
when :boolean, :integer
when :boolean
clear(feature)
update feature.key, '$set' => {
gate.key.to_s => thing.value.to_s,
}
when :integer
update feature.key, '$set' => {
gate.key.to_s => thing.value.to_s,
}
Expand Down
5 changes: 4 additions & 1 deletion lib/flipper/adapters/pstore.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,10 @@ def get_all
def enable(feature, gate, thing)
@store.transaction do
case gate.data_type
when :boolean, :integer
when :boolean
clear_gates(feature)
write key(feature, gate), thing.value.to_s
when :integer
write key(feature, gate), thing.value.to_s
when :set
set_add key(feature, gate), thing.value.to_s
Expand Down
5 changes: 4 additions & 1 deletion lib/flipper/adapters/redis.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,10 @@ def get_all
# Returns true.
def enable(feature, gate, thing)
case gate.data_type
when :boolean, :integer
when :boolean
clear(feature)
@client.hset feature.key, gate.key, thing.value.to_s
when :integer
@client.hset feature.key, gate.key, thing.value.to_s
when :set
@client.hset feature.key, to_field(gate, thing), 1
Expand Down
12 changes: 11 additions & 1 deletion lib/flipper/adapters/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,17 @@ def get_all
# Returns true.
def enable(feature, gate, thing)
case gate.data_type
when :boolean, :integer
when :boolean
@gate_class.db.transaction do
clear(feature)
args = {
feature_key: feature.key,
key: gate.key.to_s,
}
@gate_class.where(args).delete
@gate_class.create(gate_attrs(feature, gate, thing))
end
when :integer
@gate_class.db.transaction do
args = {
feature_key: feature.key,
Expand Down
10 changes: 10 additions & 0 deletions lib/flipper/spec/shared_adapter_specs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,14 @@
expect(subject.features).to eq(Set.new)
expect(subject.get_all).to eq({})
end

it 'clears other gate values on enable' do
actor = Flipper::Actor.new('Flipper::Actor;22')
subject.enable(feature, actors_gate, flipper.actors(25))
subject.enable(feature, time_gate, flipper.time(25))
subject.enable(feature, group_gate, flipper.group(:admins))
subject.enable(feature, actor_gate, flipper.actor(actor))
subject.enable(feature, boolean_gate, flipper.boolean(true))
expect(subject.get(feature)).to eq(subject.default_config.merge(boolean: "true"))
end
end
10 changes: 10 additions & 0 deletions lib/flipper/test/shared_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,16 @@ def test_can_get_all_features_when_there_are_none
assert_equal Set.new, @adapter.features
assert_equal expected, @adapter.get_all
end

def test_clears_other_gate_values_on_enable
actor = Flipper::Actor.new('Flipper::Actor;22')
assert_equal true, @adapter.enable(@feature, @actors_gate, @flipper.actors(25))
assert_equal true, @adapter.enable(@feature, @time_gate, @flipper.time(25))
assert_equal true, @adapter.enable(@feature, @group_gate, @flipper.group(:admins))
assert_equal true, @adapter.enable(@feature, @actor_gate, @flipper.actor(actor))
assert_equal true, @adapter.enable(@feature, @boolean_gate, @flipper.boolean(true))
assert_equal @adapter.default_config.merge(boolean: "true"), @adapter.get(@feature)
end
end
end
end

0 comments on commit 40e57f3

Please sign in to comment.