Skip to content

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

DEV: Add support for converting and importing `user_fields` (#34295)

Selase Krakani3e0dff8ee9c1
diff --git a/migrations/config/intermediate_db.yml b/migrations/config/intermediate_db.yml
index b15ea3d3237..71fd1a6730e 100644
--- a/migrations/config/intermediate_db.yml
+++ b/migrations/config/intermediate_db.yml
@@ -121,6 +121,16 @@ schema:
- "user_id"
- "created_at"
primary_key_column_names: [ "user_id" , "email" ]
+ user_field_options:
+ primary_key_column_names: [ "user_field_id", "value" ]
+ columns:
+ exclude:
+ - "id"
+ user_fields:
+ columns:
+ exclude:
+ - "field_type"
+ - "required"
user_options:
primary_key_column_names: [ "user_id" ]
users:
@@ -411,8 +421,6 @@ schema:
- "user_chat_thread_memberships"
- "user_custom_fields"
- "user_exports"
- - "user_field_options"
- - "user_fields"
- "user_histories"
- "user_ip_address_histories"
- "user_notification_schedules"
diff --git a/migrations/db/intermediate_db_schema/100-base-schema.sql b/migrations/db/intermediate_db_schema/100-base-schema.sql
index 45683388fb7..25d06180ec7 100644
--- a/migrations/db/intermediate_db_schema/100-base-schema.sql
+++ b/migrations/db/intermediate_db_schema/100-base-schema.sql
@@ -200,6 +200,31 @@ CREATE TABLE user_emails
PRIMARY KEY (user_id, email)
);
+CREATE TABLE user_field_options
+(
+ user_field_id NUMERIC NOT NULL,
+ value TEXT NOT NULL,
+ created_at DATETIME,
+ PRIMARY KEY (user_field_id, value)
+);
+
+CREATE TABLE user_fields
+(
+ original_id NUMERIC NOT NULL PRIMARY KEY,
+ created_at DATETIME,
+ description TEXT NOT NULL,
+ editable BOOLEAN,
+ external_name TEXT,
+ external_type TEXT,
+ field_type_enum INTEGER NOT NULL,
+ name TEXT NOT NULL,
+ position INTEGER,
+ requirement INTEGER,
+ searchable BOOLEAN,
+ show_on_profile BOOLEAN,
+ show_on_user_card BOOLEAN
+);
+
CREATE TABLE user_options
(
user_id NUMERIC NOT NULL PRIMARY KEY,
diff --git a/migrations/lib/converters/discourse/steps/user_field_options.rb b/migrations/lib/converters/discourse/steps/user_field_options.rb
new file mode 100644
index 00000000000..e59092eed2a
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/user_field_options.rb
@@ -0,0 +1,29 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class UserFieldOptions < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*) FROM user_field_options
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT *
+ FROM user_field_options
+ ORDER BY user_field_id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::UserFieldOption.create(
+ user_field_id: item[:user_field_id],
+ value: item[:value],
+ created_at: item[:created_at],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/converters/discourse/steps/user_fields.rb b/migrations/lib/converters/discourse/steps/user_fields.rb
new file mode 100644
index 00000000000..9fe36efd30c
--- /dev/null
+++ b/migrations/lib/converters/discourse/steps/user_fields.rb
@@ -0,0 +1,39 @@
+# frozen_string_literal: true
+
+module Migrations::Converters::Discourse
+ class UserFields < ::Migrations::Converters::Base::ProgressStep
+ attr_accessor :source_db
+
+ def max_progress
+ @source_db.count <<~SQL
+ SELECT COUNT(*) FROM user_fields
+ SQL
+ end
+
+ def items
+ @source_db.query <<~SQL
+ SELECT *
+ FROM user_fields
+ ORDER BY id
+ SQL
+ end
+
+ def process_item(item)
+ IntermediateDB::UserField.create(
+ original_id: item[:id],
+ created_at: item[:created_at],
+ description: item[:description],
+ editable: item[:editable],
+ external_name: item[:external_name],
+ external_type: item[:external_type],
+ field_type_enum: item[:field_type_enum],
+ name: item[:name],
+ position: item[:position],
+ requirement: item[:requirement],
+ searchable: item[:searchable],
+ show_on_profile: item[:show_on_profile],
+ show_on_user_card: item[:show_on_user_card],
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/user_field.rb b/migrations/lib/database/intermediate_db/user_field.rb
new file mode 100644
index 00000000000..040cf7ef047
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/user_field.rb
@@ -0,0 +1,63 @@
+# 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 UserField
+ SQL = <<~SQL
+ INSERT INTO user_fields (
+ original_id,
+ created_at,
+ description,
+ editable,
+ external_name,
+ external_type,
+ field_type_enum,
+ name,
+ position,
+ requirement,
+ searchable,
+ show_on_profile,
+ show_on_user_card
+ )
+ VALUES (
+ ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?
+ )
+ SQL
+
+ def self.create(
+ original_id:,
+ created_at: nil,
+ description:,
+ editable: nil,
+ external_name: nil,
+ external_type: nil,
+ field_type_enum:,
+ name:,
+ position: nil,
+ requirement: nil,
+ searchable: nil,
+ show_on_profile: nil,
+ show_on_user_card: nil
+ )
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ original_id,
+ ::Migrations::Database.format_datetime(created_at),
+ description,
+ ::Migrations::Database.format_boolean(editable),
+ external_name,
+ external_type,
+ field_type_enum,
+ name,
+ position,
+ requirement,
+ ::Migrations::Database.format_boolean(searchable),
+ ::Migrations::Database.format_boolean(show_on_profile),
+ ::Migrations::Database.format_boolean(show_on_user_card),
+ )
+ end
+ end
+end
diff --git a/migrations/lib/database/intermediate_db/user_field_option.rb b/migrations/lib/database/intermediate_db/user_field_option.rb
new file mode 100644
index 00000000000..0adfafbbcea
--- /dev/null
+++ b/migrations/lib/database/intermediate_db/user_field_option.rb
@@ -0,0 +1,29 @@
+# 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 UserFieldOption
+ SQL = <<~SQL
+ INSERT INTO user_field_options (
+ user_field_id,
+ value,
+ created_at
+ )
+ VALUES (
+ ?, ?, ?
+ )
+ SQL
+
+ def self.create(user_field_id:, value:, created_at: nil)
+ ::Migrations::Database::IntermediateDB.insert(
+ SQL,
+ user_field_id,
+ value,
+ ::Migrations::Database.format_datetime(created_at),
+ )
+ end
+ end
+end
diff --git a/migrations/lib/importer/mapping_type.rb b/migrations/lib/importer/mapping_type.rb
index 9d239b17c85..91f9bd0748b 100644
--- a/migrations/lib/importer/mapping_type.rb
+++ b/migrations/lib/importer/mapping_type.rb
@@ -9,6 +9,7 @@ module Migrations::Importer
TAGS = 5
TAG_GROUPS = 6
BADGE_GROUPINGS = 7
+ USER_FIELDS = 8
UPLOADS = 10
end
end
diff --git a/migrations/lib/importer/steps/user_field_options.rb b/migrations/lib/importer/steps/user_field_options.rb
new file mode 100644
index 00000000000..7874b48f476
--- /dev/null
+++ b/migrations/lib/importer/steps/user_field_options.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class UserFieldOptions < ::Migrations::Importer::CopyStep
+ depends_on :user_fields
+
+ requires_set :existing_user_field_options, "SELECT user_field_id, value FROM user_field_options"
+
+ column_names %i[user_field_id value created_at updated_at]
+
+ total_rows_query <<~SQL, MappingType::USER_FIELDS
+ SELECT COUNT(*)
+ FROM user_field_options
+ JOIN mapped.ids mapped_user_field
+ ON user_field_options.user_field_id = mapped_user_field.original_id
+ AND mapped_user_field.type = ?
+ SQL
+
+ rows_query <<~SQL, MappingType::USER_FIELDS
+ SELECT user_field_options.*,
+ mapped_user_field.discourse_id AS discourse_user_field_id
+ FROM user_field_options
+ JOIN mapped.ids mapped_user_field
+ ON user_field_options.user_field_id = mapped_user_field.original_id
+ AND mapped_user_field.type = ?
+ ORDER BY user_field_options.user_field_id, user_field_options.value
+ SQL
+
+ private
+
+ def transform_row(row)
+ user_field_id = row[:discourse_user_field_id]
+
+ return nil unless @existing_user_field_options.add?(user_field_id, row[:value])
+
+ row[:user_field_id] = user_field_id
+
+ super
+ end
+ end
+end
diff --git a/migrations/lib/importer/steps/user_fields.rb b/migrations/lib/importer/steps/user_fields.rb
new file mode 100644
index 00000000000..53181a71b11
--- /dev/null
+++ b/migrations/lib/importer/steps/user_fields.rb
@@ -0,0 +1,114 @@
+# frozen_string_literal: true
+
+module Migrations::Importer::Steps
+ class UserFields < ::Migrations::Importer::CopyStep
+ include ::HasSanitizableFields
+
+ SANITIZER_ATTRIBUTES = %w[target].freeze
+ DEFAULT_POSITION = 0
+ REQUIREMENTS = UserField.requirements.values.to_set.freeze
+ DEFAULT_REQUIREMENT = UserField.requirements[:optional]
+ REQUIRED_FOR_ALL = UserField.requirements[:for_all_users]
+ FIELD_TYPES = UserField.field_type_enums.values.to_set.freeze
+ DEFAULT_FIELD_TYPE = UserField.field_type_enums[:text]
+
+ requires_mapping :existing_user_field_by_name, "SELECT LOWER(name), id FROM user_fields"
+
+ column_names %i[
+ id
+ created_at
+ description
+ editable
+ external_name
+ external_type
+ field_type_enum
+ name
+ position
+ requirement
+ searchable
+ show_on_profile
+ show_on_user_card
+ updated_at
+ ]
+
+ store_mapped_ids true
+
+ total_rows_query <<~SQL, MappingType::USER_FIELDS
+ SELECT COUNT(*)
+ FROM user_fields
+ LEFT JOIN mapped.ids mapped_user_field
+ ON user_fields.original_id = mapped_user_field.original_id
+ AND mapped_user_field.type = ?
+ WHERE mapped_user_field.original_id IS NULL
+ SQL
+
+ rows_query <<~SQL, MappingType::USER_FIELDS
+ SELECT user_fields.*
+ FROM user_fields
+ LEFT JOIN mapped.ids mapped_user_field
+ ON user_fields.original_id = mapped_user_field.original_id
+ AND mapped_user_field.type = ?
+ WHERE mapped_user_field.original_id IS NULL
+ ORDER BY user_fields.original_id
+ SQL
+
+ def initialize(intermediate_db, discourse_db, shared_data)
+ super
+
+ @needs_required_fields_version_bump = false
+ end
+
+ def after(total_rows:)
+ return unless @needs_required_fields_version_bump && total_rows.positive?
+
+ DB.exec(<<~SQL)
+ INSERT INTO user_required_fields_versions (created_at, updated_at)
+ VALUES (NOW(), NOW())
+ SQL
+ end
+
+ private
+
+ def transform_row(row)
+ name = row[:name]
+ name_lower = name.downcase
+
+ if (existing_id = @existing_user_field_by_name[name_lower])
+ row[:id] = existing_id
+
+ return nil
+ end
+
+ description = row[:description]
+
+ if description.empty?
+ puts " User field '#{name}' description cannot be empty"
+
+ return nil
+ end
+
+ row[:editable] ||= false
+ row[:show_on_profile] ||= false
+ row[:show_on_user_card] ||= false
+ row[:searchable] ||= false
+
+ row[:position] ||= DEFAULT_POSITION
+ row[:description] = sanitize_field(description, additional_attributes: SANITIZER_ATTRIBUTES)
+
+ row[:requirement] = ensure_valid_value(
+ value: row[:requirement],
+ allowed_set: REQUIREMENTS,
+ default_value: DEFAULT_REQUIREMENT,
+ )
+ row[:field_type_enum] = ensure_valid_value(
+ value: row[:field_type_enum],
+ allowed_set: FIELD_TYPES,
+ default_value: DEFAULT_FIELD_TYPE,
+ )
+
+ @needs_required_fields_version_bump ||= row[:requirement] == REQUIRED_FOR_ALL
+
+ super
+ end
+ end
+end
diff --git a/migrations/spec/lib/database/intermediate_db/user_field_option_spec.rb b/migrations/spec/lib/database/intermediate_db/user_field_option_spec.rb
new file mode 100644
index 00000000000..75e55b1dc44
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/user_field_option_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::UserFieldOption do
+ it_behaves_like "a database entity"
+end
diff --git a/migrations/spec/lib/database/intermediate_db/user_field_spec.rb b/migrations/spec/lib/database/intermediate_db/user_field_spec.rb
new file mode 100644
index 00000000000..72861028d4b
--- /dev/null
+++ b/migrations/spec/lib/database/intermediate_db/user_field_spec.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+RSpec.describe ::Migrations::Database::IntermediateDB::UserField do
+ it_behaves_like "a database entity"
+end