Skip to content

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

DEV: Add support for converting and importing `topic_users` (#35038)

Selase Krakani3ca1f8bf4221
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index 695ff9461f5..a80c084582e 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -123,6 +123,14 @@ schema:
columns:
exclude:
- "id"
+ topic_users:
+ primary_key_column_names: [ "topic_id", "user_id" ]
+ columns:
+ exclude:
+ - "id"
+ - "bookmarked"
+ - "liked"
+ - "posted"
topics:
columns:
exclude:
@@ -473,7 +481,6 @@ schema:
- "topic_search_data"
- "topic_thumbnails"
- "topic_timers"
- - "topic_users"
- "topic_view_stats"
- "topic_views"
- "topic_voting_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 0d6e79f17db..23f6c48a795 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -214,6 +214,23 @@ CREATE TABLE topic_tags
PRIMARY KEY (topic_id, tag_id)
);
+CREATE TABLE topic_users
+(
+ topic_id NUMERIC NOT NULL,
+ user_id NUMERIC NOT NULL,
+ cleared_pinned_at DATETIME,
+ first_visited_at DATETIME,
+ last_emailed_post_number INTEGER,
+ last_posted_at DATETIME,
+ last_read_post_number INTEGER,
+ last_visited_at DATETIME,
+ notification_level INTEGER,
+ notifications_changed_at DATETIME,
+ notifications_reason_id NUMERIC,
+ total_msecs_viewed INTEGER,
+ 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_users.rb b/migrations/lib/converters/discourse/steps/topic_users.rb
new file mode 100644
index 00000000000..7d91c1130cc
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/topic_users.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class TopicUsers < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*)
+ FROM topic_users
+ WHERE user_id >= 0
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT *
+ FROM topic_users
+ WHERE user_id >= 0
+ ORDER BY topic_id, user_id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::TopicUser.create(
+ topic_id: item[:topic_id],
+ user_id: item[:user_id],
+ cleared_pinned_at: item[:cleared_pinned_at],
+ first_visited_at: item[:first_visited_at],
+ last_emailed_post_number: item[:last_emailed_post_number],
+ last_posted_at: item[:last_posted_at],
+ last_read_post_number: item[:last_read_post_number],
+ last_visited_at: item[:last_visited_at],
+ notification_level: item[:notification_level],
+ notifications_changed_at: item[:notifications_changed_at],
+ notifications_reason_id: item[:notifications_reason_id],
+ total_msecs_viewed: item[:total_msecs_viewed],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/topic_user.rb b/migrations/lib/database/intermediate_db/topic_user.rb
new file mode 100644
index 00000000000..3b2bf0045b9
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/topic_user.rb
@@ -0,0 +1,61 @@
+# 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 TopicUser
+ SQL = <<~SQL
+ INSERT INTO topic_users (
+ topic_id,
+ user_id,
+ cleared_pinned_at,
+ first_visited_at,
+ last_emailed_post_number,
+ last_posted_at,
+ last_read_post_number,
+ last_visited_at,
+ notification_level,
+ notifications_changed_at,
+ notifications_reason_id,
+ total_msecs_viewed
+ )
+ VALUES (
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
+ )
+ SQL
+ private_constant :SQL
+
+ def self.create(
+ topic_id:,
+ user_id:,
+ cleared_pinned_at: nil,
+ first_visited_at: nil,
+ last_emailed_post_number: nil,
+ last_posted_at: nil,
+ last_read_post_number: nil,
+ last_visited_at: nil,
+ notification_level: nil,
+ notifications_changed_at: nil,
+ notifications_reason_id: nil,
+ total_msecs_viewed: nil
+ )
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ topic_id,
+ user_id,
+ ::Migrations::Database.format_datetime(cleared_pinned_at),
+ ::Migrations::Database.format_datetime(first_visited_at),
+ last_emailed_post_number,
+ ::Migrations::Database.format_datetime(last_posted_at),
+ last_read_post_number,
+ ::Migrations::Database.format_datetime(last_visited_at),
+ notification_level,
+ ::Migrations::Database.format_datetime(notifications_changed_at),
+ notifications_reason_id,
+ total_msecs_viewed,
+ )
+ end
+ end
+end
diff --git a/migrations/lib/importer/steps/topic_users.rb b/migrations/lib/importer/steps/topic_users.rb
new file mode 100644
index 00000000000..b2935efce72
--- /dev/null
+++ b/migrations/lib/importer/steps/topic_users.rb
@@ -0,0 +1,78 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class TopicUsers < ::Migrations::Importer::CopyStep
+ NOTIFICATION_LEVELS = TopicUser.notification_levels.values.to_set.freeze
+ DEFAULT_NOTIFICATION_LEVEL = TopicUser.notification_levels[:regular]
+ NOTIFICATION_REASONS = TopicUser.notification_reasons.values.to_set.freeze
+ DEFAULT_NOTIFICATION_REASON = TopicUser.notification_reasons[:user_changed]
+
+ depends_on :topics, :users
+
+ requires_set :existing_topic_users,
+ "SELECT topic_id, user_id FROM topic_users WHERE user_id > 0"
+
+ column_names %i[
+ topic_id
+ user_id
+ cleared_pinned_at
+ first_visited_at
+ last_emailed_post_number
+ last_posted_at
+ last_read_post_number
+ last_visited_at
+ notification_level
+ notifications_changed_at
+ notifications_reason_id
+ total_msecs_viewed
+ ]
+
+ total_rows_query <<~SQL, MappingType::TOPICS, MappingType::USERS
+ SELECT COUNT(*)
+ FROM topic_users
+ JOIN mapped.ids mapped_topic
+ ON topic_users.topic_id = mapped_topic.original_id AND mapped_topic.type = ?1
+ JOIN mapped.ids mapped_user
+ ON topic_users.user_id = mapped_user.original_id AND mapped_user.type = ?2
+ SQL
+
+ rows_query <<~SQL, MappingType::TOPICS, MappingType::USERS
+ SELECT topic_users.*,
+ mapped_topic.discourse_id AS discourse_topic_id,
+ mapped_user.discourse_id AS discourse_user_id
+ FROM topic_users
+ JOIN mapped.ids mapped_topic
+ ON topic_users.topic_id = mapped_topic.original_id AND mapped_topic.type = ?1
+ JOIN mapped.ids mapped_user
+ ON topic_users.user_id = mapped_user.original_id AND mapped_user.type = ?2
+ ORDER BY topic_users.topic_id, topic_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_users.add?(topic_id, user_id)
+
+ row[:notification_level] = ensure_valid_value(
+ value: row[:notification_level],
+ allowed_set: NOTIFICATION_LEVELS,
+ default_value: DEFAULT_NOTIFICATION_LEVEL,
+ )
+ row[:notifications_reason_id] = ensure_valid_value(
+ value: row[:notifications_reason_id],
+ allowed_set: NOTIFICATION_REASONS,
+ default_value: DEFAULT_NOTIFICATION_REASON,
+ )
+
+ row[:total_msecs_viewed] ||= 0
+
+ row[:topic_id] = topic_id
+ row[:user_id] = user_id
+
+ super
+ end
+ end
+end