fix: Notification page breakages (#5236)

- Remove the cascading foreign key indexes
- Add migration to clean up existing objects

fixes: #4285
This commit is contained in:
Sojan Jose
2022-08-10 13:46:46 +02:00
committed by GitHub
parent 12b6fb211a
commit 74fdfffe08
23 changed files with 93 additions and 115 deletions

View File

@@ -0,0 +1,19 @@
# Delete migration and spec after 2 consecutive releases.
class Migration::RemoveStaleNotificationsJob < ApplicationJob
queue_as :scheduled_jobs
def perform
remove_invalid_messages
end
private
def remove_invalid_messages
deleted_ids = []
Message.distinct.pluck(:inbox_id).each_slice(1000) do |id_list|
deleted_ids << (id_list - Inbox.where(id: id_list).pluck(:id))
end
Message.where(inbox_id: deleted_ids.flatten).destroy_all
end
end

View File

@@ -55,7 +55,7 @@ class Account < ApplicationRecord
has_many :csat_survey_responses, dependent: :destroy_async
has_many :custom_attribute_definitions, dependent: :destroy_async
has_many :custom_filters, dependent: :destroy_async
has_many :dashboard_apps, dependent: :destroy
has_many :dashboard_apps, dependent: :destroy_async
has_many :data_imports, dependent: :destroy_async
has_many :email_channels, dependent: :destroy_async, class_name: '::Channel::Email'
has_many :facebook_pages, dependent: :destroy_async, class_name: '::Channel::FacebookPage'
@@ -67,7 +67,7 @@ class Account < ApplicationRecord
has_many :messages, dependent: :destroy_async
has_many :notes, dependent: :destroy_async
has_many :notification_settings, dependent: :destroy_async
has_many :notifications, dependent: :destroy
has_many :notifications, dependent: :destroy_async
has_many :portals, dependent: :destroy_async, class_name: '::Portal'
has_many :sms_channels, dependent: :destroy_async, class_name: '::Channel::Sms'
has_many :teams, dependent: :destroy_async

View File

@@ -19,11 +19,6 @@
# index_account_users_on_user_id (user_id)
# uniq_user_id_per_account_id (account_id,user_id) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
# fk_rails_... (user_id => users.id) ON DELETE => cascade
#
class AccountUser < ApplicationRecord
include AvailabilityStatusable

View File

@@ -16,10 +16,6 @@
#
# index_agent_bots_on_account_id (account_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
#
class AgentBot < ApplicationRecord
include AccessTokenable

View File

@@ -23,11 +23,6 @@
# index_articles_on_associated_article_id (associated_article_id)
# index_articles_on_author_id (author_id)
#
# Foreign Keys
#
# fk_rails_... (associated_article_id => articles.id)
# fk_rails_... (author_id => users.id)
#
class Article < ApplicationRecord
include PgSearch::Model

View File

@@ -28,11 +28,6 @@
# index_campaigns_on_inbox_id (inbox_id)
# index_campaigns_on_scheduled_at (scheduled_at)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
# fk_rails_... (inbox_id => inboxes.id) ON DELETE => cascade
#
class Campaign < ApplicationRecord
include UrlHelper
validates :account_id, presence: true

View File

@@ -23,11 +23,6 @@
# index_categories_on_parent_category_id (parent_category_id)
# index_categories_on_slug_and_locale_and_portal_id (slug,locale,portal_id) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (associated_category_id => categories.id)
# fk_rails_... (parent_category_id => categories.id)
#
class Category < ApplicationRecord
belongs_to :account
belongs_to :portal

View File

@@ -19,11 +19,6 @@
# index_contact_inboxes_on_pubsub_token (pubsub_token) UNIQUE
# index_contact_inboxes_on_source_id (source_id)
#
# Foreign Keys
#
# fk_rails_... (contact_id => contacts.id) ON DELETE => cascade
# fk_rails_... (inbox_id => inboxes.id) ON DELETE => cascade
#
class ContactInbox < ApplicationRecord
include Pubsubable

View File

@@ -37,12 +37,6 @@
# index_conversations_on_status_and_account_id (status,account_id)
# index_conversations_on_team_id (team_id)
#
# Foreign Keys
#
# fk_rails_... (campaign_id => campaigns.id) ON DELETE => cascade
# fk_rails_... (contact_inbox_id => contact_inboxes.id) ON DELETE => cascade
# fk_rails_... (team_id => teams.id) ON DELETE => cascade
#
class Conversation < ApplicationRecord
include Labelable
@@ -88,7 +82,7 @@ class Conversation < ApplicationRecord
has_many :mentions, dependent: :destroy_async
has_many :messages, dependent: :destroy_async, autosave: true
has_one :csat_survey_response, dependent: :destroy_async
has_many :notifications, as: :primary_actor, dependent: :destroy
has_many :notifications, as: :primary_actor, dependent: :destroy_async
before_save :ensure_snooze_until_reset
before_create :mark_conversation_pending_if_bot

View File

@@ -21,14 +21,6 @@
# index_csat_survey_responses_on_conversation_id (conversation_id)
# index_csat_survey_responses_on_message_id (message_id) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
# fk_rails_... (assigned_agent_id => users.id) ON DELETE => cascade
# fk_rails_... (contact_id => contacts.id) ON DELETE => cascade
# fk_rails_... (conversation_id => conversations.id) ON DELETE => cascade
# fk_rails_... (message_id => messages.id) ON DELETE => cascade
#
class CsatSurveyResponse < ApplicationRecord
belongs_to :account
belongs_to :conversation

View File

@@ -15,11 +15,6 @@
# index_dashboard_apps_on_account_id (account_id)
# index_dashboard_apps_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id)
# fk_rails_... (user_id => users.id)
#
class DashboardApp < ApplicationRecord
belongs_to :user
belongs_to :account

View File

@@ -16,10 +16,6 @@
#
# index_data_imports_on_account_id (account_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
#
class DataImport < ApplicationRecord
belongs_to :account
validates :data_type, inclusion: { in: ['contacts'], message: I18n.t('errors.data_import.data_type.invalid') }

View File

@@ -18,11 +18,6 @@
# index_macros_on_created_by_id (created_by_id)
# index_macros_on_updated_by_id (updated_by_id)
#
# Foreign Keys
#
# fk_rails_... (created_by_id => users.id)
# fk_rails_... (updated_by_id => users.id)
#
class Macro < ApplicationRecord
belongs_to :account
belongs_to :created_by,

View File

@@ -17,11 +17,6 @@
# index_mentions_on_user_id (user_id)
# index_mentions_on_user_id_and_conversation_id (user_id,conversation_id) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (conversation_id => conversations.id) ON DELETE => cascade
# fk_rails_... (user_id => users.id) ON DELETE => cascade
#
class Mention < ApplicationRecord
include SortHandler

View File

@@ -85,8 +85,9 @@ class Message < ApplicationRecord
belongs_to :contact, required: false
belongs_to :sender, polymorphic: true, required: false
has_many :attachments, dependent: :destroy_async, autosave: true, before_add: :validate_attachments_limit
has_many :attachments, dependent: :destroy, autosave: true, before_add: :validate_attachments_limit
has_one :csat_survey_response, dependent: :destroy_async
has_many :notifications, as: :primary_actor, dependent: :destroy_async
after_create_commit :execute_after_create_commit_callbacks

View File

@@ -16,12 +16,6 @@
# index_notes_on_contact_id (contact_id)
# index_notes_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
# fk_rails_... (contact_id => contacts.id) ON DELETE => cascade
# fk_rails_... (user_id => users.id) ON DELETE => cascade
#
class Note < ApplicationRecord
before_validation :ensure_account_id
validates :content, presence: true

View File

@@ -15,10 +15,6 @@
# index_teams_on_account_id (account_id)
# index_teams_on_name_and_account_id (name,account_id) UNIQUE
#
# Foreign Keys
#
# fk_rails_... (account_id => accounts.id) ON DELETE => cascade
#
class Team < ApplicationRecord
belongs_to :account
has_many :team_members, dependent: :destroy_async

View File

@@ -14,11 +14,6 @@
# index_team_members_on_team_id_and_user_id (team_id,user_id) UNIQUE
# index_team_members_on_user_id (user_id)
#
# Foreign Keys
#
# fk_rails_... (team_id => teams.id) ON DELETE => cascade
# fk_rails_... (user_id => users.id) ON DELETE => cascade
#
class TeamMember < ApplicationRecord
belongs_to :user
belongs_to :team