Skip to content

A platform for community discussion. Free, open, simple.

DEV: Add `categories` converter and importer steps (#33067)

Selase Krakani5ad072aab456
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index e390ea2698f..12852b8f1c7 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -15,6 +15,29 @@ schema:
exclude:
- "grant_count"
- "system"
+ categories:
+ columns:
+ add:
+ - name: "about_topic_title"
+ datatype: text
+ - name: "existing_id"
+ datatype: numeric
+ exclude:
+ - "contains_messages"
+ - "latest_post_id"
+ - "latest_topic_id"
+ - "name_lower"
+ - "post_count"
+ - "posts_day"
+ - "posts_month"
+ - "posts_week"
+ - "posts_year"
+ - "topic_count"
+ - "topics_day"
+ - "topics_month"
+ - "topics_week"
+ - "topics_year"
+ - "reviewable_by_group_id"
user_emails:
columns:
include:
@@ -79,7 +102,6 @@ schema:
- "badge_groupings"
- "badge_types"
- "bookmarks"
- - "categories"
- "categories_web_hooks"
- "category_custom_fields"
- "category_featured_topics"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index e1e89c1d090..d17e5c6d1f9 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -26,6 +26,56 @@ CREATE TABLE badges
"trigger" INTEGER
);
+CREATE TABLE categories
+(
+ original_id NUMERIC NOT NULL PRIMARY KEY,
+ about_topic_title TEXT,
+ all_topics_wiki BOOLEAN,
+ allow_badges BOOLEAN,
+ allow_global_tags BOOLEAN,
+ allow_unlimited_owner_edits_on_first_post BOOLEAN,
+ auto_close_based_on_last_post BOOLEAN,
+ auto_close_hours FLOAT,
+ color TEXT,
+ created_at DATETIME,
+ default_list_filter TEXT,
+ default_slow_mode_seconds INTEGER,
+ default_top_period TEXT,
+ default_view TEXT,
+ description TEXT,
+ email_in TEXT,
+ email_in_allow_strangers BOOLEAN,
+ emoji TEXT,
+ existing_id NUMERIC,
+ icon TEXT,
+ locale TEXT,
+ mailinglist_mirror BOOLEAN,
+ minimum_required_tags INTEGER,
+ name TEXT NOT NULL,
+ navigate_to_first_post_after_read BOOLEAN,
+ num_featured_topics INTEGER,
+ parent_category_id NUMERIC,
+ position INTEGER,
+ read_only_banner TEXT,
+ read_restricted BOOLEAN,
+ search_priority INTEGER,
+ show_subcategory_list BOOLEAN,
+ slug TEXT NOT NULL,
+ sort_ascending BOOLEAN,
+ sort_order TEXT,
+ style_type INTEGER,
+ subcategory_list_style TEXT,
+ text_color TEXT,
+ topic_featured_link_allowed BOOLEAN,
+ topic_id NUMERIC,
+ topic_template TEXT,
+ uploaded_background_dark_id TEXT,
+ uploaded_background_id TEXT,
+ uploaded_logo_dark_id TEXT,
+ uploaded_logo_id TEXT,
+ user_id NUMERIC NOT NULL
+);
+
CREATE TABLE user_emails
(
email TEXT NOT NULL,
diff --git a/migrations/lib/converters/discourse/settings.yml b/migrations/lib/converters/discourse/settings.yml
index 058130377f0..71bb5609357 100644
--- a/migrations/lib/converters/discourse/settings.yml
+++ b/migrations/lib/converters/discourse/settings.yml
@@ -7,3 +7,6 @@ source_db:
user: "username"
password: "password"
dbname: "discourse_source_db"
+
+categories:
+ map_seeded_categories: true
diff --git a/migrations/lib/converters/discourse/steps/categories.rb b/migrations/lib/converters/discourse/steps/categories.rb
new file mode 100644
index 00000000000..b35880db917
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/categories.rb
@@ -0,0 +1,114 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class Categories < ::Migrations::Converters::Base::ProgressStep
+ SEEDED_CATEGORY_SETTINGS = %w[
+ uncategorized_category_id
+ meta_category_id
+ staff_category_id
+ general_category_id
+ ].freeze
+
+ attr_accessor :source_db
+
+ def execute
+ super
+
+ @background_upload_creator = UploadCreator.new(column_prefix: "background")
+ @background_dark_upload_creator = UploadCreator.new(column_prefix: "background_dark")
+ @logo_upload_creator = UploadCreator.new(column_prefix: "logo")
+ @logo_dark_upload_creator = UploadCreator.new(column_prefix: "logo_dark")
+ end
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*) FROM categories
+ SQL
+ end
+
+ def items
+ @source_db.query(
+ <<~SQL,
+ SELECT c.*,
+ ss.name AS existing_id,
+ bg.url AS background_url,
+ bg.original_filename AS background_filename,
+ bg.origin AS background_origin,
+ bg.user_id AS background_user_id,
+ bg_dark.url AS background_dark_url,
+ bg_dark.original_filename AS background_dark_filename,
+ bg_dark.origin AS background_dark_origin,
+ bg_dark.user_id AS background_dark_user_id,
+ logo.url AS logo_url,
+ logo.original_filename AS logo_filename,
+ logo.origin AS logo_origin,
+ logo.user_id AS logo_user_id,
+ logo_dark.url AS logo_dark_url,
+ logo_dark.original_filename AS logo_dark_filename,
+ logo_dark.origin AS logo_dark_origin,
+ logo_dark.user_id AS logo_dark_user_id
+ FROM categories c
+ LEFT JOIN site_settings ss ON $1
+ AND c.id = ss.value::int
+ AND ss.name = ANY($2::text[])
+ LEFT JOIN uploads bg ON c.uploaded_background_id = bg.id
+ LEFT JOIN uploads bg_dark ON c.uploaded_background_dark_id = bg_dark.id
+ LEFT JOIN uploads logo ON c.uploaded_logo_id = logo.id
+ LEFT JOIN uploads logo_dark ON c.uploaded_logo_dark_id = logo_dark.id
+ ORDER BY c.id
+ SQL
+ !!settings.dig(:categories, :map_seeded_categories),
+ @source_db.encode_array(SEEDED_CATEGORY_SETTINGS),
+ )
+ end
+
+ def process_item(item)
+ IntermediateDB::Category.create(
+ original_id: item[:id],
+ about_topic_title: item[:about_topic_title],
+ all_topics_wiki: item[:all_topics_wiki],
+ allow_badges: item[:allow_badges],
+ allow_global_tags: item[:allow_global_tags],
+ allow_unlimited_owner_edits_on_first_post: item[:allow_unlimited_owner_edits_on_first_post],
+ auto_close_based_on_last_post: item[:auto_close_based_on_last_post],
+ auto_close_hours: item[:auto_close_hours],
+ color: item[:color],
+ created_at: item[:created_at],
+ default_list_filter: item[:default_list_filter],
+ default_slow_mode_seconds: item[:default_slow_mode_seconds],
+ default_top_period: item[:default_top_period],
+ default_view: item[:default_view],
+ description: item[:description],
+ email_in: item[:email_in],
+ email_in_allow_strangers: item[:email_in_allow_strangers],
+ emoji: item[:emoji],
+ existing_id: item[:existing_id],
+ icon: item[:icon],
+ mailinglist_mirror: item[:mailinglist_mirror],
+ minimum_required_tags: item[:minimum_required_tags],
+ name: item[:name],
+ navigate_to_first_post_after_read: item[:navigate_to_first_post_after_read],
+ num_featured_topics: item[:num_featured_topics],
+ parent_category_id: item[:parent_category_id],
+ position: item[:position],
+ read_only_banner: item[:read_only_banner],
+ read_restricted: item[:read_restricted],
+ show_subcategory_list: item[:show_subcategory_list],
+ slug: item[:slug],
+ sort_ascending: item[:sort_ascending],
+ sort_order: item[:sort_order],
+ style_type: item[:style_type],
+ subcategory_list_style: item[:subcategory_list_style],
+ text_color: item[:text_color],
+ topic_featured_link_allowed: item[:topic_featured_link_allowed],
+ topic_id: item[:topic_id],
+ topic_template: item[:topic_template],
+ uploaded_background_dark_id: @background_dark_upload_creator.create_for(item),
+ uploaded_background_id: @background_upload_creator.create_for(item),
+ uploaded_logo_dark_id: @logo_dark_upload_creator.create_for(item),
+ uploaded_logo_id: @logo_upload_creator.create_for(item),
+ user_id: item[:user_id],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/adapter/postgres.rb b/migrations/lib/database/adapter/postgres.rb
index b1e64b41c2f..57229a8b980 100644
--- a/migrations/lib/database/adapter/postgres.rb
+++ b/migrations/lib/database/adapter/postgres.rb
@@ -59,6 +59,12 @@ module Migrations::Database::Adapter
@connection.escape_string(str)
end
+ def encode_array(array)
+ @array_encoder ||= PG::TextEncoder::Array.new
+
+ @array_encoder.encode(array)
+ end
+
private
def configure_connection
diff --git a/migrations/lib/database/connection.rb b/migrations/lib/database/connection.rb
index e230adbdabb..a62e8af6baa 100644
--- a/migrations/lib/database/connection.rb
+++ b/migrations/lib/database/connection.rb
@@ -64,6 +64,10 @@ module Migrations::Database
@db.query(sql, *parameters, &block)
end
+ def query_array(sql, *parameters, &block)
+ @db.query_array(sql, *parameters, &block)
+ end
+
def count(sql, *parameters)
@db.query_single_splat(sql, *parameters)
end
diff --git a/migrations/lib/database/intermediate_db/category.rb b/migrations/lib/database/intermediate_db/category.rb
new file mode 100644
index 00000000000..fd4c60d9d73
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/category.rb
@@ -0,0 +1,163 @@
+# frozen_string_literal: true
+
+# This file is auto-generated from the IntermediateDB schema. To make changes,
+# update the "config/intermediate_db.yml" configuration file and then run
+# `bin/cli schema generate` to regenerate this file.
+
+module Migrations::Database::IntermediateDB
+ module Category
+ SQL = <<~SQL
+ INSERT INTO categories (
+ original_id,
+ about_topic_title,
+ all_topics_wiki,
+ allow_badges,
+ allow_global_tags,
+ allow_unlimited_owner_edits_on_first_post,
+ auto_close_based_on_last_post,
+ auto_close_hours,
+ color,
+ created_at,
+ default_list_filter,
+ default_slow_mode_seconds,
+ default_top_period,
+ default_view,
+ description,
+ email_in,
+ email_in_allow_strangers,
+ emoji,
+ existing_id,
+ icon,
+ locale,
+ mailinglist_mirror,
+ minimum_required_tags,
+ name,
+ navigate_to_first_post_after_read,
+ num_featured_topics,
+ parent_category_id,
+ position,
+ read_only_banner,
+ read_restricted,
+ search_priority,
+ show_subcategory_list,
+ slug,
+ sort_ascending,
+ sort_order,
+ style_type,
+ subcategory_list_style,
+ text_color,
+ topic_featured_link_allowed,
+ topic_id,
+ topic_template,
+ uploaded_background_dark_id,
+ uploaded_background_id,
+ uploaded_logo_dark_id,
+ uploaded_logo_id,
+ user_id
+ )
+ VALUES (
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
+ )
+ SQL
+
+ def self.create(
+ original_id:,
+ about_topic_title: nil,
+ all_topics_wiki: nil,
+ allow_badges: nil,
+ allow_global_tags: nil,
+ allow_unlimited_owner_edits_on_first_post: nil,
+ auto_close_based_on_last_post: nil,
+ auto_close_hours: nil,
+ color: nil,
+ created_at: nil,
+ default_list_filter: nil,
+ default_slow_mode_seconds: nil,
+ default_top_period: nil,
+ default_view: nil,
+ description: nil,
+ email_in: nil,
+ email_in_allow_strangers: nil,
+ emoji: nil,
+ existing_id: nil,
+ icon: nil,
+ locale: nil,
+ mailinglist_mirror: nil,
+ minimum_required_tags: nil,
+ name:,
+ navigate_to_first_post_after_read: nil,
+ num_featured_topics: nil,
+ parent_category_id: nil,
+ position: nil,
+ read_only_banner: nil,
+ read_restricted: nil,
+ search_priority: nil,
+ show_subcategory_list: nil,
+ slug:,
+ sort_ascending: nil,
+ sort_order: nil,
+ style_type: nil,
+ subcategory_list_style: nil,
+ text_color: nil,
+ topic_featured_link_allowed: nil,
+ topic_id: nil,
+ topic_template: nil,
+ uploaded_background_dark_id: nil,
+ uploaded_background_id: nil,
+ uploaded_logo_dark_id: nil,
+ uploaded_logo_id: nil,
+ user_id:
+ )
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ original_id,
+ about_topic_title,
+ ::Migrations::Database.format_boolean(all_topics_wiki),
+ ::Migrations::Database.format_boolean(allow_badges),
+ ::Migrations::Database.format_boolean(allow_global_tags),
+ ::Migrations::Database.format_boolean(allow_unlimited_owner_edits_on_first_post),
+ ::Migrations::Database.format_boolean(auto_close_based_on_last_post),
+ auto_close_hours,
+ color,
+ ::Migrations::Database.format_datetime(created_at),
+ default_list_filter,
+ default_slow_mode_seconds,
+ default_top_period,
+ default_view,
+ description,
+ email_in,
+ ::Migrations::Database.format_boolean(email_in_allow_strangers),
+ emoji,
+ existing_id,
+ icon,
+ locale,
+ ::Migrations::Database.format_boolean(mailinglist_mirror),
+ minimum_required_tags,
+ name,
+ ::Migrations::Database.format_boolean(navigate_to_first_post_after_read),
+ num_featured_topics,
+ parent_category_id,
+ position,
+ read_only_banner,
+ ::Migrations::Database.format_boolean(read_restricted),
+ search_priority,
+ ::Migrations::Database.format_boolean(show_subcategory_list),
+ slug,
+ ::Migrations::Database.format_boolean(sort_ascending),
+ sort_order,
+ style_type,
+ subcategory_list_style,
+ text_color,
+ ::Migrations::Database.format_boolean(topic_featured_link_allowed),
+ topic_id,
+ topic_template,
+ uploaded_background_dark_id,
+ uploaded_background_id,
+ uploaded_logo_dark_id,
+ uploaded_logo_id,
+ user_id,
+ )
+ end
+ end
+end
diff --git a/migrations/lib/importer/mapping_type.rb b/migrations/lib/importer/mapping_type.rb
index d2e16c61949..76cb68718b4 100644
--- a/migrations/lib/importer/mapping_type.rb
+++ b/migrations/lib/importer/mapping_type.rb
@@ -4,6 +4,7 @@ module Migrations::Importer
module MappingType
USERS = 1
BADGES = 2
+ CATEGORIES = 3
UPLOADS = 10
end
end
diff --git a/migrations/lib/importer/steps/categories.rb b/migrations/lib/importer/steps/categories.rb
new file mode 100644
index 00000000000..e51b25f47cd
--- /dev/null
+++ b/migrations/lib/importer/steps/categories.rb
@@ -0,0 +1,251 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class Categories < ::Migrations::Importer::CopyStep
+ DEFAULT_COLOR = "0088CC"
+ DEFAULT_LIST_FILTER = "all"
+ DEFAULT_TOP_PERIOD = "all"
+ DEFAULT_MINIMUM_REQUIRED_TAGS = 0
+ DEFAULT_STYLE_TYPE = "square"
+ DEFAULT_SUBCATEGORY_LIST_STYLE = "rows_with_featured_topics"
+ DEFAULT_TEXT_COLOR = "FFFFFF"
+ MAX_NAME_LENGTH = 50
+
+ depends_on :users, :uploads
+ store_mapped_ids true
+
+ requires_mapping :ids_by_name, "SELECT name, id FROM categories"
+ requires_set :existing_ids, "SELECT id FROM categories"
+ requires_set :existing_category_names, <<~SQL
+ SELECT COALESCE(parent_category_id::text, '') || '-' || LOWER(name) FROM categories
+ SQL
+ requires_set :existing_slugs, <<~SQL
+ SELECT
+ COALESCE(parent_category_id::text, '') || ':' || LOWER(slug)
+ FROM categories
+ WHERE slug <> ''
+ SQL
+
+ column_names %i[
+ id
+ all_topics_wiki
+ allow_badges
+ allow_global_tags
+ allow_unlimited_owner_edits_on_first_post
+ auto_close_based_on_last_post
+ auto_close_hours
+ color
+ created_at
+ updated_at
+ default_list_filter
+ default_slow_mode_seconds
+ default_top_period
+ default_view
+ description
+ email_in
+ email_in_allow_strangers
+ emoji
+ icon
+ mailinglist_mirror
+ minimum_required_tags
+ name
+ name_lower
+ navigate_to_first_post_after_read
+ num_featured_topics
+ parent_category_id
+ position
+ read_only_banner
+ read_restricted
+ show_subcategory_list
+ slug
+ sort_ascending
+ sort_order
+ style_type
+ subcategory_list_style
+ text_color
+ topic_featured_link_allowed
+ topic_template
+ uploaded_background_dark_id
+ uploaded_background_id
+ uploaded_logo_dark_id
+ uploaded_logo_id
+ user_id
+ ]
+
+ total_rows_query <<~SQL, MappingType::CATEGORIES
+ SELECT COUNT(*)
+ FROM categories c
+ LEFT JOIN mapped.ids mapped_category
+ ON c.original_id = mapped_category.original_id AND mapped_category.type = ?
+ WHERE mapped_category.original_id IS NULL
+ SQL
+
+ rows_query <<~SQL, MappingType::CATEGORIES, MappingType::USERS, MappingType::UPLOADS
+ WITH
+ RECURSIVE
+ tree AS (
+ SELECT c.*, 0 AS level
+ FROM categories c
+ WHERE c.parent_category_id IS NULL
+ UNION ALL
+ SELECT c.*, tree.level + 1 AS level
+ FROM categories c,
+ tree
+ WHERE c.parent_category_id = tree.original_id
+ )
+ SELECT t.*,
+ COALESCE(t.position,
+ ROW_NUMBER() OVER (
+ PARTITION BY parent_category_id
+ ORDER BY parent_category_id NULLS FIRST, name
+ )
+ ) AS position,
+ mapped_user.discourse_id AS mapped_user_id,
+ mapped_bg.discourse_id AS mapped_uploaded_background_id,
+ mapped_bg_dark.discourse_id AS mapped_uploaded_background_dark_id,
+ mapped_logo.discourse_id AS mapped_uploaded_logo_id,
+ mapped_logo_dark.discourse_id AS mapped_uploaded_logo_dark_id
+ FROM tree t
+ LEFT JOIN mapped.ids mapped_category
+ ON t.original_id = mapped_category.original_id AND mapped_category.type = ?1
+ LEFT JOIN mapped.ids mapped_user
+ ON t.user_id = mapped_user.original_id AND mapped_user.type = ?2
+ LEFT JOIN mapped.ids mapped_bg
+ ON t.uploaded_background_id = mapped_bg.original_id AND mapped_bg.type = ?3
+ LEFT JOIN mapped.ids mapped_bg_dark
+ ON t.uploaded_background_dark_id = mapped_bg_dark.original_id AND mapped_bg_dark.type = ?3
+ LEFT JOIN mapped.ids mapped_logo
+ ON t.uploaded_logo_id = mapped_logo.original_id AND mapped_logo.type = ?3
+ LEFT JOIN mapped.ids mapped_logo_dark
+ ON t.uploaded_logo_dark_id = mapped_logo_dark.original_id AND mapped_logo_dark.type = ?3
+ WHERE mapped_category.original_id IS NULL
+ ORDER BY t.level, position, t.original_id
+ SQL
+
+ def execute
+ @highest_position = Category.unscoped.maximum(:position) || 0
+ @mapped_category_ids = @intermediate_db.query_array(<<~SQL, MappingType::CATEGORIES).to_h
+ SELECT original_id, discourse_id FROM mapped.ids WHERE type = ?
+ SQL
+
+ super
+ end
+
+ private
+
+ def transform_row(row)
+ if (existing_id = row[:existing_id])
+ row[:id] = resolve_existing_id(existing_id)
+
+ return nil if row[:id].present?
+ end
+
+ row[:all_topics_wiki] ||= false
+ row[:allow_global_tags] ||= false
+ row[:allow_unlimited_owner_edits_on_first_post] ||= false
+ row[:auto_close_based_on_last_post] ||= false
+ row[:email_in_allow_strangers] ||= false
+ row[:mailinglist_mirror] ||= false
+ row[:navigate_to_first_post_after_read] ||= false
+ row[:read_restricted] ||= false
+ row[:show_subcategory_list] ||= false
+
+ row[:allow_badges] = true if row[:allow_badges].nil?
+ row[:topic_featured_link_allowed] = true if row[:topic_featured_link_allowed].nil?
+
+ row[:color] ||= DEFAULT_COLOR
+ row[:description] = (row[:description] || "").scrub.strip.presence
+ row[:default_list_filter] ||= DEFAULT_LIST_FILTER
+ row[:default_top_period] ||= DEFAULT_TOP_PERIOD
+ row[:minimum_required_tags] ||= DEFAULT_MINIMUM_REQUIRED_TAGS
+ row[:parent_category_id] = @mapped_category_ids[row[:parent_category_id]]
+ row[:name], row[:name_lower] = ensure_unique_name(row[:name], row[:parent_category_id])
+ row[:slug] = ensure_unique_slug(row[:slug], row[:parent_category_id], row[:name_lower])
+
+ row[:style_type] ||= DEFAULT_STYLE_TYPE
+ row[:subcategory_list_style] ||= DEFAULT_SUBCATEGORY_LIST_STYLE
+ row[:text_color] ||= DEFAULT_TEXT_COLOR
+ row[:user_id] = row[:mapped_user_id] || Discourse::SYSTEM_USER_ID
+ row[:uploaded_background_dark_id] = row[:mapped_uploaded_background_dark_id]
+ row[:uploaded_background_id] = row[:mapped_uploaded_background_id]
+ row[:uploaded_logo_dark_id] = row[:mapped_uploaded_logo_dark_id]
+ row[:uploaded_logo_id] = row[:mapped_uploaded_logo_id]
+
+ if row[:position]
+ @highest_position = row[:position] if row[:position] > @highest_position
+ else
+ row[:position] = @highest_position += 1
+ end
+
+ super
+
+ # `parent_category_id` is self-referential. In addition to loading existing category IDs
+ # at the start of the step, we also need to track mapped IDs created during the step
+ @mapped_category_ids[row[:original_id]] = row[:id] unless @mapped_category_ids.key?(
+ row[:original_id],
+ )
+
+ row
+ end
+
+ def resolve_existing_id(existing_id)
+ return if existing_id.blank?
+
+ case existing_id
+ when /\A\d+\z/
+ id = existing_id.to_i
+ id if @existing_ids.include?(id)
+ when /_id\z/
+ begin
+ SiteSetting.get(existing_id)
+ rescue StandardError => e
+ # TODO(selase): Adopt importer framework warning implementation once available
+ puts " #{e.message}"
+ nil
+ end
+ else
+ @ids_by_name[existing_id]
+ end
+ end
+
+ def ensure_unique_name(original_name, parent_id)
+ truncated_name = original_name[0...MAX_NAME_LENGTH].scrub.strip
+ name_lower = truncated_name.downcase
+
+ if @existing_category_names.add?("#{parent_id}-#{name_lower}")
+ return truncated_name, name_lower
+ end
+
+ counter = 1
+ loop do
+ suffix = counter.to_s
+ trim_length = MAX_NAME_LENGTH - suffix.length
+
+ candidate_name = "#{truncated_name[0...trim_length]}#{suffix}"
+ candidate_name_lower = candidate_name.downcase
+
+ if @existing_category_names.add?("#{parent_id}-#{candidate_name_lower}")
+ return candidate_name, candidate_name_lower
+ end
+
+ counter += 1
+ end
+ end
+
+ def ensure_unique_slug(slug, parent_id, name_lower)
+ # `name_lower` is already deduplicated,
+ # safe to use directly as fallback without deduplication
+ return Slug.for(name_lower, "") if slug.blank?
+
+ parent_prefix = "#{parent_id}:"
+ slug_lower = slug.downcase
+
+ return slug if @existing_slugs.add?("#{parent_prefix}#{slug_lower}")
+
+ new_slug = "#{slug_lower}-1"
+ new_slug.next! until @existing_slugs.add?("#{parent_prefix}#{new_slug}")
+
+ new_slug
+ end
+ end
+end
diff --git a/migrations/lib/importer/steps/category_upload_references.rb b/migrations/lib/importer/steps/category_upload_references.rb
new file mode 100644
index 00000000000..0f83086d7ab
--- /dev/null
+++ b/migrations/lib/importer/steps/category_upload_references.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class CategoryUploadReferences < ::Migrations::Importer::Step
+ depends_on :categories
+
+ def execute
+ super
+
+ DB.exec(<<~SQL)
+ INSERT INTO upload_references (upload_id, target_type, target_id, created_at, updated_at)
+ SELECT upload_id, 'Category', target_id, created_at, updated_at
+ FROM (
+ SELECT uploaded_logo_id AS upload_id, id AS target_id, created_at, updated_at
+ FROM categories
+ WHERE uploaded_logo_id IS NOT NULL
+ UNION ALL
+ SELECT uploaded_logo_dark_id AS upload_id, id AS target_id, created_at, updated_at
+ FROM categories
+ WHERE uploaded_logo_dark_id IS NOT NULL
+ UNION ALL
+ SELECT uploaded_background_id AS upload_id, id AS target_id, created_at, updated_at
+ FROM categories
+ WHERE uploaded_background_id IS NOT NULL
+ UNION ALL
+ SELECT uploaded_background_dark_id AS upload_id, id AS target_id, created_at, updated_at
+ FROM categories
+ WHERE uploaded_background_dark_id IS NOT NULL
+ ) AS category_upload_refs
+ ON CONFLICT DO NOTHING
+ SQL
+ end
+ end
+end
diff --git a/migrations/spec/lib/database/intermediate_db/category_spec.rb b/migrations/spec/lib/database/intermediate_db/category_spec.rb
new file mode 100644
index 00000000000..d370401ba3c
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/category_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::Category do
+ it_behaves_like "a database entity"
+end