|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'support/time_machine/structure' |
| 5 | + |
| 6 | +RSpec.describe ChronoModel::TimeMachine do |
| 7 | + include ChronoTest::TimeMachine::Helpers |
| 8 | + |
| 9 | + after do |
| 10 | + Overlapper::History.delete_all |
| 11 | + Overlapper.delete_all |
| 12 | + OverlapBro::History.delete_all |
| 13 | + OverlapBro.delete_all |
| 14 | + end |
| 15 | + |
| 16 | + describe 'records created in the same transaction' do |
| 17 | + it 'are all visible in as_of() queries using the first record creation time' do |
| 18 | + first_record = nil |
| 19 | + second_record = nil |
| 20 | + third_record = nil |
| 21 | + |
| 22 | + Overlapper.transaction do |
| 23 | + first_record = Overlapper.create!(name: 'first') |
| 24 | + second_record = Overlapper.create!(name: 'second') |
| 25 | + third_record = Overlapper.create!(name: 'third') |
| 26 | + end |
| 27 | + |
| 28 | + Overlapper.transaction do |
| 29 | + Overlapper.create!(name: 'second transaction record') |
| 30 | + end |
| 31 | + |
| 32 | + # Get the validity start time of the first record from history |
| 33 | + first_record_validity_start = first_record.history.last.validity.begin |
| 34 | + |
| 35 | + # All records created in the same transaction should be visible |
| 36 | + # when querying as_of() with the first record's creation timestamp |
| 37 | + records_at_first_timestamp = Overlapper.as_of(first_record_validity_start) |
| 38 | + expect(records_at_first_timestamp.map(&:id)).to contain_exactly(first_record.id, second_record.id, third_record.id) |
| 39 | + end |
| 40 | + |
| 41 | + it 'allows querying associated records created in the same transaction' do |
| 42 | + overlap = nil |
| 43 | + |
| 44 | + Overlapper.transaction do |
| 45 | + bro = OverlapBro.create!(name: 'bro record') |
| 46 | + overlap = Overlapper.create!(name: 'child', overlap_bro: bro) |
| 47 | + end |
| 48 | + |
| 49 | + overlap_validity_start = overlap.history.last.validity.begin |
| 50 | + overlap_at_creation = Overlapper.as_of(overlap_validity_start).find(overlap.id) |
| 51 | + # Child record should be visible through the association |
| 52 | + expect(overlap_at_creation.overlap_bro.name).to eq('bro record') |
| 53 | + end |
| 54 | + |
| 55 | + it 'maintains consistent timestamps for records created together' do |
| 56 | + records = nil |
| 57 | + |
| 58 | + Overlapper.transaction do |
| 59 | + records = Array.new(5) { |i| Overlapper.create!(name: "record-#{i}") } |
| 60 | + end |
| 61 | + |
| 62 | + # All records should have the same validity start time (transaction time) |
| 63 | + validity_starts = records.map { |r| r.history.last.validity.begin } |
| 64 | + |
| 65 | + expect(validity_starts.uniq.size).to eq(1), |
| 66 | + "Expected all records to have the same validity start time, but got: #{validity_starts.inspect}" |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + describe 'records updated in the same transaction' do |
| 71 | + # This test reproduces the issue where clock_timestamp() in UPDATE triggers |
| 72 | + # causes records updated within the same transaction to have different timestamps, |
| 73 | + # making them invisible in as_of() queries. |
| 74 | + # |
| 75 | + # Scenario: |
| 76 | + # 1. Create an "overlap_bro" record |
| 77 | + # 2. Create associated "overlappers" that reference this version |
| 78 | + # 3. Update the overlappers within the same transaction |
| 79 | + # 4. Query as_of(overlap_bro.created_at) should show all overlappers |
| 80 | + it 'updated records are visible in as_of() queries using the transaction start time' do |
| 81 | + bro = nil |
| 82 | + |
| 83 | + OverlapBro.transaction do |
| 84 | + bro = OverlapBro.create!(name: 'bro_v1') |
| 85 | + 3.times do |i| |
| 86 | + overlap = Overlapper.create!(name: "overlapper_#{i}", overlap_bro: bro) |
| 87 | + overlap.update!(name: "overlapper_#{i}_updated") |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + bro_created_at = bro.history.last.validity.begin |
| 92 | + |
| 93 | + # Query all overlappers as of the bro's creation time |
| 94 | + # With clock_timestamp() in UPDATE trigger, updated overlappers would have |
| 95 | + # a later timestamp and would NOT be visible here |
| 96 | + overlappers_at_bro_time = Overlapper.as_of(bro_created_at).where(overlap_bro_id: bro.id) |
| 97 | + |
| 98 | + expect(overlappers_at_bro_time.count).to eq(3), |
| 99 | + "Expected 3 overlappers visible at bro creation time, got #{overlappers_at_bro_time.count}" |
| 100 | + |
| 101 | + expect(overlappers_at_bro_time.map(&:name)).to contain_exactly( |
| 102 | + 'overlapper_0_updated', |
| 103 | + 'overlapper_1_updated', |
| 104 | + 'overlapper_2_updated' |
| 105 | + ) |
| 106 | + end |
| 107 | + |
| 108 | + it 'multiple updates in same transaction maintain consistent timestamp' do |
| 109 | + overlap = nil |
| 110 | + |
| 111 | + Overlapper.transaction do |
| 112 | + overlap = Overlapper.create!(name: 'original') |
| 113 | + overlap.update!(name: 'update1') |
| 114 | + overlap.update!(name: 'update2') |
| 115 | + overlap.update!(name: 'final') |
| 116 | + end |
| 117 | + |
| 118 | + # All history entries created in this transaction should share the same timestamp |
| 119 | + # (due to squashing or consistent now() usage) |
| 120 | + history_entries = overlap.history.order(:hid) |
| 121 | + |
| 122 | + # The final state should be visible at the creation timestamp |
| 123 | + creation_time = history_entries.first.validity.begin |
| 124 | + overlap_at_creation = Overlapper.as_of(creation_time).find(overlap.id) |
| 125 | + expect(overlap_at_creation.name).to eq('final') |
| 126 | + end |
| 127 | + end |
| 128 | + |
| 129 | + describe 'records updated after initial creation' do |
| 130 | + it 'handles updates without timestamp conflicts' do |
| 131 | + overlap = Overlapper.create!(name: 'original') |
| 132 | + |
| 133 | + # Sleep a tiny bit to ensure different transaction time |
| 134 | + sleep(0.01) |
| 135 | + |
| 136 | + overlap.update!(name: 'updated') |
| 137 | + |
| 138 | + # The update should succeed and create a valid history entry |
| 139 | + overlap.reload |
| 140 | + expect(overlap.name).to eq('updated') |
| 141 | + |
| 142 | + # History should have entries with non-overlapping validity ranges |
| 143 | + history_entries = overlap.history.order(:hid) |
| 144 | + expect(history_entries.size).to eq 2 |
| 145 | + end |
| 146 | + end |
| 147 | +end |
0 commit comments