mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 08:09:33 +08:00
5cfe323445
What is this change required? I noticed that actions in `SidebarSectionsController` resulted in lots of N+1 queries problem and I wanted a solution to prevent such problems without having to write N+1 queries tests. I have also used strict loading for `SidebarSection` queries in performance sensitive spots. Note that in this commit, I have also set `config.active_record.action_on_strict_loading_violation = :log` for the production environment so that we have more visibility of potential N+1 queries problem in the logs. In development and test environment, we're sticking with the default of raising an error.
81 lines
2.3 KiB
Ruby
81 lines
2.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class SidebarSection < ActiveRecord::Base
|
|
MAX_TITLE_LENGTH = 30
|
|
|
|
belongs_to :user
|
|
has_many :sidebar_section_links, -> { order("position") }, dependent: :destroy
|
|
|
|
has_many :sidebar_urls,
|
|
through: :sidebar_section_links,
|
|
source: :linkable,
|
|
source_type: "SidebarUrl"
|
|
|
|
accepts_nested_attributes_for :sidebar_urls, allow_destroy: true
|
|
|
|
before_save :set_system_user_for_public_section
|
|
|
|
validates :title,
|
|
presence: true,
|
|
uniqueness: {
|
|
scope: %i[user_id],
|
|
},
|
|
length: {
|
|
maximum: MAX_TITLE_LENGTH,
|
|
}
|
|
|
|
scope :public_sections, -> { where("public") }
|
|
enum :section_type, { community: 0 }, scopes: false, suffix: true
|
|
|
|
def reset_community!
|
|
ActiveRecord::Base.transaction do
|
|
self.update!(title: "Community")
|
|
self.sidebar_section_links.destroy_all
|
|
community_urls =
|
|
SidebarUrl::COMMUNITY_SECTION_LINKS.map do |url_data|
|
|
"('#{url_data[:name]}', '#{url_data[:path]}', '#{url_data[:icon]}', '#{url_data[:segment]}', false, now(), now())"
|
|
end
|
|
|
|
result = DB.query <<~SQL
|
|
INSERT INTO sidebar_urls(name, value, icon, segment, external, created_at, updated_at)
|
|
VALUES #{community_urls.join(",")}
|
|
RETURNING sidebar_urls.id
|
|
SQL
|
|
|
|
sidebar_section_links =
|
|
result.map.with_index do |url, index|
|
|
"(-1, #{url.id}, 'SidebarUrl', #{self.id}, #{index}, now(), now())"
|
|
end
|
|
|
|
DB.query <<~SQL
|
|
INSERT INTO sidebar_section_links(user_id, linkable_id, linkable_type, sidebar_section_id, position, created_at, updated_at)
|
|
VALUES #{sidebar_section_links.join(",")}
|
|
SQL
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def set_system_user_for_public_section
|
|
self.user_id = Discourse.system_user.id if self.public
|
|
end
|
|
end
|
|
|
|
# == Schema Information
|
|
#
|
|
# Table name: sidebar_sections
|
|
#
|
|
# id :bigint not null, primary key
|
|
# user_id :integer not null
|
|
# title :string(30) not null
|
|
# created_at :datetime not null
|
|
# updated_at :datetime not null
|
|
# public :boolean default(FALSE), not null
|
|
# section_type :integer
|
|
#
|
|
# Indexes
|
|
#
|
|
# index_sidebar_sections_on_section_type (section_type) UNIQUE
|
|
# index_sidebar_sections_on_user_id_and_title (user_id,title) UNIQUE
|
|
#
|