-
Notifications
You must be signed in to change notification settings - Fork 371
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
causztic
wants to merge
10
commits into
Casecommons:master
Choose a base branch
from
causztic:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ccc2e82
Concat primary key
causztic 00a4fd8
Handle rank joins on subquery
causztic c22a3c8
Update specs, remove unuse method
causztic 038625a
Cleanup
causztic 6b61481
Add failing test cases
causztic e1fd67d
Remove out of scope tests
causztic cf4d3d6
Merge branch 'Casecommons:master' into master
causztic 55834f9
Rename specs
causztic 9af3754
Update test description
causztic a757499
rake standard:fix
causztic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's great that you are testing |
||
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 |
Oops, something went wrong.
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.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
55834f9