discourse/migrations/lib/importer/shared_data.rb
Gerhard Schlager 251cac39af DEV: Adds a basic importer for the IntermediateDB
* 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
2025-04-07 17:22:36 +02:00

52 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Migrations::Importer
class SharedData
def initialize(discourse_db)
@discourse_db = discourse_db
end
def load_set(sql)
@discourse_db.query_array(sql).map(&:first).to_set
end
def load_mapping(sql)
rows = @discourse_db.query_array(sql)
if rows.first && rows.first.size > 2
rows.to_h { |key, *values| [key, *values] }
else
rows.to_h
end
end
def load(type)
case type
when :usernames
@existing_usernames_lower ||= load_set <<~SQL
SELECT username_lower
FROM users
SQL
when :group_names
@existing_group_names_lower ||= load_set <<~SQL
SELECT LOWER(name)
FROM groups
SQL
else
raise "Unknown type: #{type}"
end
end
def unload_shared_data(type)
case type
when :usernames
@existing_usernames_lower = nil
when :group_names
@existing_group_names_lower = nil
else
raise "Unknown type: #{type}"
end
end
end
end