From f70f39dd04886c2ced6d424cdcb8690fcddd52f7 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Fri, 1 Dec 2023 10:52:47 -0500 Subject: [PATCH] Add explicit `dependent: nil` to associations (#28169) --- .rubocop_todo.yml | 14 -------------- app/models/concerns/account/counters.rb | 2 +- app/models/conversation.rb | 2 +- app/models/custom_emoji.rb | 2 +- app/models/custom_emoji_category.rb | 2 +- app/models/domain_block.rb | 2 +- app/models/invite.rb | 2 +- app/models/status.rb | 14 +++++++++----- app/models/user.rb | 8 ++++---- app/models/web/push_subscription.rb | 2 +- 10 files changed, 20 insertions(+), 30 deletions(-) diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 9fb163ceb9..03543c8e0d 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -112,20 +112,6 @@ Rails/HasAndBelongsToMany: - 'app/models/status.rb' - 'app/models/tag.rb' -# Configuration parameters: Include. -# Include: app/models/**/*.rb -Rails/HasManyOrHasOneDependent: - Exclude: - - 'app/models/concerns/account/counters.rb' - - 'app/models/conversation.rb' - - 'app/models/custom_emoji.rb' - - 'app/models/custom_emoji_category.rb' - - 'app/models/domain_block.rb' - - 'app/models/invite.rb' - - 'app/models/status.rb' - - 'app/models/user.rb' - - 'app/models/web/push_subscription.rb' - # Configuration parameters: Include. # Include: app/controllers/**/*.rb, app/mailers/**/*.rb Rails/LexicallyScopedActionFilter: diff --git a/app/models/concerns/account/counters.rb b/app/models/concerns/account/counters.rb index fb69be9b70..4488398128 100644 --- a/app/models/concerns/account/counters.rb +++ b/app/models/concerns/account/counters.rb @@ -6,7 +6,7 @@ module Account::Counters ALLOWED_COUNTER_KEYS = %i(statuses_count following_count followers_count).freeze included do - has_one :account_stat, inverse_of: :account + has_one :account_stat, inverse_of: :account, dependent: nil after_save :save_account_stat end diff --git a/app/models/conversation.rb b/app/models/conversation.rb index 5de2599627..a7fe148840 100644 --- a/app/models/conversation.rb +++ b/app/models/conversation.rb @@ -13,7 +13,7 @@ class Conversation < ApplicationRecord validates :uri, uniqueness: true, if: :uri? - has_many :statuses + has_many :statuses, dependent: nil def local? uri.nil? diff --git a/app/models/custom_emoji.rb b/app/models/custom_emoji.rb index 717de27723..97b1c63bf3 100644 --- a/app/models/custom_emoji.rb +++ b/app/models/custom_emoji.rb @@ -37,7 +37,7 @@ class CustomEmoji < ApplicationRecord belongs_to :category, class_name: 'CustomEmojiCategory', optional: true - has_one :local_counterpart, -> { where(domain: nil) }, class_name: 'CustomEmoji', primary_key: :shortcode, foreign_key: :shortcode, inverse_of: false + has_one :local_counterpart, -> { where(domain: nil) }, class_name: 'CustomEmoji', primary_key: :shortcode, foreign_key: :shortcode, inverse_of: false, dependent: nil has_attached_file :image, styles: { static: { format: 'png', convert_options: '-coalesce +profile "!icc,*" +set date:modify +set date:create +set date:timestamp' } }, validate_media_type: false diff --git a/app/models/custom_emoji_category.rb b/app/models/custom_emoji_category.rb index 3c87f2b2e5..f480746517 100644 --- a/app/models/custom_emoji_category.rb +++ b/app/models/custom_emoji_category.rb @@ -11,7 +11,7 @@ # class CustomEmojiCategory < ApplicationRecord - has_many :emojis, class_name: 'CustomEmoji', foreign_key: 'category_id', inverse_of: :category + has_many :emojis, class_name: 'CustomEmoji', foreign_key: 'category_id', inverse_of: :category, dependent: nil validates :name, presence: true, uniqueness: true end diff --git a/app/models/domain_block.rb b/app/models/domain_block.rb index a7af2d62e2..ff23f8fcc4 100644 --- a/app/models/domain_block.rb +++ b/app/models/domain_block.rb @@ -25,7 +25,7 @@ class DomainBlock < ApplicationRecord validates :domain, presence: true, uniqueness: true, domain: true - has_many :accounts, foreign_key: :domain, primary_key: :domain, inverse_of: false + has_many :accounts, foreign_key: :domain, primary_key: :domain, inverse_of: false, dependent: nil delegate :count, to: :accounts, prefix: true scope :matches_domain, ->(value) { where(arel_table[:domain].matches("%#{value}%")) } diff --git a/app/models/invite.rb b/app/models/invite.rb index 8e816cef06..c0cbc58458 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -20,7 +20,7 @@ class Invite < ApplicationRecord include Expireable belongs_to :user, inverse_of: :invites - has_many :users, inverse_of: :invite + has_many :users, inverse_of: :invite, dependent: nil scope :available, -> { where(expires_at: nil).or(where('expires_at >= ?', Time.now.utc)) } diff --git a/app/models/status.rb b/app/models/status.rb index 7b1ca69c7c..eb7159dc61 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -66,12 +66,14 @@ class Status < ApplicationRecord has_many :bookmarks, inverse_of: :status, dependent: :destroy has_many :reblogs, foreign_key: 'reblog_of_id', class_name: 'Status', inverse_of: :reblog, dependent: :destroy has_many :reblogged_by_accounts, through: :reblogs, class_name: 'Account', source: :account - has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread + has_many :replies, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :thread, dependent: nil has_many :mentions, dependent: :destroy, inverse_of: :status has_many :mentioned_accounts, through: :mentions, source: :account, class_name: 'Account' - has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status has_many :media_attachments, dependent: :nullify + # The `dependent` option is enabled by the initial `mentions` association declaration + has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status # rubocop:disable Rails/HasManyOrHasOneDependent + # Those associations are used for the private search index has_many :local_mentioned, -> { merge(Account.local) }, through: :active_mentions, source: :account has_many :local_favorited, -> { merge(Account.local) }, through: :favourites, source: :account @@ -80,11 +82,13 @@ class Status < ApplicationRecord has_and_belongs_to_many :tags - has_one :preview_cards_status, inverse_of: :status # Because of a composite primary key, the dependent option cannot be used + # Because of a composite primary key, the `dependent` option cannot be used on this association + has_one :preview_cards_status, inverse_of: :status # rubocop:disable Rails/HasManyOrHasOneDependent + has_one :notification, as: :activity, dependent: :destroy - has_one :status_stat, inverse_of: :status + has_one :status_stat, inverse_of: :status, dependent: nil has_one :poll, inverse_of: :status, dependent: :destroy - has_one :trend, class_name: 'StatusTrend', inverse_of: :status + has_one :trend, class_name: 'StatusTrend', inverse_of: :status, dependent: nil validates :uri, uniqueness: true, presence: true, unless: :local? validates :text, presence: true, unless: -> { with_media? || reblog? } diff --git a/app/models/user.rb b/app/models/user.rb index 550f8bfd30..a1574c02ad 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -84,12 +84,12 @@ class User < ApplicationRecord belongs_to :role, class_name: 'UserRole', optional: true accepts_nested_attributes_for :account - has_many :applications, class_name: 'Doorkeeper::Application', as: :owner - has_many :backups, inverse_of: :user - has_many :invites, inverse_of: :user + has_many :applications, class_name: 'Doorkeeper::Application', as: :owner, dependent: nil + has_many :backups, inverse_of: :user, dependent: nil + has_many :invites, inverse_of: :user, dependent: nil has_many :markers, inverse_of: :user, dependent: :destroy has_many :webauthn_credentials, dependent: :destroy - has_many :ips, class_name: 'UserIp', inverse_of: :user + has_many :ips, class_name: 'UserIp', inverse_of: :user, dependent: nil has_one :invite_request, class_name: 'UserInviteRequest', inverse_of: :user, dependent: :destroy accepts_nested_attributes_for :invite_request, reject_if: ->(attributes) { attributes['text'].blank? && !Setting.require_invite_text } diff --git a/app/models/web/push_subscription.rb b/app/models/web/push_subscription.rb index 0ffbe068eb..a3a2ec3f03 100644 --- a/app/models/web/push_subscription.rb +++ b/app/models/web/push_subscription.rb @@ -19,7 +19,7 @@ class Web::PushSubscription < ApplicationRecord belongs_to :user, optional: true belongs_to :access_token, class_name: 'Doorkeeper::AccessToken', optional: true - has_one :session_activation, foreign_key: 'web_push_subscription_id', inverse_of: :web_push_subscription + has_one :session_activation, foreign_key: 'web_push_subscription_id', inverse_of: :web_push_subscription, dependent: nil validates :endpoint, presence: true validates :key_p256dh, presence: true