Skip to content

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

DEV: Add support for converting and importing `category_moderation_groups`

Italo Alves02dfb651aedf
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index ae9ded3f0d5..6d7bda799e8 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -52,6 +52,13 @@ schema:
exclude:
- "created_at"
- "id"
+ category_moderation_groups:
+ primary_key_column_names: [ "category_id", "group_id" ]
+ columns:
+ exclude:
+ - "created_at"
+ - "updated_at"
+ - "id"
group_users:
primary_key_column_names: [ "group_id", "user_id" ]
columns:
@@ -313,7 +320,6 @@ schema:
- "category_form_templates"
- "category_groups"
- "category_localizations"
- - "category_moderation_groups"
- "category_required_tag_groups"
- "category_search_data"
- "category_settings"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index c2854668348..61049ca12d5 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -93,6 +93,13 @@ CREATE TABLE category_custom_fields
PRIMARY KEY (category_id, name)
);
+CREATE TABLE category_moderation_groups
+(
+ category_id NUMERIC,
+ group_id NUMERIC,
+ PRIMARY KEY (category_id, group_id)
+);
+
CREATE TABLE category_users
(
category_id NUMERIC NOT NULL,
diff --git a/migrations/lib/converters/discourse/steps/category_moderation_groups.rb b/migrations/lib/converters/discourse/steps/category_moderation_groups.rb
new file mode 100644
index 00000000000..57d2c94b391
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/category_moderation_groups.rb
@@ -0,0 +1,30 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class CategoryModerationGroups < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*)
+ FROM category_moderation_groups
+ WHERE group_id > 0
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT *
+ FROM category_moderation_groups
+ WHERE group_id > 0
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::CategoryModerationGroup.create(
+ category_id: item[:category_id],
+ group_id: item[:group_id],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/category_moderation_group.rb b/migrations/lib/database/intermediate_db/category_moderation_group.rb
new file mode 100644
index 00000000000..9ca29c65bbd
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/category_moderation_group.rb
@@ -0,0 +1,30 @@
+# 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 CategoryModerationGroup
+ SQL = <<~SQL
+ INSERT INTO category_moderation_groups (
+ category_id,
+ group_id
+ )
+ VALUES (
+ ?, ?
+ )
+ SQL
+ private_constant :SQL
+
+ # Creates a new `category_moderation_groups` record in the IntermediateDB.
+ #
+ # @param category_id [Integer, String, nil]
+ # @param group_id [Integer, String, nil]
+ #
+ # @return [void]
+ def self.create(category_id:, group_id:)
+ ::Migrations::Database::IntermediateDB.insert(SQL, category_id, group_id)
+ end
+ end
+end
diff --git a/migrations/lib/importer/steps/categories/category_moderation_groups.rb b/migrations/lib/importer/steps/categories/category_moderation_groups.rb
new file mode 100644
index 00000000000..81764ff53dc
--- /dev/null
+++ b/migrations/lib/importer/steps/categories/category_moderation_groups.rb
@@ -0,0 +1,47 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class CategoryModerationGroups < ::Migrations::Importer::CopyStep
+ depends_on :categories, :groups
+
+ column_names %i[category_id group_id]
+
+ requires_set :existing_category_moderation_groups,
+ "SELECT category_id, group_id FROM category_moderation_groups WHERE group_id > 0"
+
+ total_rows_query <<~SQL, MappingType::CATEGORIES, MappingType::GROUPS
+ SELECT COUNT(*)
+ FROM category_moderation_groups
+ JOIN mapped.ids mapped_categories
+ ON category_moderation_groups.category_id = mapped_categories.original_id AND mapped_categories.type = ?1
+ JOIN mapped.ids mapped_groups
+ ON category_moderation_groups.group_id = mapped_groups.original_id AND mapped_groups.type = ?2
+ SQL
+
+ rows_query <<~SQL, MappingType::CATEGORIES, MappingType::GROUPS
+ SELECT category_moderation_groups*,
+ mapped_categories.discourse_id AS discourse_category_id,
+ mapped_groups.discourse_id AS discourse_group_id
+ FROM category_moderation_groups
+ JOIN mapped.ids mapped_categories
+ ON category_moderation_groups.category_id = mapped_categories.original_id AND mapped_categories.type = ?1
+ JOIN mapped.ids mapped_groups
+ ON category_moderation_groups.group_id = mapped_groups.original_id AND mapped_groups.type = ?2
+ ORDER BY discourse_category_id, discourse_group_id
+ SQL
+
+ private
+
+ def transform_row(row)
+ category_id = row[:discourse_category_id]
+ group_id = row[:discourse_group_id]
+
+ return nil unless @existing_category_moderation_groups.add?(category_id, group_id)
+
+ row[:category_id] = category_id
+ row[:group_id] = group_id
+
+ super
+ end
+ end
+end