discourse/migrations/lib/migrations.rb
Gerhard Schlager 7c3a29c9d6
DEV: Add converter framework for migrations-tooling (#28540)
* Updates GitHub Actions
* Switches from `bundler/inline` to an optional group in the `Gemfile` because the previous solution didn't work well with rspec
* Adds the converter framework and tests
* Allows loading private converters (see README)
* Switches from multiple CLI tools to a single CLI
* Makes DB connections reusable and adds a new abstraction for the `IntermediateDB`
* `IntermediateDB` acts as an interface for IPC calls when a converter steps runs in parallel (forks). Only the main process writes to the DB.
* Includes a simple example implementation of a converter for now.
2024-09-09 17:14:39 +02:00

76 lines
2.0 KiB
Ruby

# frozen_string_literal: true
require "bundler/setup"
Bundler.setup
require "active_support"
require "active_support/core_ext"
require "zeitwerk"
require_relative "converters"
module Migrations
def self.root_path
@root_path ||= File.expand_path("..", __dir__)
end
def self.load_rails_environment(quiet: false)
message = "Loading Rails environment ..."
print message unless quiet
rails_root = File.expand_path("../..", __dir__)
# rubocop:disable Discourse/NoChdir
Dir.chdir(rails_root) do
begin
require File.join(rails_root, "config/environment")
rescue LoadError => e
$stderr.puts e.message
raise
end
end
# rubocop:enable Discourse/NoChdir
print "\r"
print " " * message.length
print "\r"
end
def self.configure_zeitwerk
loader = Zeitwerk::Loader.new
loader.log! if ENV["DEBUG"]
loader.inflector.inflect(
{ "cli" => "CLI", "intermediate_db" => "IntermediateDB", "uploads_db" => "UploadsDB" },
)
loader.push_dir(File.join(::Migrations.root_path, "lib"), namespace: ::Migrations)
loader.push_dir(File.join(::Migrations.root_path, "lib", "common"), namespace: ::Migrations)
# All sub-directories of a converter should have the same namespace.
# Unfortunately `loader.collapse` doesn't work recursively.
Converters.all.each do |name, converter_path|
module_name = name.camelize.to_sym
namespace = ::Migrations::Converters.const_set(module_name, Module.new)
Dir[File.join(converter_path, "**", "*")].each do |subdirectory|
next unless File.directory?(subdirectory)
loader.push_dir(subdirectory, namespace: namespace)
end
end
loader.setup
end
def self.enable_i18n
require "i18n"
locale_glob = File.join(::Migrations.root_path, "config", "locales", "**", "migrations.*.yml")
I18n.load_path += Dir[locale_glob]
I18n.backend.load_translations
# always use English for now
I18n.default_locale = :en
I18n.locale = :en
end
end