mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-06 05:24:51 +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
78 lines
1.8 KiB
Ruby
78 lines
1.8 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Migrations
|
|
module Importer
|
|
class SharedData
|
|
LOADERS = {
|
|
existing_usernames_lower: {
|
|
type: :set,
|
|
sql: <<~SQL,
|
|
SELECT username_lower
|
|
FROM users
|
|
SQL
|
|
},
|
|
existing_group_names_lower: {
|
|
type: :set,
|
|
sql: <<~SQL,
|
|
SELECT LOWER(name)
|
|
FROM groups
|
|
SQL
|
|
},
|
|
}.freeze
|
|
|
|
def initialize(discourse_db)
|
|
@discourse_db = discourse_db
|
|
@cache = {}
|
|
end
|
|
|
|
def load_set(sql)
|
|
result = @discourse_db.query_result(sql)
|
|
depth = result.column_count - 1
|
|
|
|
set = SetStore.create(depth)
|
|
set.bulk_add(result.rows)
|
|
|
|
set
|
|
end
|
|
|
|
def load_mapping(sql)
|
|
rows = @discourse_db.query_array(sql)
|
|
|
|
# While rows is an enumerator, it's not fully compliant, it does not
|
|
# rewind on #first, #peek, #any?, etc.
|
|
# So we need to hold on to first_row for use later
|
|
first_row = rows.first
|
|
|
|
return {} if first_row.nil?
|
|
|
|
has_multiple_values = first_row.size > 2
|
|
result =
|
|
if has_multiple_values
|
|
rows.to_h { |key, *values| [key, values] }
|
|
else
|
|
rows.to_h
|
|
end
|
|
|
|
result[first_row[0]] = has_multiple_values ? first_row[1..] : first_row[1]
|
|
result
|
|
end
|
|
|
|
def [](type)
|
|
@cache[type] ||= begin
|
|
loader_config = LOADERS[type]
|
|
raise "Unknown type: #{type}" unless loader_config
|
|
|
|
sql = loader_config[:sql]
|
|
case loader_config[:type]
|
|
when :set
|
|
load_set(sql)
|
|
when :mapping
|
|
load_mapping(sql)
|
|
else
|
|
raise "Unknown loader type: #{loader_config[:type]}"
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|