discourse/migrations/lib/cli/convert_command.rb
Gerhard Schlager 89f26da39d
MT: Switch to nested module style across migrations/ (#38564)
Ruby's compact module syntax (`module
Migrations::Database::Schema::DSL`) breaks lexical constant lookup —
`Module.nesting` only includes the innermost constant, so every
cross-module reference must be fully qualified. In practice this means
writing `Migrations::Database::Schema::Helpers` even when you're already
inside `Migrations::Database::Schema`.

Nested module definitions restore the full nesting chain, which brings
several practical benefits:

- **Less verbose code**: references like `Schema::Helpers`,
`Database::IntermediateDB`, or `Converters::Base::ProgressStep` work
without repeating the full path from root
- **Easier to write new code**: contributors don't need to remember
which prefixes are required — if you're inside the namespace, short
names just work
- **Fewer aliasing workarounds**: removes the need for constants like
`MappingType = Migrations::Importer::MappingType` that existed solely to
shorten references
- **Standard Ruby style**: consistent with how most Ruby projects and
gems structure their namespaces

The diff is large but mechanical — no logic changes, just module
wrapping and shortening references that the nesting now resolves.
Generated code (intermediate_db models/enums) keeps fully qualified
references like `Migrations::Database.format_*` since it must work
regardless of the configured output namespace.

- Convert 138 lib files from compact to nested module definitions
- Remove now-redundant fully qualified prefixes and aliases
- Update model and enum writers to generate nested modules with correct
indentation
- Regenerate all intermediate_db models and enums
2026-03-19 18:15:19 +01:00

51 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module Migrations
module CLI
class ConvertCommand
def initialize(converter_type, options)
@converter_type = converter_type.downcase
@options = options
end
def execute
validate_converter_type!
settings = load_settings
Database.reset!(settings[:intermediate_db][:path]) if @options[:reset]
converter = "migrations/converters/#{@converter_type}/converter".camelize.constantize
converter.new(settings).run(only_steps: @options[:only], skip_steps: @options[:skip])
end
private
def validate_converter_type!
converter_names = Converters.names
raise Thor::Error, <<~MSG if !converter_names.include?(@converter_type)
Unknown converter name: #{@converter_type}
Valid names are: #{converter_names.join(", ")}
MSG
end
def validate_settings_path!(settings_path)
if !File.exist?(settings_path)
raise Thor::Error, "Settings file not found: #{settings_path}"
end
end
def load_settings
settings_path = calculate_settings_path
validate_settings_path!(settings_path)
YAML.safe_load(File.read(settings_path), symbolize_names: true)
end
def calculate_settings_path
settings_path = @options[:settings] || Converters.default_settings_path(@converter_type)
File.expand_path(settings_path, Dir.pwd)
end
end
end
end