A platform for community discussion. Free, open, simple.
DEV: Add `category_users` converter and importer steps (#33367)
Selase Krakani8253b147329f
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.ymlindex 12852b8f1c7..4a24e150534 100644--- a/migrations/config/intermediate_db.yml+++ b/migrations/config/intermediate_db.yml@@ -38,6 +38,11 @@ schema:- "topics_week"- "topics_year"- "reviewable_by_group_id"+ category_users:+ primary_key_column_names: [ "category_id", "user_id" ]+ columns:+ exclude:+ - "id"user_emails:columns:include:@@ -115,7 +120,6 @@ schema:- "category_tag_groups"- "category_tag_stats"- "category_tags"- - "category_users"- "chat_channel_archives"- "chat_channel_custom_fields"- "chat_channels"diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sqlindex d17e5c6d1f9..6f36f77e610 100644--- a/migrations/db/intermediate_db_schema/100-base-schema.sql+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql@@ -76,6 +76,15 @@ CREATE TABLE categoriesuser_id NUMERIC NOT NULL);+CREATE TABLE category_users+(+ category_id NUMERIC NOT NULL,+ user_id NUMERIC NOT NULL,+ last_seen_at DATETIME,+ notification_level INTEGER NOT NULL,+ PRIMARY KEY (category_id, user_id)+);+CREATE TABLE user_emails(email TEXT NOT NULL,diff --git a/migrations/lib/converters/discourse/steps/category_users.rb b/migrations/lib/converters/discourse/steps/category_users.rbnew file mode 100644index 00000000000..8d4fa132fb2--- /dev/null+++ b/migrations/lib/converters/discourse/steps/category_users.rb@@ -0,0 +1,32 @@+# frozen_string_literal: true++module Migrations::Converters::Discourse+ class CategoryUsers < ::Migrations::Converters::Base::ProgressStep+ attr_accessor :source_db++ def max_progress+ @source_db.count <<~SQL+ SELECT COUNT(*)+ FROM category_users+ WHERE user_id >= 0+ SQL+ end++ def items+ @source_db.query <<~SQL+ SELECT *+ FROM category_users+ WHERE user_id >= 0+ SQL+ end++ def process_item(item)+ IntermediateDB::CategoryUser.create(+ category_id: item[:category_id],+ last_seen_at: item[:last_seen_at],+ notification_level: item[:notification_level],+ user_id: item[:user_id],+ )+ end+ end+enddiff --git a/migrations/lib/database/intermediate_db/category_user.rb b/migrations/lib/database/intermediate_db/category_user.rbnew file mode 100644index 00000000000..8867dc114b4--- /dev/null+++ b/migrations/lib/database/intermediate_db/category_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 CategoryUser+ SQL = <<~SQL+ INSERT INTO category_users (+ category_id,+ user_id,+ last_seen_at,+ notification_level+ )+ VALUES (+ ?, ?, ?, ?+ )+ SQL++ def self.create(category_id:, user_id:, last_seen_at: nil, notification_level:)+ ::Migrations::Database::IntermediateDB.insert(+ SQL,+ category_id,+ user_id,+ ::Migrations::Database.format_datetime(last_seen_at),+ notification_level,+ )+ end+ end+enddiff --git a/migrations/lib/importer/steps/category_users.rb b/migrations/lib/importer/steps/category_users.rbnew file mode 100644index 00000000000..1c36e2c53f5--- /dev/null+++ b/migrations/lib/importer/steps/category_users.rb@@ -0,0 +1,62 @@+# frozen_string_literal: true++module Migrations::Importer::Steps+ class CategoryUsers < ::Migrations::Importer::CopyStep+ DEFAULT_NOTIFICATION_LEVEL = CategoryUser.default_notification_level+ VALID_NOTIFICATION_LEVELS = CategoryUser.notification_levels.values.to_set.freeze++ depends_on :categories, :users++ column_names %i[category_id last_seen_at notification_level user_id]++ total_rows_query <<~SQL, MappingType::CATEGORIES, MappingType::USERS+ SELECT COUNT(*)+ FROM category_users+ JOIN mapped.ids mapped_categories+ ON category_users.category_id = mapped_categories.original_id AND mapped_categories.type = ?1+ JOIN mapped.ids mapped_users+ ON category_users.user_id = mapped_users.original_id AND mapped_users.type = ?2+ SQL++ rows_query <<~SQL, MappingType::CATEGORIES, MappingType::USERS+ SELECT category_users.*,+ mapped_categories.discourse_id AS discourse_category_id,+ mapped_users.discourse_id AS discourse_user_id+ FROM category_users+ JOIN mapped.ids mapped_categories+ ON category_users.category_id = mapped_categories.original_id AND mapped_categories.type = ?1+ JOIN mapped.ids mapped_users+ ON category_users.user_id = mapped_users.original_id AND mapped_users.type = ?2+ ORDER BY discourse_category_id, discourse_user_id+ SQL++ def execute+ @existing_category_users = Hash.new { |h, k| h[k] = Set.new }++ @discourse_db+ .query_array("SELECT category_id, user_id FROM category_users WHERE user_id >= 0")+ .each { |row| @existing_category_users[row[0]].add(row[1]) }++ super+ end++ private++ def transform_row(row)+ category_id = row[:discourse_category_id]+ user_id = row[:discourse_user_id]++ return nil unless @existing_category_users[category_id].add?(user_id)++ row[:category_id] = category_id+ row[:user_id] = user_id+ row[:notification_level] = ensure_valid_value(+ value: row[:notification_level],+ allowed_set: VALID_NOTIFICATION_LEVELS,+ default_value: DEFAULT_NOTIFICATION_LEVEL,+ )++ super+ end+ end+enddiff --git a/migrations/spec/lib/database/intermediate_db/category_user_spec.rb b/migrations/spec/lib/database/intermediate_db/category_user_spec.rbnew file mode 100644index 00000000000..2e5db7bfed6--- /dev/null+++ b/migrations/spec/lib/database/intermediate_db/category_user_spec.rb@@ -0,0 +1,5 @@+# frozen_string_literal: true++RSpec.describe ::Migrations::Database::IntermediateDB::CategoryUser do+ it_behaves_like "a database entity"+end