Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly reset thread variable at_time even on exception #44

Merged
merged 1 commit into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions lib/temporal_tables/relation_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,15 @@ def to_sql(*args)

def threadify_at
if at_value && !Thread.current[:at_time]
Thread.current[:at_time] = at_value
result = yield
Thread.current[:at_time] = nil
begin
Thread.current[:at_time] = at_value
yield
ensure
Thread.current[:at_time] = nil
end
else
result = yield
yield
end
result
end

def limited_ids_for(*args)
Expand Down
9 changes: 9 additions & 0 deletions spec/basic_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@
end
end

describe 'creating a join query' do
it 'correctly joins even after generating sql has failed before' do
expect { Person.history.at(100.years.ago).joins(:warts).where(x: proc {}).to_sql }.to raise_error(TypeError)

# it still fetches the history correctly
expect(Person.history.at(Time.current).joins(:warts)).to be_present
end
end

describe 'when preloading associations' do
let(:orig_emily) { emily.history.at(@init_time).preload(:warts).first }

Expand Down
Loading