Files
discourse/migrations/lib/importer/executor.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

64 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module Migrations::Importer
class Executor
def initialize(config)
@intermediate_db = ::Migrations::Database.connect(config[:intermediate_db])
@discourse_db = DiscourseDB.new
@shared_data = SharedData.new(@discourse_db)
attach_mappings_db(config[:mappings_db])
end
def start
runtime =
::Migrations::DateHelper.track_time do
execute_steps
ensure
cleanup
end
puts I18n.t("importer.done", runtime: ::Migrations::DateHelper.human_readable_time(runtime))
end
private
def attach_mappings_db(db_path)
# ::Migrations::Database.reset!(db_path)
::Migrations::Database.migrate(
db_path,
migrations_path: ::Migrations::Database::MAPPINGS_DB_SCHEMA_PATH,
)
@intermediate_db.execute("ATTACH DATABASE ? AS mapped", db_path)
end
def step_classes
steps_module = ::Migrations::Importer::Steps
classes =
steps_module
.constants
.map { |c| steps_module.const_get(c) }
.select { |klass| klass.is_a?(Class) && klass < ::Migrations::Importer::Step }
TopologicalSorter.sort(classes)
end
def execute_steps
max = step_classes.size
step_classes
.each
.with_index(1) do |step_class, index|
puts "#{step_class.title} [#{index}/#{max}]"
step = step_class.new(@intermediate_db, @discourse_db, @shared_data)
step.execute
puts ""
end
end
def cleanup
@intermediate_db.close
@discourse_db.close
end
end
end