mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-27 05:21:05 +08:00
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
98 lines
2.7 KiB
Ruby
Vendored
98 lines
2.7 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module Migrations
|
|
module Database
|
|
module Schema
|
|
module DSL
|
|
Convention = Data.define(:name, :pattern, :rename_to, :type_override, :required)
|
|
|
|
ConventionsConfig =
|
|
Data.define(:conventions, :ignored_columns) do
|
|
def effective_name(column_name)
|
|
convention = convention_for(column_name)
|
|
return convention.rename_to if convention&.rename_to
|
|
column_name.to_s
|
|
end
|
|
|
|
def convention_for(column_name)
|
|
column_name = column_name.to_s
|
|
# Exact name match first, then regex pattern match
|
|
conventions.find { |c| c.name == column_name } ||
|
|
conventions.find { |c| c.pattern&.match?(column_name) }
|
|
end
|
|
|
|
def required?(column_name)
|
|
convention_for(column_name)&.required == true
|
|
end
|
|
|
|
def ignored_column?(column_name)
|
|
ignored_columns.include?(column_name.to_s)
|
|
end
|
|
end
|
|
|
|
class ConventionsBuilder
|
|
def initialize
|
|
@conventions = []
|
|
@ignored_columns = []
|
|
end
|
|
|
|
def column(name, &block)
|
|
builder = ConventionEntryBuilder.new(name: name.to_s)
|
|
builder.instance_eval(&block)
|
|
@conventions << builder.build
|
|
end
|
|
|
|
def columns_matching(pattern, &block)
|
|
pattern = Regexp.new(pattern) unless pattern.is_a?(Regexp)
|
|
builder = ConventionEntryBuilder.new(pattern:)
|
|
builder.instance_eval(&block)
|
|
@conventions << builder.build
|
|
end
|
|
|
|
def ignore_columns(*names)
|
|
@ignored_columns.concat(names.flatten.map(&:to_s))
|
|
end
|
|
|
|
def build
|
|
ConventionsConfig.new(
|
|
conventions: @conventions.freeze,
|
|
ignored_columns: @ignored_columns.freeze,
|
|
)
|
|
end
|
|
end
|
|
|
|
class ConventionEntryBuilder
|
|
def initialize(name: nil, pattern: nil)
|
|
@name = name
|
|
@pattern = pattern
|
|
@rename_to = nil
|
|
@type_override = nil
|
|
@required = nil
|
|
end
|
|
|
|
def rename_to(value)
|
|
@rename_to = value.to_s
|
|
end
|
|
|
|
def type(value)
|
|
@type_override = value.to_s
|
|
end
|
|
|
|
def required(value = true)
|
|
@required = value
|
|
end
|
|
|
|
def build
|
|
Convention.new(
|
|
name: @name,
|
|
pattern: @pattern,
|
|
rename_to: @rename_to,
|
|
type_override: @type_override,
|
|
required: @required,
|
|
)
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|