Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix double enable of actor/group unique error #313

Merged
merged 3 commits into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,3 +43,6 @@ RSpec/InstanceVariable:

Style/AccessorMethodName:
Enabled: false

Lint/HandleExceptions:
Enabled: false
41 changes: 24 additions & 17 deletions lib/flipper/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,24 +113,9 @@ def get_all
def enable(feature, gate, thing)
case gate.data_type
when :boolean, :integer
@gate_class.transaction do
@gate_class.where(
feature_key: feature.key,
key: gate.key
).delete_all

@gate_class.create! do |g|
g.feature_key = feature.key
g.key = gate.key
g.value = thing.value.to_s
end
end
enable_single(feature, gate, thing)
when :set
@gate_class.create! do |g|
g.feature_key = feature.key
g.key = gate.key
g.value = thing.value.to_s
end
enable_multi(feature, gate, thing)
else
unsupported_data_type gate.data_type
end
Expand Down Expand Up @@ -178,6 +163,28 @@ def unsupported_data_type(data_type)

private

def enable_single(feature, gate, thing)
@gate_class.transaction do
@gate_class.where(feature_key: feature.key, key: gate.key).delete_all
@gate_class.create! do |g|
g.feature_key = feature.key
g.key = gate.key
g.value = thing.value.to_s
end
end
end

def enable_multi(feature, gate, thing)
@gate_class.create! do |g|
g.feature_key = feature.key
g.key = gate.key
g.value = thing.value.to_s
end
rescue ::ActiveRecord::RecordNotUnique
rescue ::ActiveRecord::StatementInvalid => error
raise unless error.message =~ /unique/i
end

def result_for_feature(feature, db_gates)
db_gates ||= []
result = {}
Expand Down
6 changes: 4 additions & 2 deletions lib/flipper/adapters/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ def enable(feature, gate, thing)
key: gate.key.to_s,
}
@gate_class.where(args).delete

@gate_class.create(gate_attrs(feature, gate, thing))
end
when :set
@gate_class.create(gate_attrs(feature, gate, thing))
begin
@gate_class.create(gate_attrs(feature, gate, thing))
rescue ::Sequel::UniqueConstraintViolation
end
else
unsupported_data_type gate.data_type
end
Expand Down
11 changes: 11 additions & 0 deletions lib/flipper/spec/shared_adapter_specs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,4 +258,15 @@
expect(stats).to eq(subject.default_config.merge(boolean: 'true'))
expect(search).to eq(subject.default_config)
end

it 'can double enable an actor without error' do
actor = Flipper::Actor.new('Flipper::Actor;22')
expect(subject.enable(feature, actor_gate, flipper.actor(actor))).to eq(true)
expect(subject.enable(feature, actor_gate, flipper.actor(actor))).to eq(true)
end

it 'can double enable a group without error' do
expect(subject.enable(feature, group_gate, flipper.group(:admins))).to eq(true)
expect(subject.enable(feature, group_gate, flipper.group(:admins))).to eq(true)
end
end
11 changes: 11 additions & 0 deletions lib/flipper/test/shared_adapter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,17 @@ def test_can_get_all_features
assert_equal @adapter.default_config.merge(boolean: 'true'), stats
assert_equal @adapter.default_config, search
end

def test_can_double_enable_an_actor_without_error
actor = Flipper::Actor.new('Flipper::Actor;22')
assert_equal true, @adapter.enable(@feature, @actor_gate, @flipper.actor(actor))
assert_equal true, @adapter.enable(@feature, @actor_gate, @flipper.actor(actor))
end

def test_can_double_enable_a_group_without_error
assert_equal true, @adapter.enable(@feature, @group_gate, @flipper.group(:admins))
assert_equal true, @adapter.enable(@feature, @group_gate, @flipper.group(:admins))
end
end
end
end