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 postgresql known issues #312

Merged
merged 5 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion audiences/app/models/audiences/external_user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ def self.wrap(resources)
attrs = resources.map do |data|
{ user_id: data["id"], data: data, created_at: Time.current, updated_at: Time.current }
end
upsert_all(attrs) # rubocop:disable Rails/SkipsModelValidations
unique_by = :user_id if connection.supports_insert_conflict_target?
upsert_all(attrs, unique_by: unique_by) # rubocop:disable Rails/SkipsModelValidations
where(user_id: attrs.pluck(:user_id))
end

Expand Down
9 changes: 8 additions & 1 deletion audiences/app/models/audiences/users_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ def users
private

def result
@result ||= @scope.where("data LIKE ?", "%#{@query}%")
@result ||= @scope.where("#{data_attribute_query} LIKE ?", "%#{@query}%")
end

def data_attribute_query
case @scope.connection.adapter_name
when "PostgreSQL" then "CAST(data AS TEXT)"
else "CAST(data AS CHAR)"
end
end
end
end
13 changes: 6 additions & 7 deletions audiences/spec/models/audiences/external_user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "rails_helper"

RSpec.describe Audiences::ExternalUser do
RSpec.describe Audiences::ExternalUser, :aggregate_failures do
describe "#map" do
it "takes a list of user data and creates ExternalUser instances, returning them" do
john, joseph, mary, steve, *others = Audiences::ExternalUser.wrap([
Expand All @@ -26,13 +26,12 @@

it "updates existing users" do
joseph = Audiences::ExternalUser.create(user_id: 456, data: { "id" => 456, displayName: "Joseph F. Doe" })
user_data = [
{ "id" => 123, "displayName" => "John Doe" },
{ "id" => 456, "displayName" => "Joseph Doe" },
]

john, updated_joseph, *others = Audiences::ExternalUser.wrap([
{ "id" => 123,
"displayName" => "John Doe" },
{ "id" => 456,
"displayName" => "Joseph Doe" },
])
john, updated_joseph, *others = Audiences::ExternalUser.wrap(user_data).order(:user_id)

expect(others).to be_empty
expect(john.user_id).to eql "123"
Expand Down
10 changes: 8 additions & 2 deletions audiences/spec/models/audiences/user_search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@

RSpec.describe Audiences::UsersSearch do
it "searches through any serialized data attribute" do
john_doe = Audiences::ExternalUser.create(user_id: 123, data: { name: "John Doe" })
frank_doe = Audiences::ExternalUser.create(user_id: 321, data: { name: "Frank Doe", territory: "Philadelphia" })
john_doe = Audiences::ExternalUser.create(
user_id: 123,
data: { displayName: "John Doe" }
)
frank_doe = Audiences::ExternalUser.create(
user_id: 321,
data: { displayName: "Frank Doe", territory: "Philadelphia" }
)

john_search = Audiences::UsersSearch.new(query: "John")
phila_search = Audiences::UsersSearch.new(query: "Phila")
Expand Down
Loading