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

81 lines
2.1 KiB
Ruby
Vendored

# frozen_string_literal: true
module Migrations
module Database
module Schema
module DSL
EnumDef = Data.define(:name, :values, :datatype)
class EnumBuilder
def initialize(name)
@name = name.to_s
@values = {}
@source_block = nil
end
def value(name, val)
@values[name.to_s] = val
end
def source(&block)
@source_block = block
end
def build
values = resolve_values
if values.empty?
raise ConfigError, "Enum :#{@name} must define at least one value or a source."
end
datatype = validate_and_infer_datatype(values)
EnumDef.new(name: @name, values: values.freeze, datatype:)
end
private
def resolve_values
if @source_block
evaluate_source
else
@values
end
end
def evaluate_source
result = @source_block.call
case result
when Hash
result.transform_keys(&:to_s)
when Array
result.each_with_index.to_h { |k, i| [k.to_s, i] }
else
raise ConfigError,
"Enum :#{@name} source must return a Hash or Array, got #{result.class}."
end
rescue ConfigError
raise
rescue StandardError => e
raise ConfigError, "Enum :#{@name} failed to evaluate source: #{e.message}"
end
def validate_and_infer_datatype(values)
types = values.values.map(&:class).uniq
if types.size > 1
raise ConfigError, "Enum :#{@name} values must all be Strings or all Integers"
end
type = types.first
if type == String
:text
elsif type == Integer
:integer
else
raise ConfigError,
"Enum :#{@name} values must be Strings or Integers, got #{types.first}"
end
end
end
end
end
end
end