From 834c219b9b3b21cd7ac73e37f3057e148aa43a0d Mon Sep 17 00:00:00 2001 From: Shivam Mishra Date: Tue, 23 Jan 2024 15:50:00 +0530 Subject: [PATCH] feat(perf): update query to do a simpler search [CW-2997] (#8763) Message search would frequently timeout. The reason was that the query would join the conversation too, the new query searches the message table directly Co-authored-by: Sojan --- app/models/inbox.rb | 2 +- spec/mailboxes/imap/imap_mailbox_spec.rb | 2 +- spec/models/inbox_spec.rb | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/app/models/inbox.rb b/app/models/inbox.rb index 311d7369b..17f4fc045 100644 --- a/app/models/inbox.rb +++ b/app/models/inbox.rb @@ -66,7 +66,7 @@ class Inbox < ApplicationRecord has_many :inbox_members, dependent: :destroy_async has_many :members, through: :inbox_members, source: :user has_many :conversations, dependent: :destroy_async - has_many :messages, through: :conversations + has_many :messages, dependent: :destroy_async has_one :agent_bot_inbox, dependent: :destroy_async has_one :agent_bot, through: :agent_bot_inbox diff --git a/spec/mailboxes/imap/imap_mailbox_spec.rb b/spec/mailboxes/imap/imap_mailbox_spec.rb index 798f21112..33302629d 100644 --- a/spec/mailboxes/imap/imap_mailbox_spec.rb +++ b/spec/mailboxes/imap/imap_mailbox_spec.rb @@ -11,7 +11,7 @@ RSpec.describe Imap::ImapMailbox do imap_port: 993, imap_login: 'imap@gmail.com', imap_password: 'password', account: account) end - let(:inbox) { create(:inbox, channel: channel, account: account) } + let(:inbox) { channel.inbox } let!(:contact) { create(:contact, email: 'email@gmail.com', phone_number: '+919584546666', account: account, identifier: '123') } let(:conversation) { Conversation.where(inbox_id: channel.inbox).last } let(:class_instance) { described_class.new } diff --git a/spec/models/inbox_spec.rb b/spec/models/inbox_spec.rb index 30c6ef235..8bcb6bcd5 100644 --- a/spec/models/inbox_spec.rb +++ b/spec/models/inbox_spec.rb @@ -25,7 +25,7 @@ RSpec.describe Inbox do it { is_expected.to have_many(:conversations).dependent(:destroy_async) } - it { is_expected.to have_many(:messages).through(:conversations) } + it { is_expected.to have_many(:messages).dependent(:destroy_async) } it { is_expected.to have_one(:agent_bot_inbox) }