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

150 lines
3.7 KiB
Ruby

# frozen_string_literal: true
module Migrations
module Importer
class Step
Enums = Database::IntermediateDB::Enums
class << self
# stree-ignore
def title(value = (getter = true; nil))
if getter
return(
@title ||=
I18n.t(
"importer.default_step_title",
type: name&.demodulize&.underscore&.humanize(capitalize: false),
)
)
end
@title = value
end
def depends_on(*step_names)
steps_module = Steps
classes =
step_names.map do |name|
name = name.to_s.camelize
klass = steps_module.const_get(name) if steps_module.const_defined?(name)
unless klass.is_a?(Class) && klass < Step
raise NameError, "Class #{class_name} not found"
end
klass
end
@dependencies ||= []
@dependencies.concat(classes)
end
def dependencies
@dependencies || []
end
# stree-ignore
def priority(value = (getter = true; nil))
if getter
@priority
else
@priority = value
end
end
def requires_shared_data(*names)
@required_shared_data ||= []
@required_shared_data += names
end
def required_shared_data
@required_shared_data || []
end
def requires_mapping(name, sql)
@required_mappings ||= {}
@required_mappings[name] = sql
end
def required_mappings
@required_mappings || {}
end
def requires_set(name, sql)
@required_sets ||= {}
@required_sets[name] = sql
end
def required_sets
@required_sets || {}
end
end
def initialize(intermediate_db, discourse_db, shared_data, config)
@intermediate_db = intermediate_db
@discourse_db = discourse_db
@shared_data = shared_data
@config = config
@stats = StepStats.new(skip_count: 0, warning_count: 0, error_count: 0)
setup
end
def execute
load_required_data
end
private
# Override in subclasses if necessary
def setup
end
def load_required_data
required_shared_data = self.class.required_shared_data
required_mappings = self.class.required_mappings
required_sets = self.class.required_sets
return if required_shared_data.empty? && required_mappings.blank? && required_sets.blank?
print " #{I18n.t("importer.loading_required_data")} "
runtime =
DateHelper.track_time do
required_shared_data.each do |name|
instance_variable_set("@#{name}", @shared_data[name])
end
required_mappings.each do |name, sql|
instance_variable_set("@#{name}", @shared_data.load_mapping(sql))
end
required_sets.each do |name, sql|
instance_variable_set("@#{name}", @shared_data.load_set(sql))
end
end
puts DateHelper.human_readable_time(runtime) if runtime >= 1
end
def update_progressbar(increment_by: 1)
@progressbar.update(
increment_by:,
skip_count: @stats.skip_count,
warning_count: @stats.warning_count,
error_count: @stats.error_count,
)
end
def with_progressbar(max_progress)
ExtendedProgressBar
.new(max_progress:)
.run do |progressbar|
@progressbar = progressbar
yield
@progressbar = nil
end
end
end
end
end