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

86 lines
2.3 KiB
Ruby

# frozen_string_literal: true
module Migrations
module Database
class Migrator
def initialize(db_path)
@db_path = File.expand_path(db_path, Migrations.root_path)
@db = nil
end
def migrate(migrations_path)
@migrations_path = migrations_path
@db = Connection.open_database(path: @db_path)
if new_database?
create_schema_migrations_table
performed_migrations = Set.new
else
performed_migrations = find_performed_migrations
end
migrate_from_path(@migrations_path, performed_migrations)
@db.close
nil
end
def reset!
[@db_path, "#{@db_path}-wal", "#{@db_path}-shm"].each do |path|
FileUtils.remove_file(path, force: true) if File.exist?(path)
end
nil
end
private
def new_database?
@db.query_single_splat(<<~SQL) == 0
SELECT COUNT(*)
FROM sqlite_schema
WHERE type = 'table' AND name = 'schema_migrations'
SQL
end
def find_performed_migrations
@db.query_splat(<<~SQL).to_set
SELECT path
FROM schema_migrations
SQL
end
def create_schema_migrations_table
@db.execute(<<~SQL)
CREATE TABLE schema_migrations
(
path TEXT NOT NULL PRIMARY KEY,
created_at DATETIME NOT NULL,
sql_hash TEXT NOT NULL
);
SQL
end
def migrate_from_path(migration_path, performed_migrations)
file_pattern = File.join(migration_path, "*.sql")
root_path = @migrations_path || Migrations.root_path
Dir[file_pattern].sort.each do |path|
relative_path = Pathname(path).relative_path_from(root_path).to_s
if performed_migrations.exclude?(relative_path)
sql = File.read(path)
sql_hash = Digest::SHA1.hexdigest(sql)
@db.transaction do
@db.execute(sql)
@db.execute(<<~SQL, path: relative_path, sql_hash:)
INSERT INTO schema_migrations (path, created_at, sql_hash)
VALUES (:path, datetime('now'), :sql_hash)
SQL
end
end
end
end
end
end
end