From 43162b663d12af7c21e7ccbc5c09e5a1fa265b37 Mon Sep 17 00:00:00 2001 From: Earlopain <14981592+Earlopain@users.noreply.github.com> Date: Thu, 28 Nov 2024 18:03:30 +0100 Subject: [PATCH] Fix compatibility with `rails-head` when duplicated advisory lockable column (#1553) * Fix compatibility with `rails-head` The primary key and column may be identical. Current rails head will select this column twice, which confuses postgres * Use if statement to prevent duplicate instead of array+compact --------- Co-authored-by: Ben Sheldon [he/him] --- app/models/concerns/good_job/advisory_lockable.rb | 10 +++++++++- .../models/concerns/good_job/advisory_lockable_spec.rb | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/app/models/concerns/good_job/advisory_lockable.rb b/app/models/concerns/good_job/advisory_lockable.rb index e40ef0c8..7c714c93 100644 --- a/app/models/concerns/good_job/advisory_lockable.rb +++ b/app/models/concerns/good_job/advisory_lockable.rb @@ -41,8 +41,16 @@ module AdvisoryLockable scope :advisory_lock, (lambda do |column: _advisory_lockable_column, function: advisory_lockable_function, select_limit: nil| original_query = self + primary_key_for_select = primary_key.to_sym + column_for_select = column.to_sym + cte_table = Arel::Table.new(:rows) - cte_query = original_query.select(primary_key, column).except(:limit) + cte_query = original_query.except(:limit) + cte_query = if primary_key_for_select == column_for_select + cte_query.select(primary_key_for_select) + else + cte_query.select(primary_key_for_select, column_for_select) + end cte_query = cte_query.limit(select_limit) if select_limit cte_type = supports_cte_materialization_specifiers? ? :MATERIALIZED : :"" composed_cte = Arel::Nodes::As.new(cte_table, Arel::Nodes::UnaryOperation.new(cte_type, cte_query.arel)) diff --git a/spec/app/models/concerns/good_job/advisory_lockable_spec.rb b/spec/app/models/concerns/good_job/advisory_lockable_spec.rb index 15d019e8..c41565bc 100644 --- a/spec/app/models/concerns/good_job/advisory_lockable_spec.rb +++ b/spec/app/models/concerns/good_job/advisory_lockable_spec.rb @@ -51,7 +51,7 @@ FROM "good_jobs" WHERE "good_jobs"."id" IN ( WITH "rows" AS #{'MATERIALIZED' if model_class.supports_cte_materialization_specifiers?} ( - SELECT "good_jobs"."id", "good_jobs"."id" + SELECT "good_jobs"."id" FROM "good_jobs" WHERE "good_jobs"."priority" = 99 ORDER BY "good_jobs"."priority" DESC