discourse/migrations/lib/database.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

77 lines
1.6 KiB
Ruby

# frozen_string_literal: true
require "date"
require "extralite"
require "ipaddr"
require "oj"
module Migrations
module Database
INTERMEDIATE_DB_SCHEMA_PATH = File.join(::Migrations.root_path, "db", "intermediate_db_schema")
MAPPINGS_DB_SCHEMA_PATH = File.join(::Migrations.root_path, "db", "mappings_db_schema")
UPLOADS_DB_SCHEMA_PATH = File.join(::Migrations.root_path, "db", "uploads_db_schema")
def self.migrate(db_path, migrations_path:)
Migrator.new(db_path).migrate(migrations_path)
end
def self.reset!(db_path)
Migrator.new(db_path).reset!
end
def self.connect(path)
connection = Connection.new(path:)
return connection unless block_given?
begin
yield(connection)
ensure
connection.close
end
nil
end
def self.format_datetime(value)
value&.utc&.iso8601
end
def self.format_date(value)
value&.to_date&.iso8601
end
def self.format_boolean(value)
return nil if value.nil?
value ? 1 : 0
end
def self.format_ip_address(value)
return nil if value.blank?
IPAddr.new(value).to_s
rescue ArgumentError
nil
end
def self.to_blob(value)
return nil if value.blank?
::Extralite::Blob.new(value)
end
def self.to_json(value)
return nil if value.nil?
::Oj.dump(value, mode: :compat)
end
def self.to_date(text)
text.present? ? Date.parse(text) : nil
end
def self.to_datetime(text)
text.present? ? DateTime.parse(text) : nil
end
def self.to_boolean(value)
value == 1
end
end
end