mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-12 16:44:46 +08:00
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
97 lines
3.1 KiB
Ruby
97 lines
3.1 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
RSpec.describe Migrations::Database::IntermediateDB do
|
|
before { reset_memoization(described_class, :@db) }
|
|
after { reset_memoization(described_class, :@db) }
|
|
|
|
def create_connection_double
|
|
connection = instance_double(Migrations::Database::Connection)
|
|
allow(connection).to receive(:insert)
|
|
allow(connection).to receive(:close)
|
|
connection
|
|
end
|
|
|
|
describe ".setup" do
|
|
it "works with `Migrations::Database::Connection`" do
|
|
Dir.mktmpdir do |storage_path|
|
|
db_path = File.join(storage_path, "test.db")
|
|
connection = Migrations::Database::Connection.new(path: db_path)
|
|
|
|
connection.db.execute("CREATE TABLE foo (id INTEGER)")
|
|
|
|
described_class.setup(connection)
|
|
described_class.insert("INSERT INTO foo (id) VALUES (?)", 1)
|
|
described_class.insert("INSERT INTO foo (id) VALUES (?)", 2)
|
|
|
|
expect(connection.db.query_splat("SELECT id FROM foo")).to contain_exactly(1, 2)
|
|
|
|
connection.close
|
|
end
|
|
end
|
|
|
|
it "works with `Migrations::Database::OfflineConnection`" do
|
|
connection = Migrations::Database::OfflineConnection.new
|
|
|
|
described_class.setup(connection)
|
|
described_class.insert("INSERT INTO foo (id, name) VALUES (?, ?)", 1, "Alice")
|
|
described_class.insert("INSERT INTO foo (id, name) VALUES (?, ?)", 2, "Bob")
|
|
|
|
expect(connection.parametrized_insert_statements).to eq(
|
|
[
|
|
["INSERT INTO foo (id, name) VALUES (?, ?)", [1, "Alice"]],
|
|
["INSERT INTO foo (id, name) VALUES (?, ?)", [2, "Bob"]],
|
|
],
|
|
)
|
|
|
|
connection.close
|
|
end
|
|
|
|
it "switches the connection" do
|
|
old_connection = create_connection_double
|
|
new_connection = create_connection_double
|
|
|
|
sql = "INSERT INTO foo (id) VALUES (?)"
|
|
|
|
described_class.setup(old_connection)
|
|
described_class.insert(sql, 1)
|
|
expect(old_connection).to have_received(:insert).with(sql, [1])
|
|
expect(new_connection).to_not have_received(:insert)
|
|
|
|
described_class.setup(new_connection)
|
|
described_class.insert(sql, 2)
|
|
expect(old_connection).to_not have_received(:insert).with(sql, [2])
|
|
expect(new_connection).to have_received(:insert).with(sql, [2])
|
|
end
|
|
|
|
it "closes a previous connection" do
|
|
old_connection = create_connection_double
|
|
new_connection = create_connection_double
|
|
|
|
described_class.setup(old_connection)
|
|
described_class.setup(new_connection)
|
|
expect(old_connection).to have_received(:close)
|
|
expect(new_connection).to_not have_received(:close)
|
|
end
|
|
end
|
|
|
|
context "with fake connection" do
|
|
let(:connection) { create_connection_double }
|
|
let!(:sql) { "INSERT INTO foo (id, name) VALUES (?, ?)" }
|
|
|
|
before { described_class.setup(connection) }
|
|
|
|
describe ".insert" do
|
|
it "calls `#insert` on the connection" do
|
|
described_class.insert(sql, 1, "Alice")
|
|
expect(connection).to have_received(:insert).with(sql, [1, "Alice"])
|
|
end
|
|
end
|
|
|
|
describe ".close" do
|
|
it "closes the underlying connection" do
|
|
described_class.close
|
|
expect(connection).to have_received(:close).with(no_args)
|
|
end
|
|
end
|
|
end
|
|
end
|