Skip to content

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

DEV: Add support for converting and importing `topic_allowed_groups` (#35039)

Selase Krakani2d7d6c2ee9e6
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index f0621121549..f23efc03456 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -113,6 +113,11 @@ schema:
- "public_topic_count"
- "staff_topic_count"
- "target_tag_id"
+ topic_allowed_groups:
+ primary_key_column_names: [ "topic_id", "group_id" ]
+ columns:
+ exclude:
+ - "id"
topic_allowed_users:
primary_key_column_names: [ "topic_id", "user_id" ]
columns:
@@ -452,7 +457,6 @@ schema:
- "theme_translation_overrides"
- "themes"
- "top_topics"
- - "topic_allowed_groups"
- "topic_custom_fields"
- "topic_embeds"
- "topic_groups"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index 2e7af250f58..9dec69661c8 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -191,6 +191,13 @@ CREATE TABLE tags
name TEXT NOT NULL
);
+CREATE TABLE topic_allowed_groups
+(
+ group_id NUMERIC NOT NULL,
+ topic_id NUMERIC NOT NULL,
+ PRIMARY KEY (topic_id, group_id)
+);
+
CREATE TABLE topic_allowed_users
(
topic_id NUMERIC NOT NULL,
diff --git a/migrations/lib/converters/discourse/steps/topic_allowed_groups.rb b/migrations/lib/converters/discourse/steps/topic_allowed_groups.rb
new file mode 100644
index 00000000000..c4ddd9d8d99
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/topic_allowed_groups.rb
@@ -0,0 +1,25 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class TopicAllowedGroups < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*) FROM topic_allowed_groups
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT *
+ FROM topic_allowed_groups
+ ORDER BY topic_id, group_id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::TopicAllowedGroup.create(topic_id: item[:topic_id], group_id: item[:group_id])
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/topic_allowed_group.rb b/migrations/lib/database/intermediate_db/topic_allowed_group.rb
new file mode 100644
index 00000000000..33efcbc0c0d
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/topic_allowed_group.rb
@@ -0,0 +1,24 @@
+# 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 TopicAllowedGroup
+ SQL = <<~SQL
+ INSERT INTO topic_allowed_groups (
+ group_id,
+ topic_id
+ )
+ VALUES (
+ ?, ?
+ )
+ SQL
+ private_constant :SQL
+
+ def self.create(group_id:, topic_id:)
+ ::Migrations::Database::IntermediateDB.insert(SQL, group_id, topic_id)
+ end
+ end
+end
diff --git a/migrations/lib/importer/steps/topic_allowed_groups.rb b/migrations/lib/importer/steps/topic_allowed_groups.rb
new file mode 100644
index 00000000000..e7f12fbfbff
--- /dev/null
+++ b/migrations/lib/importer/steps/topic_allowed_groups.rb
@@ -0,0 +1,51 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class TopicAllowedGroups < ::Migrations::Importer::CopyStep
+ depends_on :topics, :groups
+
+ requires_set :existing_topic_allowed_groups,
+ "SELECT topic_id, group_id FROM topic_allowed_groups"
+
+ column_names %i[topic_id group_id]
+
+ total_rows_query <<~SQL, MappingType::TOPICS, MappingType::GROUPS, Archetype.private_message
+ SELECT COUNT(*)
+ FROM topic_allowed_groups
+ JOIN mapped.ids mapped_topic
+ ON topic_allowed_groups.topic_id = mapped_topic.original_id AND mapped_topic.type = ?1
+ JOIN mapped.ids mapped_group
+ ON topic_allowed_groups.group_id = mapped_group.original_id AND mapped_group.type = ?2
+ JOIN topics
+ ON topic_allowed_groups.topic_id = topics.original_id AND topics.archetype = ?3
+ SQL
+
+ rows_query <<~SQL, MappingType::TOPICS, MappingType::GROUPS, Archetype.private_message
+ SELECT topic_allowed_groups.*,
+ mapped_topic.discourse_id AS discourse_topic_id,
+ mapped_group.discourse_id AS discourse_group_id
+ FROM topic_allowed_groups
+ JOIN mapped.ids mapped_topic
+ ON topic_allowed_groups.topic_id = mapped_topic.original_id AND mapped_topic.type = ?1
+ JOIN mapped.ids mapped_group
+ ON topic_allowed_groups.group_id = mapped_group.original_id AND mapped_group.type = ?2
+ JOIN topics
+ ON topic_allowed_groups.topic_id = topics.original_id AND topics.archetype = ?3
+ ORDER BY topic_allowed_groups.topic_id, topic_allowed_groups.group_id
+ SQL
+
+ private
+
+ def transform_row(row)
+ topic_id = row[:discourse_topic_id]
+ group_id = row[:discourse_group_id]
+
+ return nil unless @existing_topic_allowed_groups.add?(topic_id, group_id)
+
+ row[:topic_id] = topic_id
+ row[:group_id] = group_id
+
+ super
+ end
+ end
+end