diff --git a/audiences/app/models/audiences/external_user.rb b/audiences/app/models/audiences/external_user.rb index 4b53d71d..e1789b69 100644 --- a/audiences/app/models/audiences/external_user.rb +++ b/audiences/app/models/audiences/external_user.rb @@ -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 diff --git a/audiences/app/models/audiences/users_search.rb b/audiences/app/models/audiences/users_search.rb index edce4c82..07f67ab9 100644 --- a/audiences/app/models/audiences/users_search.rb +++ b/audiences/app/models/audiences/users_search.rb @@ -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 diff --git a/audiences/docs/CHANGELOG.md b/audiences/docs/CHANGELOG.md index 711eb8fc..71ca1371 100644 --- a/audiences/docs/CHANGELOG.md +++ b/audiences/docs/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- Fix PostgreSQL compability [#312](https://github.com/powerhome/audiences/pull/312) + # Version 1.0.2 (2024-04-30) - Fix mysql2 compability – drop sqlite3 [#292](https://github.com/powerhome/audiences/pull/292) diff --git a/audiences/spec/models/audiences/external_user_spec.rb b/audiences/spec/models/audiences/external_user_spec.rb index ded4cbb4..7b17ed76 100644 --- a/audiences/spec/models/audiences/external_user_spec.rb +++ b/audiences/spec/models/audiences/external_user_spec.rb @@ -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([ @@ -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" diff --git a/audiences/spec/models/audiences/user_search_spec.rb b/audiences/spec/models/audiences/user_search_spec.rb index 3f68895f..369dac80 100644 --- a/audiences/spec/models/audiences/user_search_spec.rb +++ b/audiences/spec/models/audiences/user_search_spec.rb @@ -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")