Skip to content

Commit

Permalink
Rescue unique errors in AR and Sequel when setting value
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Jul 27, 2021
1 parent 4722a8c commit 87f5a98
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 9 additions & 4 deletions lib/flipper/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,15 @@ def set(feature, gate, thing, options = {})
@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
g.key = gate.key
g.value = thing.value.to_s
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

Expand Down
6 changes: 5 additions & 1 deletion lib/flipper/adapters/sequel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,11 @@ def set(feature, gate, thing, options = {})
@gate_class.db.transaction do
clear(feature) if clear_feature
@gate_class.where(args).delete
@gate_class.create(gate_attrs(feature, gate, thing))

begin
@gate_class.create(gate_attrs(feature, gate, thing))
rescue ::Sequel::UniqueConstraintViolation
end
end
end

Expand Down

0 comments on commit 87f5a98

Please sign in to comment.