discourse/migrations/lib/cli/upload_command.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

61 lines
1.6 KiB
Ruby

# frozen_string_literal: true
module Migrations
module CLI
class UploadCommand
def initialize(options)
@options = options
end
def execute
puts "Starting uploads..."
Migrations.load_rails_environment(quiet: true)
adjust_db_pool_size
validate_settings_file!
settings = load_settings
Uploader::Uploads.perform!(settings)
puts ""
end
private
def load_settings
settings = SettingsParser.parse!(@options.settings)
merge_settings_from_cli_args!(settings)
settings
end
def merge_settings_from_cli_args!(settings)
settings[:fix_missing] = options.fix_missing if @options.fix_missing.present?
settings[:create_optimized_images] = options.optimize if @options.optimize.present?
end
def validate_settings_file!
path = @options.settings
raise NoSettingsFound, "Settings file not found: #{path}" if !File.exist?(path)
end
def adjust_db_pool_size
max_db_connections = DB.query_single("SHOW max_connections").first.to_i
current_size = ActiveRecord::Base.connection_pool.size
if current_size < max_db_connections
db_config = ActiveRecord::Base.connection_db_config.configuration_hash.dup
db_config[:pool] = max_db_connections
ActiveRecord::Base.establish_connection(db_config)
puts "Adjusted DB pool size from #{current_size} to #{ActiveRecord::Base.connection_pool.size}"
else
puts "DB pool size: #{current_size} (max connections: #{max_db_connections})"
end
end
end
end
end