Skip to content

Commit

Permalink
Add save points to fix it
Browse files Browse the repository at this point in the history
Unsure if this is right approach but it does work. Oddly only postgres was complaining about the poisoned transaction.
  • Loading branch information
jnunemaker committed Aug 27, 2024
1 parent e58b257 commit 33153db
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions lib/flipper/adapters/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'set'
require 'securerandom'
require 'flipper'
require 'active_record'

Expand Down Expand Up @@ -244,17 +245,26 @@ def delete(feature, gate)
end

def enable_multi(feature, gate, thing)
with_connection(@gate_class) do
@gate_class.create! do |g|
g.feature_key = feature.key
g.key = gate.key
g.value = thing.value.to_s
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}")
end
end
end
end

nil
rescue ::ActiveRecord::RecordNotUnique
# already added so no need move on with life
end

def result_for_gates(feature, gates)
Expand Down

0 comments on commit 33153db

Please sign in to comment.