diff --git a/app/jobs/conversations/multi_search_job.rb b/app/jobs/conversations/multi_search_job.rb new file mode 100644 index 000000000..cdaeb04a0 --- /dev/null +++ b/app/jobs/conversations/multi_search_job.rb @@ -0,0 +1,9 @@ +class Conversations::MultiSearchJob < ApplicationJob + queue_as :default + + def perform + Contact.rebuild_pg_search_documents + PgSearch::Multisearch.rebuild(Conversation) + PgSearch::Multisearch.rebuild(Message) + end +end diff --git a/app/models/contact.rb b/app/models/contact.rb index 41c0bae4a..2f46e9172 100644 --- a/app/models/contact.rb +++ b/app/models/contact.rb @@ -147,6 +147,8 @@ class Contact < ApplicationRecord email_format end + # NOTE: To add multi search records with conversation_id associated to contacts for previously added records. + # We can not find conversation_id from contacts directly so we added this joins here. def self.rebuild_pg_search_documents return super unless name == 'Contact' diff --git a/db/migrate/20221212061802_enable_multi_searchable.rb b/db/migrate/20221212061802_enable_multi_searchable.rb index 4680478f0..3ebd4ee97 100644 --- a/db/migrate/20221212061802_enable_multi_searchable.rb +++ b/db/migrate/20221212061802_enable_multi_searchable.rb @@ -1,8 +1,6 @@ class EnableMultiSearchable < ActiveRecord::Migration[6.1] def up - Contact.rebuild_pg_search_documents - PgSearch::Multisearch.rebuild(Conversation) - PgSearch::Multisearch.rebuild(Message) + ::Conversations::MultiSearchJob.perform_now execute 'CREATE EXTENSION IF NOT EXISTS pg_trgm;' end diff --git a/spec/finders/text_search_spec.rb b/spec/finders/text_search_spec.rb index 0258b4dff..a5d1e6def 100644 --- a/spec/finders/text_search_spec.rb +++ b/spec/finders/text_search_spec.rb @@ -59,5 +59,29 @@ describe ::TextSearch do expect(result[:conversations].length).to be 1 end end + + context 'when create records in tables including multi search' do + let(:contact) { create(:contact, name: 'Welma', account_id: account.id, email: 'welma@scoobydoo.com') } + let(:conversation) { create(:conversation, account: account, inbox: inbox, assignee: user_1, status: 'open', contact_id: contact.id) } + + it 'conversation creation pg search records' do + contact_search_record = PgSearch::Document.find_by(searchable_id: contact.id, searchable_type: contact.class.name) + conversation_search_record = PgSearch::Document.find_by(searchable_id: conversation.id, searchable_type: conversation.class.name) + + expect(contact_search_record).to be_present + expect(conversation_search_record).to be_present + end + + it 'conversation deletion deletes pg search records' do + contact.destroy! + conversation.destroy! + + contact_search_record = PgSearch::Document.find_by(searchable_id: contact.id, searchable_type: contact.class.name) + conversation_search_record = PgSearch::Document.find_by(searchable_id: conversation.id, searchable_type: conversation.class.name) + + expect(contact_search_record).to be_nil + expect(conversation_search_record).to be_nil + end + end end end