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

41 lines
1.3 KiB
Ruby

# frozen_string_literal: true
require "extralite"
RSpec.describe Migrations::Database::PreparedStatementCache do
let(:cache) { described_class.new(3) }
def create_statement_double
instance_double(Extralite::Query, close: nil)
end
it "should inherit behavior from LruRedux::Cache" do
expect(described_class).to be < LruRedux::Cache
end
it "closes the statement when an old entry is removed" do
cache["a"] = a_statement = create_statement_double
cache["b"] = b_statement = create_statement_double
cache["c"] = c_statement = create_statement_double
# this should remove the oldest entry "a" from the cache and call #close on the statement
cache["d"] = d_statement = create_statement_double
expect(a_statement).to have_received(:close)
expect(b_statement).not_to have_received(:close)
expect(c_statement).not_to have_received(:close)
expect(d_statement).not_to have_received(:close)
end
it "closes all statements when the cache is cleared" do
cache["a"] = a_statement = create_statement_double
cache["b"] = b_statement = create_statement_double
cache["c"] = c_statement = create_statement_double
cache.clear
expect(a_statement).to have_received(:close)
expect(b_statement).to have_received(:close)
expect(c_statement).to have_received(:close)
end
end