Exclude record valid range end in uniqueness validation #184
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
ActiveRecord::Bitemporal::Uniqueness#scope_relation
validates that a record is unique over the valid period of the record being added. For example, ifvalid_from
is 2024-03-01 andvalid_datetime
is 2024-06-01, the period to be validated will be from 2024-06-01 to 9999-12-31.On the other hand, if the
valid_datetime
is earlier than thevalid_from
(e.g. 2024-01-01), the following period is validated:In this case the range is calculated as follows:
activerecord-bitemporal/lib/activerecord-bitemporal/bitemporal.rb
Lines 595 to 602 in 322fa8a
In principle this should be the same as the actual update logic, but it's not an exact match. The corresponding logic is as follows:
activerecord-bitemporal/lib/activerecord-bitemporal/bitemporal.rb
Lines 490 to 492 in 322fa8a
activerecord-bitemporal/lib/activerecord-bitemporal/bitemporal.rb
Lines 533 to 536 in 322fa8a
Since the
find_at_time
adds theWHERE valid_from <= valid_datetime AND valid_to > valid_datetime
, it is correct to use...
rather than..
to see whethervalid_datetime
is included in the valid period.Excluding the end of range would change the validated range when the
valid_datetime
is equal tovalid_to
, sogt
is changed togteq
.This change has little to no user impact, it's just a refactoring.