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

Handle composite primary keys in scopes #537

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 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
37 changes: 31 additions & 6 deletions lib/pg_search/scope_options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,15 @@ def increment_counter
delegate :connection, :quoted_table_name, to: :model

def subquery
model
.unscoped
.select("#{primary_key} AS pg_search_id")
.select("#{rank} AS rank")
query = model.unscoped

query = if composite_primary_key?
query.select(model.primary_key.map.with_index(1) { |part, index| "#{quoted_column_name(part)} AS pg_search_id_#{index}" })
else
query.select("#{primary_key} AS pg_search_id")
end

query.select("#{rank} AS rank")
.joins(subquery_join)
.where(conditions)
.limit(nil)
Expand All @@ -101,12 +106,17 @@ def order_within_rank
end

def primary_key
"#{quoted_table_name}.#{connection.quote_column_name(model.primary_key)}"
if composite_primary_key?
model.primary_key.map { |part| quoted_column_name(part) }.join(",")
else
quoted_column_name(model.primary_key)
end
end

def subquery_join
if config.associations.any?
config.associations.map do |association|
# TODO: handle composite primary keys in associations
association.join(primary_key)
end.join(" ")
end
Expand Down Expand Up @@ -142,7 +152,14 @@ def rank
end

def rank_join(rank_table_alias)
"INNER JOIN (#{subquery.to_sql}) AS #{rank_table_alias} ON #{primary_key} = #{rank_table_alias}.pg_search_id"
join_condition = if composite_primary_key?
model.primary_key.map.with_index(1) { |part, index| "#{quoted_column_name(part)} = #{rank_table_alias}.pg_search_id_#{index}" }
.join(" AND ")
else
"#{primary_key} = #{rank_table_alias}.pg_search_id"
end

"INNER JOIN (#{subquery.to_sql}) AS #{rank_table_alias} ON #{join_condition}"
end

def include_table_aliasing_for_rank(scope)
Expand All @@ -152,5 +169,13 @@ def include_table_aliasing_for_rank(scope)
new_scope.instance_eval { extend PgSearchRankTableAliasing }
end
end

def composite_primary_key?
model.primary_key.is_a?(Array)
end

def quoted_column_name(column)
"#{quoted_table_name}.#{connection.quote_column_name(column)}"
end
end
end
152 changes: 152 additions & 0 deletions spec/integration/composite_primary_key_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
# frozen_string_literal: true

require "spec_helper"

describe "composite_primary_key" do
context 'without relations' do
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it'd be good to call these "associations" instead of "relations" because that's the Rails term for them.

"Relation" has a different meaning in PostgreSQL.

https://guides.rubyonrails.org/association_basics.html

Copy link
Author

Choose a reason for hiding this comment

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

with_model :Parent do
table primary_key: [:first_name, :last_name] do |t|
t.string :first_name
t.string :last_name
t.string :hobby
end

model do
include PgSearch::Model
self.primary_key = [:first_name, :last_name]
pg_search_scope :search_hobby, against: :hobby
end
end

before { Parent.create!(id: ["first_name", "last_name"], hobby: "golf") }
let!(:record_1) { Parent.create!(id: ["first_name_2", "last_name_2"], hobby: "basketball") }

it "searches without any issues" do
expect(Parent.search_hobby("basketball")).to eq([record_1])
end
end

context "without composite_primary_key, searching against relation with a composite_primary_key" do
with_model :Parent do
table primary_key: [:first_name, :last_name] do |t|
t.string :first_name
t.string :last_name
t.string :hobby
end

model do
include PgSearch::Model
has_many :children
self.primary_key = [:first_name, :last_name]
end
end

with_model :Child do
table do |t|
t.string :parent_first_name
t.string :parent_last_name
end

model do
include PgSearch::Model
belongs_to :parent, foreign_key: [:parent_first_name, :parent_last_name]

pg_search_scope :search_parent_hobby, associated_against: {
Copy link
Collaborator

Choose a reason for hiding this comment

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

It's great that you are testing associated_against, good catch!

parent: [:hobby]
}
end
end

before do
parent = Parent.create!(id: ["first_name", "last_name"], hobby: "golf")
Child.create!(parent: parent)
end

let!(:record_1) do
parent = Parent.create!(id: ["first_name_2", "last_name_2"], hobby: "basketball")
Child.create!(parent: parent)
end

it "searches without any issues" do
expect(Child.search_parent_hobby("basketball")).to eq([record_1])
end
end

context "with composite_primary_key, searching against relation with a composite_primary_key" do
with_model :Parent do
table primary_key: [:first_name, :last_name] do |t|
t.string :first_name
t.string :last_name
t.string :hobby
end

model do
include PgSearch::Model
has_many :children, foreign_key: [:first_name, :last_name]
self.primary_key = [:first_name, :last_name]
end
end

with_model :Child do
table primary_key: [:first_name, :last_name] do |t|
t.string :hobby
t.string :first_name
t.string :last_name
t.string :parent_first_name
t.string :parent_last_name
end

model do
include PgSearch::Model
belongs_to :parent, foreign_key: [:parent_first_name, :parent_last_name]
has_many :siblings, through: :parent, source: :children

pg_search_scope :search_parent_hobby, associated_against: {
parent: [:hobby]
}

pg_search_scope :search_sibling_hobby, associated_against: {
siblings: [:hobby]
}

pg_search_scope :search_hobby, associated_against: {
parent: [:hobby],
siblings: [:hobby]
}
end
end

before do
parent = Parent.create!(id: ["first_name", "last_name"], hobby: "golf")
Child.create!(id: ["first_name", "last_name"], parent: parent)
end

it "searches direct relation without any issues" do
parent = Parent.create!(id: ["first_name_2", "last_name_2"], hobby: "basketball")
record_1 = Child.create!(id: ["first_name_2", "last_name_2"], parent: parent)
expect(Child.search_parent_hobby("basketball")).to eq([record_1])
end

it "searches through relation without any issues" do
parent = Parent.create!(id: ["first_name_2", "last_name_2"], hobby: "juggling")
Child.create!(id: ["first_name_2", "last_name_2"], parent: parent, hobby: "basketball")
record_1 = Child.create!(id: ["first_name_3", "last_name_3"], parent: parent, hobby: "studying")

expect(Child.search_sibling_hobby("basketball")).to eq([record_1])
end

it "searches through multiple relations without any issues" do
parent_1 = Parent.create!(id: ["first_name_2", "last_name_2"], hobby: "basketball")
record_1 = Child.create!(id: ["first_name_2", "last_name_2"], parent: parent_1, hobby: "studying") # match by parent

parent_2 = Parent.create!(id: ["first_name_3", "last_name_3"], hobby: "juggling")
record_2 = Child.create!(id: ["first_name_3", "last_name_3"], parent: parent_2, hobby: "bowling") # match by sibling
record_3 = Child.create!(id: ["first_name_4", "last_name_4"], parent: parent_2, hobby: "basketball") # match by self

parent_3 = Parent.create!(id: ["first_name_4", "last_name_4"], hobby: "golf")
Child.create!(id: ["first_name_5", "last_name_5"], parent: parent_3, hobby: "sleeping")

expect(Child.search_hobby("basketball")).to eq([record_1, record_2, record_3])
end
end
end
Loading