Shopify / job-iteration

Makes your background jobs interruptible and resumable by design.
https://www.rubydoc.info/gems/job-iteration
MIT License
1.15k stars 45 forks source link

Composite Primary keys not working #511

Open davidwparker opened 1 month ago

davidwparker commented 1 month ago

Hello.

I have the following model:

class Model < ApplicationRecord
  self.primary_key = [:sub_id, :id]
....

When I call find_each on it, I get results like this:

SELECT "models".* FROM "models" WHERE "model"."sub_id" = $1 AND (("models"."sub_id" > 'xxxxx-yyy-...') OR ("models"."sub_id" = 'xxxxx-yyy-...' AND "models"."id" > 'aaaaa-bb....')) ORDER BY "models"."sub_id" ASC, "models"."id" ASC LIMIT $4

But when calling with this library, I'd expect the same, but am getting different:

# frozen_string_literal: true

class MyJob < ApplicationIterationJob
  BATCH_SIZE = 2

  def build_enumerator(sub_id, cursor:)
    enumerator_builder.active_record_on_batches(
      Model.where(sub_id: sub_id),
      cursor: cursor,
      batch_size: BATCH_SIZE
    )
  end

  def each_iteration(models, _sub_id)
...
  end
end

results in

SELECT "models".* FROM "models" WHERE "models"."sub_id" = $1 AND (models.sub_id > 'xxxxx-yyy-....' OR (models.sub_id = 'xxxxx-yyy-....' AND (models.id > 'xxxxx-yyy-....','aaaaa-bb.....'))) ORDER BY models.sub_id,models.id LIMIT $4 

ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR:  argument of AND must be type boolean, not type record
LINE 1: ...y_id = 'xxxxx-yyy....' AND (company_f...

And it's due to the fact that (models.id > 'xxxxx-yyy-....','aaaaa-bb.....') should just be using aaaaa-bb... instead of the whole thing.

I've checked the docs, and previous tickets, but not seeing this come up.

Relevant PR https://github.com/Shopify/job-iteration/pull/365