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

Fix batch methods for historical models #324

Merged
merged 1 commit into from
Aug 13, 2024
Merged
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
36 changes: 35 additions & 1 deletion lib/chrono_model/patches/batches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,44 @@

module ChronoModel
module Patches
# Overrides the default batch methods for historical models
#
# In the default implementation, `cursor` defaults to `primary_key`, which is 'id'.
# However, historical models need to use 'hid' instead of 'id'.
#
# This patch addresses an issue where `with_hid_pkey` is called after the cursor
# is already set, potentially leading to incorrect behavior.
#
# Notes:
# - `find_each` and `find_in_batches` internally utilize `in_batches`.
# However, in the upcoming Rails 8.0, this implementation will be
# insufficient due to a new conditional branch using `enum_for`.
# - This approach prevents specifying 'id' as a cursor for historical models.
# If 'id' is needed, it must be handled separately.
#
# See: ifad/chronomodel#321 for more context
module Batches
def in_batches(**)
def find_each(**options)
return super unless try(:history?)

options[:cursor] = 'hid' if options[:cursor] == 'id'

with_hid_pkey { super }
end

def find_in_batches(**options)
return super unless try(:history?)

options[:cursor] = 'hid' if options[:cursor] == 'id'

with_hid_pkey { super }
end

def in_batches(**options)
return super unless try(:history?)

options[:cursor] = 'hid' if options[:cursor] == 'id'

with_hid_pkey { super }
end
end
Expand Down
Loading