Commit ffd53c56 authored by David Wilkins's avatar David Wilkins Committed by Sean McGivern

Remove after_initialize, after_create for discussion_id

- We're already overridding the `discussion_id` method in Note.  The
  after_initialize callback is meant to ensure there's a discussion_id
  for all notes if there wasn't one created (or it got deleted?)

- after_initialize runs Ruby code for _every_ instantiation whether it
  needs to or not.  I've found this to cause performance problems.
parent 4136a334
......@@ -152,9 +152,7 @@ class Note < ApplicationRecord
scope :for_note_or_capitalized_note, ->(text) { where(note: [text, text.capitalize]) }
scope :like_note_or_capitalized_note, ->(text) { where('(note LIKE ? OR note LIKE ?)', text, text.capitalize) }
after_initialize :ensure_discussion_id
before_validation :nullify_blank_type, :nullify_blank_line_code
before_validation :set_discussion_id, on: :create
after_save :keep_around_commit, if: :for_project_noteable?, unless: :importing?
after_save :expire_etag_cache, unless: :importing?
after_save :touch_noteable, unless: :importing?
......@@ -394,7 +392,7 @@ class Note < ApplicationRecord
# See `Discussion.override_discussion_id` for details.
def discussion_id(noteable = nil)
discussion_class(noteable).override_discussion_id(self) || super()
discussion_class(noteable).override_discussion_id(self) || super() || ensure_discussion_id
end
# Returns a discussion containing just this note.
......@@ -533,17 +531,13 @@ class Note < ApplicationRecord
end
def ensure_discussion_id
return unless self.persisted?
# Needed in case the SELECT statement doesn't ask for `discussion_id`
return unless self.has_attribute?(:discussion_id)
return if self.discussion_id
return if self.attribute_present?(:discussion_id)
set_discussion_id
update_column(:discussion_id, self.discussion_id)
self.discussion_id = derive_discussion_id
end
def set_discussion_id
self.discussion_id ||= discussion_class.discussion_id(self)
def derive_discussion_id
discussion_class.discussion_id(self)
end
def all_referenced_mentionables_allowed?(user)
......
---
title: Remove after_initialize and before_validation for Note
merge_request: 22128
author:
type: performance
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment