mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-02 00:05:39 +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
89 lines
1.9 KiB
Ruby
89 lines
1.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "date"
|
|
require "extralite"
|
|
require "ipaddr"
|
|
require "oj"
|
|
|
|
module Migrations
|
|
module Database
|
|
INTERMEDIATE_DB_SCHEMA_PATH = File.join(Migrations.root_path, "db", "intermediate_db_schema")
|
|
MAPPINGS_DB_SCHEMA_PATH = File.join(Migrations.root_path, "db", "mappings_db_schema")
|
|
UPLOADS_DB_SCHEMA_PATH = File.join(Migrations.root_path, "db", "uploads_db_schema")
|
|
|
|
def self.migrate(db_path, migrations_path:)
|
|
Migrator.new(db_path).migrate(migrations_path)
|
|
end
|
|
|
|
def self.reset!(db_path)
|
|
Migrator.new(db_path).reset!
|
|
end
|
|
|
|
def self.connect(path)
|
|
connection = Connection.new(path:)
|
|
return connection unless block_given?
|
|
|
|
begin
|
|
yield(connection)
|
|
ensure
|
|
connection.close
|
|
end
|
|
nil
|
|
end
|
|
|
|
def self.schema_path(type)
|
|
case type
|
|
when "intermediate_db"
|
|
INTERMEDIATE_DB_SCHEMA_PATH
|
|
when "mappings_db"
|
|
MAPPINGS_DB_SCHEMA_PATH
|
|
when "uploads_db"
|
|
UPLOADS_DB_SCHEMA_PATH
|
|
else
|
|
raise "Unknown type: #{type}"
|
|
end
|
|
end
|
|
|
|
def self.format_datetime(value)
|
|
value&.utc&.iso8601
|
|
end
|
|
|
|
def self.format_date(value)
|
|
value&.to_date&.iso8601
|
|
end
|
|
|
|
def self.format_boolean(value)
|
|
return nil if value.nil?
|
|
value ? 1 : 0
|
|
end
|
|
|
|
def self.format_ip_address(value)
|
|
return nil if value.blank?
|
|
IPAddr.new(value).to_s
|
|
rescue ArgumentError
|
|
nil
|
|
end
|
|
|
|
def self.to_blob(value)
|
|
return nil if value.blank?
|
|
Extralite::Blob.new(value)
|
|
end
|
|
|
|
def self.to_json(value)
|
|
return nil if value.nil?
|
|
Oj.dump(value, mode: :compat)
|
|
end
|
|
|
|
def self.to_date(text)
|
|
text.present? ? Date.parse(text) : nil
|
|
end
|
|
|
|
def self.to_datetime(text)
|
|
text.present? ? DateTime.parse(text) : nil
|
|
end
|
|
|
|
def self.to_boolean(value)
|
|
value == 1
|
|
end
|
|
end
|
|
end
|