Skip to content

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

DEV: Add `group_users` converter and importer steps (#33687)

Selase Krakanie05582ab5d5f
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index 6a0fdf61d9b..d20309eeb21 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -79,6 +79,12 @@ schema:
- "publish_read_state"
- "title"
- "visibility_level"
+ group_users:
+ primary_key_column_names: [ "group_id", "user_id" ]
+ columns:
+ exclude:
+ - "id"
+ - "first_unread_pm_at"
user_emails:
columns:
include:
@@ -242,7 +248,6 @@ schema:
- "group_mentions"
- "group_requests"
- "group_tag_notification_defaults"
- - "group_users"
- "groups_web_hooks"
- "ignored_users"
- "imap_sync_logs"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index 60b8042d371..60d7402f0ef 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -93,6 +93,16 @@ CREATE TABLE category_users
PRIMARY KEY (category_id, user_id)
);
+CREATE TABLE group_users
+(
+ group_id NUMERIC NOT NULL,
+ user_id NUMERIC NOT NULL,
+ created_at DATETIME,
+ notification_level INTEGER,
+ owner BOOLEAN,
+ PRIMARY KEY (group_id, user_id)
+);
+
CREATE TABLE "groups"
(
original_id NUMERIC NOT NULL PRIMARY KEY,
@@ -149,6 +159,7 @@ CREATE TABLE user_options
chat_separate_sidebar_mode INTEGER,
chat_sound TEXT,
color_scheme_id NUMERIC,
+ composition_mode INTEGER,
dark_scheme_id NUMERIC,
default_calendar INTEGER,
digest_after_minutes INTEGER,
diff --git a/migrations/lib/converters/discourse/steps/group_users.rb b/migrations/lib/converters/discourse/steps/group_users.rb
new file mode 100644
index 00000000000..7b0744f4f7c
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/group_users.rb
@@ -0,0 +1,36 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class GroupUsers < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*)
+ FROM group_users
+ WHERE group_id NOT IN (10, 11, 12, 13, 14) -- Exclude Trust Level groups
+ AND user_id > 0
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT *
+ FROM group_users
+ WHERE group_id NOT IN (10, 11, 12, 13, 14) -- Exclude Trust Level groups
+ AND user_id > 0
+ ORDER BY group_id, user_id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::GroupUser.create(
+ created_at: item[:created_at],
+ group_id: item[:group_id],
+ user_id: item[:user_id],
+ owner: item[:owner],
+ notification_level: item[:notification_level],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/group_user.rb b/migrations/lib/database/intermediate_db/group_user.rb
new file mode 100644
index 00000000000..33a6f18d9cf
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/group_user.rb
@@ -0,0 +1,33 @@
+# 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 GroupUser
+ SQL = <<~SQL
+ INSERT INTO group_users (
+ group_id,
+ user_id,
+ created_at,
+ notification_level,
+ owner
+ )
+ VALUES (
+ ?, ?, ?, ?, ?
+ )
+ SQL
+
+ def self.create(group_id:, user_id:, created_at: nil, notification_level: nil, owner: nil)
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ group_id,
+ user_id,
+ ::Migrations::Database.format_datetime(created_at),
+ notification_level,
+ ::Migrations::Database.format_boolean(owner),
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/user_option.rb b/migrations/lib/database/intermediate_db/user_option.rb
index eb464c59f82..73988300f97 100644
--- a/migrations/lib/database/intermediate_db/user_option.rb
+++ b/migrations/lib/database/intermediate_db/user_option.rb
@@ -24,6 +24,7 @@ module Migrations::Database::IntermediateDB
chat_separate_sidebar_mode,
chat_sound,
color_scheme_id,
+ composition_mode,
dark_scheme_id,
default_calendar,
digest_after_minutes,
@@ -74,7 +75,7 @@ module Migrations::Database::IntermediateDB
VALUES (
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?,
- ?, ?
+ ?, ?, ?
)
SQL
@@ -95,6 +96,7 @@ module Migrations::Database::IntermediateDB
chat_separate_sidebar_mode: nil,
chat_sound: nil,
color_scheme_id: nil,
+ composition_mode: nil,
dark_scheme_id: nil,
default_calendar: nil,
digest_after_minutes: nil,
@@ -160,6 +162,7 @@ module Migrations::Database::IntermediateDB
chat_separate_sidebar_mode,
chat_sound,
color_scheme_id,
+ composition_mode,
dark_scheme_id,
default_calendar,
digest_after_minutes,
diff --git a/migrations/lib/importer/steps/group_users.rb b/migrations/lib/importer/steps/group_users.rb
new file mode 100644
index 00000000000..f7aa494e80b
--- /dev/null
+++ b/migrations/lib/importer/steps/group_users.rb
@@ -0,0 +1,56 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class GroupUsers < ::Migrations::Importer::CopyStep
+ NOTIFICATION_LEVELS = GroupUser.notification_levels.values.to_set.freeze
+ DEFAULT_NOTIFICATION_LEVEL = GroupUser.notification_levels[:tracking]
+
+ depends_on :groups, :users
+
+ requires_set :existing_group_users,
+ "SELECT group_id, user_id FROM group_users WHERE user_id > 0"
+
+ column_names %i[group_id user_id created_at updated_at owner notification_level]
+
+ total_rows_query <<~SQL, MappingType::GROUPS, MappingType::USERS
+ SELECT COUNT(*)
+ FROM group_users
+ JOIN mapped.ids mapped_groups
+ ON group_users.group_id = mapped_groups.original_id AND mapped_groups.type = ?1
+ JOIN mapped.ids mapped_users
+ ON group_users.user_id = mapped_users.original_id AND mapped_users.type = ?2
+ SQL
+
+ rows_query <<~SQL, MappingType::GROUPS, MappingType::USERS
+ SELECT group_users.*,
+ mapped_groups.discourse_id AS discourse_group_id,
+ mapped_users.discourse_id AS discourse_user_id
+ FROM group_users
+ JOIN mapped.ids mapped_groups
+ ON group_users.group_id = mapped_groups.original_id AND mapped_groups.type = ?1
+ JOIN mapped.ids mapped_users
+ ON group_users.user_id = mapped_users.original_id AND mapped_users.type = ?2
+ ORDER BY discourse_group_id, discourse_user_id
+ SQL
+
+ private
+
+ def transform_row(row)
+ group_id = row[:discourse_group_id]
+ user_id = row[:discourse_user_id]
+
+ return nil unless @existing_group_users.add?(group_id, user_id)
+
+ row[:owner] ||= false
+ row[:group_id] = group_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/group_user_spec.rb b/migrations/spec/lib/database/intermediate_db/group_user_spec.rb
new file mode 100644
index 00000000000..718f5e5b40e
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/group_user_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::GroupUser do
+ it_behaves_like "a database entity"
+end