Skip to content

Commit

Permalink
Use requires_new with transaction to avoid poisoning
Browse files Browse the repository at this point in the history
  • Loading branch information
jnunemaker committed Sep 3, 2024
1 parent 8695db3 commit 1c9e0a2
Showing 1 changed file with 7 additions and 12 deletions.
19 changes: 7 additions & 12 deletions lib/flipper/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,20 +247,15 @@ def delete(feature, gate)
def enable_multi(feature, gate, thing)
with_connection(@gate_class) do |connection|
begin
connection.transaction do
savepoint_name = "flipper_savepoint_#{SecureRandom.hex(8)}"
connection.execute("SAVEPOINT #{savepoint_name}")
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
# already added so no need move on with life
connection.execute("ROLLBACK TO SAVEPOINT #{savepoint_name}")
connection.transaction(requires_new: true) do
@gate_class.create! do |g|
g.feature_key = feature.key
g.key = gate.key
g.value = thing.value.to_s
end
end
rescue ::ActiveRecord::RecordNotUnique
# already added so no need move on with life
end
end

Expand Down

0 comments on commit 1c9e0a2

Please sign in to comment.