A platform for community discussion. Free, open, simple.
DEV: Add support for converting and importing `topic_allowed_users` (#35040)
Selase Krakani9e85544f72b2
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.ymlindex d54f3115691..f0621121549 100644--- a/migrations/config/intermediate_db.yml+++ b/migrations/config/intermediate_db.yml@@ -113,21 +113,11 @@ schema:- "public_topic_count"- "staff_topic_count"- "target_tag_id"- user_associated_accounts:- primary_key_column_names: [ "user_id", "provider_name" ]- columns:- exclude:- - "id"- - "credentials"- - "extra"- modify:- - name: "last_used"- nullable: true- user_custom_fields:+ topic_allowed_users:+ primary_key_column_names: [ "topic_id", "user_id" ]columns:exclude:- "id"- primary_key_column_names: [ "user_id" , "name", "value" ]topics:columns:exclude:@@ -159,6 +149,21 @@ schema:- "highest_staff_post_number"- "reviewable_score"- "slow_mode_seconds"+ user_associated_accounts:+ primary_key_column_names: [ "user_id", "provider_name" ]+ columns:+ exclude:+ - "id"+ - "credentials"+ - "extra"+ modify:+ - name: "last_used"+ nullable: true+ user_custom_fields:+ columns:+ exclude:+ - "id"+ primary_key_column_names: [ "user_id" , "name", "value" ]user_emails:columns:include:@@ -448,7 +453,6 @@ schema:- "themes"- "top_topics"- "topic_allowed_groups"- - "topic_allowed_users"- "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.sqlindex 3d3dbe56bff..2e7af250f58 100644--- a/migrations/db/intermediate_db_schema/100-base-schema.sql+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql@@ -191,6 +191,14 @@ CREATE TABLE tagsname TEXT NOT NULL);+CREATE TABLE topic_allowed_users+(+ topic_id NUMERIC NOT NULL,+ user_id NUMERIC NOT NULL,+ created_at DATETIME,+ PRIMARY KEY (topic_id, user_id)+);+CREATE TABLE topics(original_id NUMERIC NOT NULL PRIMARY KEY,diff --git a/migrations/lib/converters/discourse/steps/topic_allowed_users.rb b/migrations/lib/converters/discourse/steps/topic_allowed_users.rbnew file mode 100644index 00000000000..4563217765a--- /dev/null+++ b/migrations/lib/converters/discourse/steps/topic_allowed_users.rb@@ -0,0 +1,32 @@+# frozen_string_literal: true++module Migrations::Converters::Discourse+ class TopicAllowedUsers < ::Migrations::Converters::Base::ProgressStep+ attr_accessor :source_db++ def max_progress+ @source_db.count <<~SQL+ SELECT COUNT(*)+ FROM topic_allowed_users+ WHERE user_id >= 0+ SQL+ end++ def items+ @source_db.query <<~SQL+ SELECT *+ FROM topic_allowed_users+ WHERE user_id >= 0+ ORDER BY topic_id, user_id+ SQL+ end++ def process_item(item)+ IntermediateDB::TopicAllowedUser.create(+ topic_id: item[:topic_id],+ user_id: item[:user_id],+ created_at: item[:created_at],+ )+ end+ end+enddiff --git a/migrations/lib/database/intermediate_db/topic_allowed_user.rb b/migrations/lib/database/intermediate_db/topic_allowed_user.rbnew file mode 100644index 00000000000..afb7b2d280f--- /dev/null+++ b/migrations/lib/database/intermediate_db/topic_allowed_user.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 TopicAllowedUser+ SQL = <<~SQL+ INSERT INTO topic_allowed_users (+ topic_id,+ user_id,+ created_at+ )+ VALUES (+ ?, ?, ?+ )+ SQL+ private_constant :SQL++ def self.create(topic_id:, user_id:, created_at: nil)+ ::Migrations::Database::IntermediateDB.insert(+ SQL,+ topic_id,+ user_id,+ ::Migrations::Database.format_datetime(created_at),+ )+ end+ end+enddiff --git a/migrations/lib/importer/steps/topic_allowed_users.rb b/migrations/lib/importer/steps/topic_allowed_users.rbnew file mode 100644index 00000000000..b6768a7b36a--- /dev/null+++ b/migrations/lib/importer/steps/topic_allowed_users.rb@@ -0,0 +1,51 @@+# frozen_string_literal: true++module Migrations::Importer::Steps+ class TopicAllowedUsers < ::Migrations::Importer::CopyStep+ depends_on :topics, :users++ requires_set :existing_topic_allowed_users,+ "SELECT topic_id, user_id FROM topic_allowed_users WHERE user_id > 0"++ column_names %i[topic_id user_id created_at updated_at]++ total_rows_query <<~SQL, MappingType::TOPICS, MappingType::USERS, Archetype.private_message+ SELECT COUNT(*)+ FROM topic_allowed_users+ JOIN mapped.ids mapped_topic+ ON topic_allowed_users.topic_id = mapped_topic.original_id AND mapped_topic.type = ?1+ JOIN mapped.ids mapped_user+ ON topic_allowed_users.user_id = mapped_user.original_id AND mapped_user.type = ?2+ JOIN topics+ ON topic_allowed_users.topic_id = topics.original_id AND topics.archetype = ?3+ SQL++ rows_query <<~SQL, MappingType::TOPICS, MappingType::USERS, Archetype.private_message+ SELECT topic_allowed_users.*,+ mapped_topic.discourse_id AS discourse_topic_id,+ mapped_user.discourse_id AS discourse_user_id+ FROM topic_allowed_users+ JOIN mapped.ids mapped_topic+ ON topic_allowed_users.topic_id = mapped_topic.original_id AND mapped_topic.type = ?1+ JOIN mapped.ids mapped_user+ ON topic_allowed_users.user_id = mapped_user.original_id AND mapped_user.type = ?2+ JOIN topics+ ON topic_allowed_users.topic_id = topics.original_id AND topics.archetype = ?3+ ORDER BY topic_allowed_users.topic_id, topic_allowed_users.user_id+ SQL++ private++ def transform_row(row)+ topic_id = row[:discourse_topic_id]+ user_id = row[:discourse_user_id]++ return nil unless @existing_topic_allowed_users.add?(topic_id, user_id)++ row[:topic_id] = topic_id+ row[:user_id] = user_id++ super+ end+ end+end