Skip to content

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

DEV: Add support for converting and importing `tag_users` (#34101)

Selase Krakani713b4088af64
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index 2bb174fa8de..b15ea3d3237 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -101,6 +101,11 @@ schema:
exclude:
- "id"
tag_groups: {}
+ tag_users:
+ primary_key_column_names: [ "tag_id", "user_id" ]
+ columns:
+ exclude:
+ - "id"
tags:
columns:
exclude:
@@ -355,7 +360,6 @@ schema:
- "stylesheet_cache"
- "summary_sections"
- "tag_search_data"
- - "tag_users"
- "tags_web_hooks"
- "theme_fields"
- "theme_modifier_sets"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index e10eb19c190..45683388fb7 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -174,6 +174,15 @@ CREATE TABLE tag_groups
parent_tag_id NUMERIC
);
+CREATE TABLE tag_users
+(
+ tag_id NUMERIC NOT NULL,
+ user_id NUMERIC NOT NULL,
+ created_at DATETIME,
+ notification_level INTEGER NOT NULL,
+ PRIMARY KEY (tag_id, user_id)
+);
+
CREATE TABLE tags
(
original_id NUMERIC NOT NULL PRIMARY KEY,
diff --git a/migrations/lib/converters/discourse/steps/tag_users.rb b/migrations/lib/converters/discourse/steps/tag_users.rb
new file mode 100644
index 00000000000..8c47255f92f
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/tag_users.rb
@@ -0,0 +1,32 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class TagUsers < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*)
+ FROM tag_users
+ WHERE user_id >= 0
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT * FROM tag_users
+ WHERE user_id >= 0
+ ORDER BY tag_id, user_id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::TagUser.create(
+ tag_id: item[:tag_id],
+ user_id: item[:user_id],
+ notification_level: item[:notification_level],
+ created_at: item[:created_at],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/tag_user.rb b/migrations/lib/database/intermediate_db/tag_user.rb
new file mode 100644
index 00000000000..8950a030905
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/tag_user.rb
@@ -0,0 +1,31 @@
+# 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 TagUser
+ SQL = <<~SQL
+ INSERT INTO tag_users (
+ tag_id,
+ user_id,
+ created_at,
+ notification_level
+ )
+ VALUES (
+ ?, ?, ?, ?
+ )
+ SQL
+
+ def self.create(tag_id:, user_id:, created_at: nil, notification_level:)
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ tag_id,
+ user_id,
+ ::Migrations::Database.format_datetime(created_at),
+ notification_level,
+ )
+ end
+ end
+end
diff --git a/migrations/lib/importer/steps/tag_users.rb b/migrations/lib/importer/steps/tag_users.rb
new file mode 100644
index 00000000000..fd39d2b04f0
--- /dev/null
+++ b/migrations/lib/importer/steps/tag_users.rb
@@ -0,0 +1,54 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class TagUsers < ::Migrations::Importer::CopyStep
+ NOTIFICATION_LEVELS = TagUser.notification_levels.values.to_set.freeze
+ DEFAULT_NOTIFICATION_LEVEL = TagUser.notification_levels[:regular]
+
+ depends_on :tags, :users
+
+ requires_set :existing_tag_users, "SELECT tag_id, user_id FROM tag_users"
+
+ column_names %i[tag_id user_id notification_level created_at updated_at]
+
+ total_rows_query <<~SQL, MappingType::TAGS, MappingType::USERS
+ SELECT COUNT(*)
+ FROM tag_users
+ JOIN mapped.ids mapped_tag
+ ON tag_users.tag_id = mapped_tag.original_id AND mapped_tag.type = ?1
+ JOIN mapped.ids mapped_user
+ ON tag_users.user_id = mapped_user.original_id AND mapped_user.type = ?2
+ SQL
+
+ rows_query <<~SQL, MappingType::TAGS, MappingType::USERS
+ SELECT tag_users.*,
+ mapped_tag.discourse_id AS discourse_tag_id,
+ mapped_user.discourse_id AS discourse_user_id
+ FROM tag_users
+ JOIN mapped.ids mapped_tag
+ ON tag_users.tag_id = mapped_tag.original_id AND mapped_tag.type = ?1
+ JOIN mapped.ids mapped_user
+ ON tag_users.user_id = mapped_user.original_id AND mapped_user.type = ?2
+ ORDER BY tag_id, user_id
+ SQL
+
+ private
+
+ def transform_row(row)
+ tag_id = row[:discourse_tag_id]
+ user_id = row[:discourse_user_id]
+
+ return nil unless @existing_tag_users.add?(tag_id, user_id)
+
+ row[:tag_id] = tag_id
+ row[:user_id] = user_id
+ row[:notification_level] = ensure_valid_value(
+ value: row[:notification_level],
+ allowed_set: NOTIFICATION_LEVELS,
+ default_value: DEFAULT_NOTIFICATION_LEVEL,
+ )
+
+ super
+ end
+ end
+end
diff --git a/migrations/spec/lib/database/intermediate_db/tag_user_spec.rb b/migrations/spec/lib/database/intermediate_db/tag_user_spec.rb
new file mode 100644
index 00000000000..67261231c13
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/tag_user_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::TagUser do
+ it_behaves_like "a database entity"
+end