From 8f965ad99e6706e48a11eb45917f4b4cb94d4d75 Mon Sep 17 00:00:00 2001 From: Malaber Date: Thu, 30 Apr 2026 12:08:00 +0200 Subject: [PATCH] fix: preserve timer broadcast after logging --- app/services/tournament_transaction_logger.rb | 4 ++- .../tournament_transaction_logger_spec.rb | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/app/services/tournament_transaction_logger.rb b/app/services/tournament_transaction_logger.rb index 4111c6a..90b0255 100644 --- a/app/services/tournament_transaction_logger.rb +++ b/app/services/tournament_transaction_logger.rb @@ -9,7 +9,9 @@ class TournamentTransactionLogger raise 'Tournament transaction log writes must happen inside a transaction' end - tournament.lock! + # Lock a fresh row instance so callers keep saved_change_to_* state for + # after_commit callbacks on the object they just updated. + Tournament.lock.find(tournament.id) end def record!(tournament:, user:, action:, message:, metadata: {}) diff --git a/spec/services/tournament_transaction_logger_spec.rb b/spec/services/tournament_transaction_logger_spec.rb index 402a71f..78e2851 100644 --- a/spec/services/tournament_transaction_logger_spec.rb +++ b/spec/services/tournament_transaction_logger_spec.rb @@ -27,5 +27,32 @@ RSpec.describe TournamentTransactionLogger do expect(second.sequence).to eq(2) expect(tournament.transaction_log_entries.order(:sequence).pluck(:message)).to eq(%w[first second]) end + + it 'does not clear pending after_commit change tracking on the caller tournament' do + tournament = create(:tournament) + user = tournament.owner + old_timer = { timestamp: nil, timer_mode: nil, timer_reason: nil, timer_reason_text: nil } + + Tournament.transaction do + tournament.update!( + timestamp: 2.minutes.from_now, + timer_mode: 'countdown', + timer_reason: 'tournament_start' + ) + new_timer = { timestamp: tournament.timestamp.iso8601, timer_mode: 'countdown', + timer_reason: 'tournament_start', timer_reason_text: nil } + + expect(tournament.saved_change_to_timestamp?).to be(true) + + described_class.record_timer_changed!( + tournament: tournament, + user: user, + old_timer: old_timer, + new_timer: new_timer + ) + + expect(tournament.saved_change_to_timestamp?).to be(true) + end + end end end