discourse/migrations/lib/uploader/uploads.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

82 lines
2 KiB
Ruby

# frozen_string_literal: true
module Migrations
module Uploader
class Uploads
attr_reader :settings, :databases
def initialize(settings)
@settings = settings
@databases = setup_databases
configure_services
end
def perform!
tasks = build_task_pipeline
tasks.each { |task| task.run!(databases, settings) }
ensure
cleanup_resources
end
def self.perform!(settings = {})
new(settings).perform!
end
private
def build_task_pipeline
[].tap do |tasks|
tasks << Tasks::Fixer if settings[:fix_missing]
tasks << Tasks::Uploader
tasks << Tasks::Optimizer if settings[:create_optimized_images]
end
end
def setup_databases
run_uploads_db_migrations
{
uploads_db: create_database_connection(:uploads),
intermediate_db: create_database_connection(:intermediate),
}
end
def create_database_connection(type)
path = type == :uploads ? settings[:output_db_path] : settings[:source_db_path]
# TODO: Using "raw" db connection here for now
# Investigate using Migrations::Database::IntermediateDB.setup(db)
# Should we have a Migrations::Database::UploadsDB.setup(db)?
Database.connect(path)
end
def run_uploads_db_migrations
Database.migrate(
settings[:output_db_path],
migrations_path: Database::UPLOADS_DB_SCHEMA_PATH,
)
end
def configure_services
configure_logging
configure_site_settings
end
def configure_logging
@original_exifr_logger = EXIFR.logger
# disable logging for EXIFR which is used by ImageOptim
EXIFR.logger = Logger.new(nil)
end
def configure_site_settings
SiteSettings.configure!(settings[:site_settings])
end
def cleanup_resources
databases.values.each(&:close)
EXIFR.logger = @original_exifr_logger
end
end
end
end