discourse/migrations/lib/database/adapter/postgres.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

78 lines
1.7 KiB
Ruby

# frozen_string_literal: true
require "pg"
module Migrations
module Database
module Adapter
class Postgres
def initialize(settings)
@connection = PG::Connection.new(settings)
@connection.type_map_for_results = PG::BasicTypeMapForResults.new(@connection)
@connection.field_name_type = :symbol
configure_connection
end
def exec(sql)
@connection.exec(sql)
end
def query(sql, *params)
@connection.send_query_params(sql, params)
@connection.set_single_row_mode
Enumerator.new do |y|
while (result = @connection.get_result)
result.stream_each { |row| y.yield(row) }
result.clear
end
end
end
def query_first_row(sql)
@connection.exec(sql).first
end
def query_value(sql, column = nil)
if (row = query_first_row(sql))
column ? row[column.to_sym] : row.values.first
else
nil
end
end
def count(sql)
query_value(sql).to_i
end
def close
unless @connection&.finished?
@connection.finish
@connection = nil
end
end
def reset
@connection.reset
configure_connection
end
def escape_string(str)
@connection.escape_string(str)
end
def encode_array(array)
@array_encoder ||= PG::TextEncoder::Array.new
@array_encoder.encode(array)
end
private
def configure_connection
@connection.exec("SET client_min_messages TO WARNING")
end
end
end
end
end