mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-23 21:20:41 +08:00
* It only imports users and emails so far * It stores mapped IDs and usernames in a SQLite DB. In the future, we might want to copy those into the Discourse DB at the end of a migration. * The importer is split into steps which can mostly be configured with a simple DSL * Data that needs to be shared between steps can be stored in an instance of the `SharedData` class * Steps are automatically sorted via their defined dependencies before they are executed * Common logic for finding unique names (username, group name) is extracted into a helper class * If possible, steps try to avoid loading already imported data (via `mapping.ids` table) * And steps should select the `discourse_id` instead of the `original_id` of mapped IDs via SQL
39 lines
1.2 KiB
Ruby
Vendored
39 lines
1.2 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module Migrations::Importer::Steps
|
|
class UserEmails < ::Migrations::Importer::CopyStep
|
|
depends_on :users
|
|
|
|
requires_set :existing_user_ids, "SELECT DISTINCT user_id FROM user_emails"
|
|
|
|
column_names %i[user_id email primary created_at updated_at]
|
|
|
|
total_rows_query <<~SQL, MappingType::USERS
|
|
SELECT COUNT(*)
|
|
FROM users u
|
|
JOIN mapped.ids mu ON u.original_id = mu.original_id AND mu.type = ?
|
|
LEFT JOIN user_emails ue ON u.original_id = ue.user_id
|
|
SQL
|
|
|
|
rows_query <<~SQL, MappingType::USERS
|
|
SELECT mu.discourse_id AS user_id,
|
|
ue.email,
|
|
COALESCE(ue."primary", TRUE) AS "primary",
|
|
COALESCE(ue.created_at, u.created_at) AS created_at
|
|
FROM users u
|
|
JOIN mapped.ids mu ON u.original_id = mu.original_id AND mu.type = ?
|
|
LEFT JOIN user_emails ue ON u.original_id = ue.user_id
|
|
ORDER BY ue.ROWID
|
|
SQL
|
|
|
|
private
|
|
|
|
def transform_row(row)
|
|
return nil if @existing_user_ids.include?(row[:user_id])
|
|
|
|
row[:email] ||= "#{SecureRandom.hex}@email.invalid"
|
|
|
|
super
|
|
end
|
|
end
|
|
end
|