discourse/db/migrate/20230411032053_insert_community_to_sidebar_sections.rb
Krzysztof Kotlarek 709fa24558
DEV: move sidebar community section to database (#21166)
* DEV: move sidebar community section to database

Before, community section was hard-coded. In the future, we are planning to allow admins to edit it. Therefore, it has to be moved to database to `custom_sections` table.

Few steps and simplifications has to be made:
- custom section was hidden behind `enable_custom_sidebar_sections` feature flag. It has to be deleted so all forums, see community section;
- migration to add `section_type` column to sidebar section to show it is a special type;
- migration to add `segment` column to sidebar links to determine if link should be displayed in primary section or in more section;
- simplify more section to have one level only (secondary section links are merged);
- ensure that links like `everything` are correctly tracking state;
- make user an anonymous links position consistence. For example, from now on `faq` link for user and anonymous is visible in more tab;
- delete old community-section template.
2023-05-04 12:14:09 +10:00

69 lines
2.4 KiB
Ruby

# frozen_string_literal: true
class InsertCommunityToSidebarSections < ActiveRecord::Migration[7.0]
COMMUNITY_SECTION_LINKS = [
{ name: "Everything", path: "/latest", icon: "layer-group", segment: 0 },
{ name: "My Posts", path: "/my/activity", icon: "user", segment: 0 },
{ name: "Review", path: "/review", icon: "flag", segment: 0 },
{ name: "Admin", path: "/admin", icon: "wrench", segment: 0 },
{ name: "Users", path: "/u", icon: "users", segment: 1 },
{ name: "About", path: "/about", icon: "info-circle", segment: 1 },
{ name: "FAQ", path: "/faq", icon: "question-circle", segment: 1 },
{ name: "Groups", path: "/g", icon: "user-friends", segment: 1 },
{ name: "Badges", path: "/badges", icon: "certificate", segment: 1 },
]
def up
result = DB.query <<~SQL
INSERT INTO sidebar_sections(user_id, title, public, section_type, created_at, updated_at)
VALUES (-1, 'Community', true, 0, now(), now())
RETURNING sidebar_sections.id
SQL
community_section_id = result.last&.id
sidebar_urls =
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 #{sidebar_urls.join(",")}
RETURNING sidebar_urls.id
SQL
sidebar_section_links =
result.map.with_index do |url, index|
"(-1, #{url.id}, 'SidebarUrl', #{community_section_id}, #{index}, now(), now())"
end
result = 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
def down
result = DB.query <<~SQL
DELETE FROM sidebar_sections
WHERE section_type = 0
RETURNING sidebar_sections.id
SQL
community_section_id = result.last&.id
return true if !community_section_id
result = DB.query <<~SQL
DELETE FROM sidebar_section_links
WHERE sidebar_section_id = #{community_section_id}
RETURNING sidebar_section_links.linkable_id
SQL
sidebar_url_ids = result.map(&:linkable_id)
DB.query <<~SQL
DELETE FROM sidebar_urls
WHERE id IN (#{sidebar_url_ids.join(",")})
SQL
end
end