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

Allow navigation on history records without an at_value #33

Merged
merged 3 commits into from
Jan 11, 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
8 changes: 6 additions & 2 deletions lib/temporal_tables/association_extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ def target_scope
# Using responds_to? results in an infinite loop stack overflow.
if @owner.public_methods.include?(:at_value)
# If this is a history record but no at time was given,
# assume the record's effective to date
super.at(@owner.at_value || @owner.eff_to)
# assume the record's effective to date minus 1 microsecond
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a brief explanation here why the microsecond is necessary?

# The logic here is to provide the association's history record at the end of
# the parent record's effective period. Since effective ranges are exclusive of
# the eff_to value, and the eff_* columns are datetime types with a precision of microseconds,
# 1 microsecond before eff_to is the end of the inclusive boundary to accomplish this.
super.at(@owner.at_value || (@owner.eff_to - TemporalTables::ONE_MICROSECOND))
else
super
end
Expand Down
1 change: 1 addition & 0 deletions lib/temporal_tables/constants.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

module TemporalTables
END_OF_TIME = '9999-12-31'
ONE_MICROSECOND = 0.000001.seconds
end
20 changes: 20 additions & 0 deletions spec/basic_history_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,26 @@
end
end
end

describe 'when removing a creature' do
let!(:wart) { Wart.create person: emily, hairiness: 3 }

before do
emily.destroy!
sleep 0.1
end

it 'destroys associated warts and we remember the historical association' do
expect(emily).to be_destroyed
expect { wart.reload }.to raise_error(ActiveRecord::RecordNotFound) # as it belonged to emily

wart_h = wart.history.last # the last version of the wart
expect(wart_h).to be_present

emily_h = wart_h.person
expect(emily_h).to be_present # we should be able to tell what person our wart belonged to
end
end
end

describe Bird do
Expand Down
2 changes: 1 addition & 1 deletion spec/internal/app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

class Person < ActiveRecord::Base
belongs_to :coven
has_many :warts
has_many :warts, dependent: :destroy
has_many :flying_machines
end
2 changes: 1 addition & 1 deletion temporal_tables.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Gem::Specification.new do |gem|
gem.required_ruby_version = '>= 3.0.0'
gem.metadata = { 'rubygems_mfa_required' => 'true' }

gem.add_dependency 'rails', '>= 6.0', '< 7.2'
gem.add_dependency 'rails', '>= 6.1', '< 7.2'
gem.add_development_dependency 'combustion', '~> 1'
gem.add_development_dependency 'database_cleaner'
gem.add_development_dependency 'gemika', '~> 0.8'
Expand Down
Loading