discourse/migrations/lib/importer/steps/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

72 lines
2.2 KiB
Ruby
Vendored

# frozen_string_literal: true
module Migrations
module Importer
module Steps
class Uploads < CopyStep
depends_on :users
store_mapped_ids true
requires_set :existing_sha1s, "SELECT sha1 FROM uploads"
column_names %i[
user_id
original_filename
filesize
width
height
url
created_at
updated_at
sha1
origin
retain_hours
extension
thumbnail_width
thumbnail_height
etag
secure
access_control_post_id
original_sha1
animated
verification_status
security_last_changed_at
security_last_changed_reason
dominant_color
]
total_rows_query <<~SQL, MappingType::UPLOADS
SELECT COUNT(*)
FROM files.uploads up
LEFT JOIN mapped.ids mup ON up.id = mup.original_id AND mup.type = ?
WHERE up.upload IS NOT NULL
AND mup.original_id IS NULL
SQL
rows_query <<~SQL, MappingType::USERS, MappingType::UPLOADS, Discourse::SYSTEM_USER_ID
SELECT up.id, up.upload, COALESCE(mu.discourse_id, ?3) AS user_id
FROM files.uploads up
JOIN uploads xup ON up.id = xup.id
LEFT JOIN mapped.ids mu ON xup.user_id = mu.original_id AND mu.type = ?1
LEFT JOIN mapped.ids mup ON up.id = mup.original_id AND mup.type = ?2
WHERE up.upload IS NOT NULL
AND mup.original_id IS NULL
ORDER BY up.ROWID
SQL
private
def transform_row(row)
upload = JSON.parse(row[:upload], symbolize_names: true)
return nil unless @existing_sha1s.add?(upload[:sha1])
upload[:original_id] = row[:id]
upload.delete(:id)
upload[:user_id] = row[:user_id]
super(upload)
end
end
end
end
end