Skip to content

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

DEV: Add support for converting and importing `tag_groups` (#33991)

Selase Krakanib7459203ecbb
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index a121611e2cc..964261230b1 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -89,6 +89,7 @@ schema:
columns:
exclude:
- "id"
+ tag_groups: {}
tags:
columns:
exclude:
@@ -345,7 +346,6 @@ schema:
- "summary_sections"
- "tag_group_memberships"
- "tag_group_permissions"
- - "tag_groups"
- "tag_search_data"
- "tag_users"
- "tags_web_hooks"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index dd20e3fd1ba..cd380af147d 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -139,6 +139,15 @@ CREATE TABLE muted_users
user_id NUMERIC NOT NULL
);
+CREATE TABLE tag_groups
+(
+ original_id NUMERIC NOT NULL PRIMARY KEY,
+ created_at DATETIME,
+ name TEXT NOT NULL,
+ one_per_topic BOOLEAN,
+ parent_tag_id NUMERIC
+);
+
CREATE TABLE tags
(
original_id NUMERIC NOT NULL PRIMARY KEY,
diff --git a/migrations/lib/converters/discourse/steps/tag_groups.rb b/migrations/lib/converters/discourse/steps/tag_groups.rb
new file mode 100644
index 00000000000..78368e565df
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/tag_groups.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class TagGroups < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*) FROM tag_groups
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT * FROM tag_groups
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::TagGroup.create(
+ original_id: item[:id],
+ created_at: item[:created_at],
+ name: item[:name],
+ one_per_topic: item[:one_per_topic],
+ parent_tag_id: item[:parent_tag_id],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/tag_group.rb b/migrations/lib/database/intermediate_db/tag_group.rb
new file mode 100644
index 00000000000..95c30803d12
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/tag_group.rb
@@ -0,0 +1,33 @@
+# 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 TagGroup
+ SQL = <<~SQL
+ INSERT INTO tag_groups (
+ original_id,
+ created_at,
+ name,
+ one_per_topic,
+ parent_tag_id
+ )
+ VALUES (
+ ?, ?, ?, ?, ?
+ )
+ SQL
+
+ def self.create(original_id:, created_at: nil, name:, one_per_topic: nil, parent_tag_id: nil)
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ original_id,
+ ::Migrations::Database.format_datetime(created_at),
+ name,
+ ::Migrations::Database.format_boolean(one_per_topic),
+ parent_tag_id,
+ )
+ end
+ end
+end
diff --git a/migrations/lib/importer/mapping_type.rb b/migrations/lib/importer/mapping_type.rb
index aebcfa07d50..e023eb81896 100644
--- a/migrations/lib/importer/mapping_type.rb
+++ b/migrations/lib/importer/mapping_type.rb
@@ -7,6 +7,7 @@ module Migrations::Importer
CATEGORIES = 3
GROUPS = 4
TAGS = 5
+ TAG_GROUPS = 7
UPLOADS = 10
end
end
diff --git a/migrations/lib/importer/steps/tag_groups.rb b/migrations/lib/importer/steps/tag_groups.rb
new file mode 100644
index 00000000000..2b38506921e
--- /dev/null
+++ b/migrations/lib/importer/steps/tag_groups.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class TagGroups < ::Migrations::Importer::CopyStep
+ MAX_NAME_LENGTH = 100
+
+ depends_on :tags
+ store_mapped_ids true
+
+ requires_mapping :existing_tag_group_by_name, "SELECT LOWER(name), id FROM tag_groups"
+
+ column_names %i[id name one_per_topic created_at updated_at parent_tag_id]
+
+ total_rows_query <<~SQL, MappingType::TAG_GROUPS
+ SELECT COUNT(*)
+ FROM tag_groups
+ LEFT JOIN mapped.ids mapped_tag_group
+ ON tag_groups.original_id = mapped_tag_group.original_id
+ AND mapped_tag_group.type = ?
+ WHERE mapped_tag_group.original_id IS NULL
+ SQL
+
+ rows_query <<~SQL, MappingType::TAG_GROUPS, MappingType::TAGS
+ SELECT tag_groups.*,
+ mapped_tag.discourse_id AS discourse_parent_tag_id
+ FROM tag_groups
+ LEFT JOIN mapped.ids mapped_tag_group
+ ON tag_groups.original_id = mapped_tag_group.original_id
+ AND mapped_tag_group.type = ?1
+ LEFT JOIN mapped.ids mapped_tag
+ ON tag_groups.parent_tag_id = mapped_tag.original_id AND mapped_tag.type = ?2
+ WHERE mapped_tag_group.original_id IS NULL
+ ORDER BY tag_groups.original_id
+ SQL
+
+ def before(total_rows:)
+ SiteSetting.tags_listed_by_group = true if total_rows.positive?
+ end
+
+ private
+
+ def transform_row(row)
+ name = row[:name]
+
+ return nil if (row[:id] = @existing_tag_group_by_name[name.downcase])
+
+ row[:one_per_topic] ||= false
+ row[:name] = name.length > MAX_NAME_LENGTH ? name[0, MAX_NAME_LENGTH] : name
+ row[:parent_tag_id] = row[:discourse_parent_tag_id]
+
+ super
+ end
+ end
+end
diff --git a/migrations/spec/lib/database/intermediate_db/tag_group_spec.rb b/migrations/spec/lib/database/intermediate_db/tag_group_spec.rb
new file mode 100644
index 00000000000..848c35b358b
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/tag_group_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::TagGroup do
+ it_behaves_like "a database entity"
+end