mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-28 09:18:55 +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
108 lines
3.8 KiB
Ruby
Vendored
108 lines
3.8 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module Migrations
|
|
module Database
|
|
module Schema
|
|
module DSL
|
|
class ResolvedSchemaValidator
|
|
def initialize(resolved_schema)
|
|
@schema = resolved_schema
|
|
@errors = []
|
|
end
|
|
|
|
def validate
|
|
@errors.clear
|
|
|
|
@schema.tables.each { |table| validate_table(table) }
|
|
@schema.enums.each { |enum| validate_enum(enum) }
|
|
|
|
@errors
|
|
end
|
|
|
|
private
|
|
|
|
def validate_table(table)
|
|
column_names = table.columns.map(&:name).to_set
|
|
|
|
validate_columns(table)
|
|
validate_primary_key(table, column_names)
|
|
validate_indexes(table, column_names)
|
|
validate_constraints(table)
|
|
end
|
|
|
|
def validate_columns(table)
|
|
table.columns.each do |column|
|
|
if Helpers::VALID_DATATYPES.exclude?(column.datatype)
|
|
@errors << "Table '#{table.name}': column '#{column.name}' has invalid datatype '#{column.datatype}'"
|
|
end
|
|
|
|
@errors << "Table '#{table.name}': column has empty name" if column.name.blank?
|
|
|
|
if column.is_primary_key && column.nullable
|
|
@errors << "Table '#{table.name}': primary key column '#{column.name}' should not be nullable"
|
|
end
|
|
end
|
|
|
|
duplicates = table.columns.map(&:name).tally.filter_map { |n, c| n if c > 1 }
|
|
if duplicates.any?
|
|
@errors << "Table '#{table.name}': duplicate column names: #{duplicates.join(", ")}"
|
|
end
|
|
end
|
|
|
|
def validate_primary_key(table, column_names)
|
|
return unless table.primary_key_column_names
|
|
|
|
missing = table.primary_key_column_names.reject { |pk| column_names.include?(pk) }
|
|
if missing.any?
|
|
@errors << "Table '#{table.name}': primary key references missing columns: #{missing.join(", ")}"
|
|
end
|
|
end
|
|
|
|
def validate_indexes(table, column_names)
|
|
table.indexes.each do |index|
|
|
missing = index.column_names.reject { |col| column_names.include?(col) }
|
|
if missing.any?
|
|
@errors << "Table '#{table.name}': index '#{index.name}' references missing columns: #{missing.join(", ")}"
|
|
end
|
|
|
|
@errors << "Table '#{table.name}': index has empty name" if index.name.blank?
|
|
end
|
|
|
|
duplicates = table.indexes.map(&:name).tally.filter_map { |n, c| n if c > 1 }
|
|
if duplicates.any?
|
|
@errors << "Table '#{table.name}': duplicate index names: #{duplicates.join(", ")}"
|
|
end
|
|
end
|
|
|
|
def validate_constraints(table)
|
|
table.constraints.each do |constraint|
|
|
if constraint.name.blank?
|
|
@errors << "Table '#{table.name}': constraint has empty name"
|
|
end
|
|
|
|
if constraint.condition.blank?
|
|
@errors << "Table '#{table.name}': constraint '#{constraint.name}' has empty condition"
|
|
end
|
|
end
|
|
end
|
|
|
|
def validate_enum(enum)
|
|
@errors << "Enum has empty name" if enum.name.blank?
|
|
|
|
@errors << "Enum '#{enum.name}' has no values" if enum.values.empty?
|
|
|
|
if %i[integer text].exclude?(enum.datatype)
|
|
@errors << "Enum '#{enum.name}' has invalid datatype '#{enum.datatype}'"
|
|
end
|
|
|
|
expected_type = enum.datatype == :integer ? Integer : String
|
|
invalid_values = enum.values.reject { |_, value| value.is_a?(expected_type) }
|
|
if invalid_values.any?
|
|
@errors << "Enum '#{enum.name}' has values that do not match datatype '#{enum.datatype}'"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|