Skip to content

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

DEV: Add `groups` converter and importer steps (#33373)

Selase Krakania5c0326da1a0
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index 7ebce368e24..6a0fdf61d9b 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -49,6 +49,36 @@ schema:
exclude:
- "created_at"
- "id"
+ groups:
+ columns:
+ add:
+ - name: "existing_id"
+ datatype: numeric
+ include:
+ - "allow_membership_requests"
+ - "allow_unknown_sender_topic_replies"
+ - "automatic_membership_email_domains"
+ - "bio_raw"
+ - "created_at"
+ - "default_notification_level"
+ - "flair_bg_color"
+ - "flair_color"
+ - "flair_icon"
+ - "flair_upload_id"
+ - "full_name"
+ - "grant_trust_level"
+ - "id"
+ - "members_visibility_level"
+ - "membership_request_template"
+ - "mentionable_level"
+ - "messageable_level"
+ - "name"
+ - "primary_group"
+ - "public_admission"
+ - "public_exit"
+ - "publish_read_state"
+ - "title"
+ - "visibility_level"
user_emails:
columns:
include:
@@ -213,7 +243,6 @@ schema:
- "group_requests"
- "group_tag_notification_defaults"
- "group_users"
- - "groups"
- "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 5647dc521da..60b8042d371 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -93,6 +93,35 @@ CREATE TABLE category_users
PRIMARY KEY (category_id, user_id)
);
+CREATE TABLE "groups"
+(
+ original_id NUMERIC NOT NULL PRIMARY KEY,
+ allow_membership_requests BOOLEAN,
+ allow_unknown_sender_topic_replies BOOLEAN,
+ automatic_membership_email_domains TEXT,
+ bio_raw TEXT,
+ created_at DATETIME,
+ default_notification_level INTEGER,
+ existing_id NUMERIC,
+ flair_bg_color TEXT,
+ flair_color TEXT,
+ flair_icon TEXT,
+ flair_upload_id TEXT,
+ full_name TEXT,
+ grant_trust_level INTEGER,
+ members_visibility_level INTEGER,
+ membership_request_template TEXT,
+ mentionable_level INTEGER,
+ messageable_level INTEGER,
+ name TEXT NOT NULL,
+ primary_group BOOLEAN,
+ public_admission BOOLEAN,
+ public_exit BOOLEAN,
+ publish_read_state BOOLEAN,
+ title TEXT,
+ visibility_level INTEGER
+);
+
CREATE TABLE user_emails
(
email TEXT NOT NULL,
diff --git a/migrations/lib/converters/discourse/steps/groups.rb b/migrations/lib/converters/discourse/steps/groups.rb
new file mode 100644
index 00000000000..27ec55c5aa7
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/groups.rb
@@ -0,0 +1,63 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class Groups < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def execute
+ super
+
+ @flair_upload_creator = UploadCreator.new(column_prefix: "flair")
+ end
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*) FROM groups
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT groups.*,
+ uploads.url AS flair_url,
+ uploads.original_filename AS flair_filename,
+ uploads.origin AS flair_origin,
+ uploads.user_id AS flair_user_id,
+ CASE WHEN groups.automatic THEN groups.id END AS existing_id
+ FROM groups
+ LEFT JOIN uploads ON groups.flair_upload_id = uploads.id
+ ORDER BY groups.id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::Group.create(
+ original_id: item[:id],
+ allow_membership_requests: item[:allow_membership_requests],
+ allow_unknown_sender_topic_replies: item[:allow_unknown_sender_topic_replies],
+ automatic_membership_email_domains: item[:automatic_membership_email_domains],
+ bio_raw: item[:bio_raw],
+ created_at: item[:created_at],
+ default_notification_level: item[:default_notification_level],
+ existing_id: item[:existing_id],
+ flair_bg_color: item[:flair_bg_color],
+ flair_color: item[:flair_color],
+ flair_icon: item[:flair_icon],
+ flair_upload_id: @flair_upload_creator.create_for(item),
+ full_name: item[:full_name],
+ grant_trust_level: item[:grant_trust_level],
+ members_visibility_level: item[:members_visibility_level],
+ membership_request_template: item[:membership_request_template],
+ mentionable_level: item[:mentionable_level],
+ messageable_level: item[:messageable_level],
+ name: item[:name],
+ primary_group: item[:primary_group],
+ public_admission: item[:public_admission],
+ publish_read_state: item[:publish_read_state],
+ public_exit: item[:public_exit],
+ title: item[:title],
+ visibility_level: item[:visibility_level],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/group.rb b/migrations/lib/database/intermediate_db/group.rb
new file mode 100644
index 00000000000..285d3fab1ac
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/group.rb
@@ -0,0 +1,99 @@
+# 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 Group
+ SQL = <<~SQL
+ INSERT INTO "groups" (
+ original_id,
+ allow_membership_requests,
+ allow_unknown_sender_topic_replies,
+ automatic_membership_email_domains,
+ bio_raw,
+ created_at,
+ default_notification_level,
+ existing_id,
+ flair_bg_color,
+ flair_color,
+ flair_icon,
+ flair_upload_id,
+ full_name,
+ grant_trust_level,
+ members_visibility_level,
+ membership_request_template,
+ mentionable_level,
+ messageable_level,
+ name,
+ primary_group,
+ public_admission,
+ public_exit,
+ publish_read_state,
+ title,
+ visibility_level
+ )
+ VALUES (
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
+ )
+ SQL
+
+ def self.create(
+ original_id:,
+ allow_membership_requests: nil,
+ allow_unknown_sender_topic_replies: nil,
+ automatic_membership_email_domains: nil,
+ bio_raw: nil,
+ created_at: nil,
+ default_notification_level: nil,
+ existing_id: nil,
+ flair_bg_color: nil,
+ flair_color: nil,
+ flair_icon: nil,
+ flair_upload_id: nil,
+ full_name: nil,
+ grant_trust_level: nil,
+ members_visibility_level: nil,
+ membership_request_template: nil,
+ mentionable_level: nil,
+ messageable_level: nil,
+ name:,
+ primary_group: nil,
+ public_admission: nil,
+ public_exit: nil,
+ publish_read_state: nil,
+ title: nil,
+ visibility_level: nil
+ )
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ original_id,
+ ::Migrations::Database.format_boolean(allow_membership_requests),
+ ::Migrations::Database.format_boolean(allow_unknown_sender_topic_replies),
+ automatic_membership_email_domains,
+ bio_raw,
+ ::Migrations::Database.format_datetime(created_at),
+ default_notification_level,
+ existing_id,
+ flair_bg_color,
+ flair_color,
+ flair_icon,
+ flair_upload_id,
+ full_name,
+ grant_trust_level,
+ members_visibility_level,
+ membership_request_template,
+ mentionable_level,
+ messageable_level,
+ name,
+ ::Migrations::Database.format_boolean(primary_group),
+ ::Migrations::Database.format_boolean(public_admission),
+ ::Migrations::Database.format_boolean(public_exit),
+ ::Migrations::Database.format_boolean(publish_read_state),
+ title,
+ visibility_level,
+ )
+ end
+ end
+end
diff --git a/migrations/lib/importer/mapping_type.rb b/migrations/lib/importer/mapping_type.rb
index 76cb68718b4..5008d0a27e6 100644
--- a/migrations/lib/importer/mapping_type.rb
+++ b/migrations/lib/importer/mapping_type.rb
@@ -5,6 +5,7 @@ module Migrations::Importer
USERS = 1
BADGES = 2
CATEGORIES = 3
+ GROUPS = 4
UPLOADS = 10
end
end
diff --git a/migrations/lib/importer/steps/group_upload_references.rb b/migrations/lib/importer/steps/group_upload_references.rb
new file mode 100644
index 00000000000..c4386c4b360
--- /dev/null
+++ b/migrations/lib/importer/steps/group_upload_references.rb
@@ -0,0 +1,19 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class GroupUploadReferences < ::Migrations::Importer::Step
+ depends_on :groups
+
+ def execute
+ super
+
+ DB.exec(<<~SQL)
+ INSERT INTO upload_references (upload_id, target_type, target_id, created_at, updated_at)
+ SELECT flair_upload_id, 'Group', id, created_at, updated_at
+ FROM groups
+ WHERE flair_upload_id IS NOT NULL
+ ON CONFLICT DO NOTHING
+ SQL
+ end
+ end
+end
diff --git a/migrations/lib/importer/steps/groups.rb b/migrations/lib/importer/steps/groups.rb
new file mode 100644
index 00000000000..7a7143a2fd5
--- /dev/null
+++ b/migrations/lib/importer/steps/groups.rb
@@ -0,0 +1,208 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class Groups < ::Migrations::Importer::CopyStep
+ DEFAULT_VISIBILITY_LEVEL = Group.visibility_levels[:public]
+ DEFAULT_ALIAS_LEVEL = Group::ALIAS_LEVELS[:nobody]
+ DEFAULT_NOTIFICATION_LEVEL = GroupUser.notification_levels[:watching]
+ VISIBILITY_LEVELS = Group.visibility_levels.values.to_set.freeze
+ ALIAS_LEVELS = Group::ALIAS_LEVELS.values.to_set.freeze
+ TRUST_LEVELS = TrustLevel.levels.values.to_set.freeze
+ NOTIFICATION_LEVELS = GroupUser.notification_levels.values.to_set.freeze
+ DOMAIN_PROTOCOL_REGEX = %r{\Ahttps?://}.freeze
+ DOMAIN_PATH_REGEX = %r{/.*\z}.freeze
+ MAX_FULL_NAME_LENGTH = 100
+ MAX_MEMBER_REQUEST_TEMPLATE_LENGTH = 5_000
+
+ depends_on :uploads
+ store_mapped_ids true
+
+ requires_mapping :ids_by_name, "SELECT name, id FROM groups"
+ requires_set :existing_ids, "SELECT id FROM groups"
+
+ column_names %i[
+ id
+ allow_membership_requests
+ allow_unknown_sender_topic_replies
+ automatic_membership_email_domains
+ bio_cooked
+ bio_raw
+ created_at
+ updated_at
+ default_notification_level
+ flair_bg_color
+ flair_color
+ flair_icon
+ flair_upload_id
+ full_name
+ grant_trust_level
+ members_visibility_level
+ membership_request_template
+ mentionable_level
+ messageable_level
+ name
+ primary_group
+ public_admission
+ public_exit
+ publish_read_state
+ title
+ visibility_level
+ ]
+
+ total_rows_query <<~SQL, MappingType::GROUPS
+ SELECT COUNT(*)
+ FROM groups
+ LEFT JOIN mapped.ids mapped_group
+ ON groups.original_id = mapped_group.original_id AND mapped_group.type = ?
+ WHERE mapped_group.original_id IS NULL
+ SQL
+
+ rows_query <<~SQL, MappingType::GROUPS, MappingType::UPLOADS
+ SELECT groups.*,
+ mapped_flair_upload.discourse_id AS discourse_flair_upload_id,
+ EXISTS (
+ SELECT 1
+ FROM group_users
+ WHERE group_users.group_id = groups.original_id AND group_users.owner = 1
+ ) AS has_owner
+ FROM groups
+ LEFT JOIN mapped.ids mapped_group
+ ON groups.original_id = mapped_group.original_id AND mapped_group.type = ?1
+ LEFT JOIN mapped.ids mapped_flair_upload
+ ON groups.flair_upload_id = mapped_flair_upload.original_id AND mapped_flair_upload.type = ?2
+ WHERE mapped_group.original_id IS NULL
+ ORDER BY groups.ROWID
+ SQL
+
+ def initialize(intermediate_db, discourse_db, shared_data)
+ super
+
+ @unique_name_finder = ::Migrations::Importer::UniqueNameFinder.new(shared_data)
+ @max_domains = SiteSetting.max_automatic_membership_email_domains
+ end
+
+ private
+
+ def transform_row(row)
+ # TODO(selase):
+ # 1. Cook bio_raw
+ # 2. Adopt importer framework warning implementation once available
+ if (existing_id = row[:existing_id])
+ row[:id] = resolve_existing_id(existing_id)
+
+ return nil if row[:id]
+ end
+
+ row[:name] = @unique_name_finder.find_available_group_name(row[:name])
+ row[:full_name] = sanitize_string(row[:full_name], MAX_FULL_NAME_LENGTH)
+ row[:title] = sanitize_string(row[:title])
+ row[:bio_raw] = sanitize_string(row[:bio_raw])
+ row[:membership_request_template] = sanitize_string(
+ row[:membership_request_template],
+ MAX_MEMBER_REQUEST_TEMPLATE_LENGTH,
+ )
+
+ allow_membership_requests = row[:allow_membership_requests] == 1
+ has_owner = row[:has_owner] == 1
+
+ if allow_membership_requests && !has_owner
+ puts " #{row[:name]}: An owner is required to enable allow_membership_requests"
+ end
+
+ row[:allow_membership_requests] = allow_membership_requests && has_owner
+ row[:allow_unknown_sender_topic_replies] ||= false
+ row[:primary_group] ||= false
+ row[:public_admission] ||= false
+ row[:public_exit] ||= false
+ row[:publish_read_state] ||= false
+
+ row[:default_notification_level] = ensure_valid_value(
+ value: row[:default_notification_level],
+ allowed_set: NOTIFICATION_LEVELS,
+ default_value: DEFAULT_NOTIFICATION_LEVEL,
+ )
+ row[:visibility_level] = ensure_valid_value(
+ value: row[:visibility_level],
+ allowed_set: VISIBILITY_LEVELS,
+ default_value: DEFAULT_VISIBILITY_LEVEL,
+ )
+ row[:members_visibility_level] = ensure_valid_value(
+ value: row[:members_visibility_level],
+ allowed_set: VISIBILITY_LEVELS,
+ default_value: DEFAULT_VISIBILITY_LEVEL,
+ )
+ row[:mentionable_level] = ensure_valid_value(
+ value: row[:mentionable_level],
+ allowed_set: ALIAS_LEVELS,
+ default_value: DEFAULT_ALIAS_LEVEL,
+ )
+ row[:messageable_level] = ensure_valid_value(
+ value: row[:messageable_level],
+ allowed_set: ALIAS_LEVELS,
+ default_value: DEFAULT_ALIAS_LEVEL,
+ )
+
+ unless row[:grant_trust_level].nil?
+ row[:grant_trust_level] = ensure_valid_value(
+ value: row[:grant_trust_level].presence,
+ allowed_set: TRUST_LEVELS,
+ default_value: nil,
+ ) { |value, _default_value| puts " #{row[:name]}: Invalid grant_trust_level '#{value}'" }
+ end
+
+ if row[:automatic_membership_email_domains].present?
+ valid_domains = []
+
+ row[:automatic_membership_email_domains]
+ .split("|")
+ .each do |domain|
+ domain.sub!(DOMAIN_PROTOCOL_REGEX, "")
+ domain.sub!(DOMAIN_PATH_REGEX, "")
+
+ unless domain =~ Group::VALID_DOMAIN_REGEX
+ puts " #{row[:name]}: Invalid automatic_membership_email_domain '#{domain}'"
+ next
+ end
+
+ if domain.length > Group::MAX_EMAIL_DOMAIN_LENGTH
+ puts " #{row[:name]}: Invalid automatic_membership_email_domain. Domain '#{domain}' is too long " \
+ "(Max: #{Group::MAX_EMAIL_DOMAIN_LENGTH})"
+ next
+ end
+
+ valid_domains << domain
+ end
+
+ if valid_domains.size > @max_domains
+ puts " #{row[:name]}: Invalid automatic_membership_email_domain. Too many domains (Max: #{@max_domains})"
+
+ valid_domains = valid_domains.take(@max_domains)
+ end
+
+ row[:automatic_membership_email_domains] = valid_domains.join("|")
+ end
+
+ row[:flair_upload_id] = row[:discourse_flair_upload_id]
+
+ super
+ end
+
+ def resolve_existing_id(existing_id)
+ numeric_id = Integer(existing_id, exception: false)
+ return numeric_id if numeric_id && @existing_ids.include?(numeric_id)
+
+ @ids_by_name[existing_id]
+ end
+
+ def sanitize_string(value, max_length = nil)
+ return value if value.nil? || value.empty?
+
+ value = value.dup
+ value[max_length..-1] = "" if max_length && value.length > max_length
+ value.scrub!
+ value.strip!
+
+ value.empty? ? nil : value
+ end
+ end
+end
diff --git a/migrations/spec/lib/database/intermediate_db/group_spec.rb b/migrations/spec/lib/database/intermediate_db/group_spec.rb
new file mode 100644
index 00000000000..e3df14f3a51
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/group_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::Group do
+ it_behaves_like "a database entity"
+end