mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-01 11:47:16 +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
119 lines
2.9 KiB
Ruby
119 lines
2.9 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require "extralite"
|
|
|
|
module Migrations
|
|
module Database
|
|
class Connection
|
|
TRANSACTION_BATCH_SIZE = 1000
|
|
PREPARED_STATEMENT_CACHE_SIZE = 5
|
|
|
|
def self.open_database(path:)
|
|
path = File.expand_path(path, Migrations.root_path)
|
|
FileUtils.mkdir_p(File.dirname(path))
|
|
|
|
db = Extralite::Database.new(path)
|
|
db.pragma(
|
|
busy_timeout: 60_000, # 60 seconds
|
|
journal_mode: "wal",
|
|
synchronous: "off",
|
|
temp_store: "memory",
|
|
locking_mode: "normal",
|
|
cache_size: -10_000, # 10_000 pages
|
|
)
|
|
db
|
|
end
|
|
|
|
attr_reader :db, :path
|
|
|
|
def initialize(path:, transaction_batch_size: TRANSACTION_BATCH_SIZE)
|
|
@path = File.expand_path(path, Migrations.root_path)
|
|
@transaction_batch_size = transaction_batch_size
|
|
@db = self.class.open_database(path:)
|
|
@statement_counter = 0
|
|
@statement_cache = PreparedStatementCache.new(PREPARED_STATEMENT_CACHE_SIZE)
|
|
|
|
@fork_hooks = setup_fork_handling
|
|
end
|
|
|
|
def close
|
|
close_connection(keep_path: false)
|
|
|
|
before_hook, after_hook = @fork_hooks
|
|
ForkManager.remove_before_fork_hook(before_hook)
|
|
ForkManager.remove_after_fork_parent_hook(after_hook)
|
|
end
|
|
|
|
def closed?
|
|
@db.nil? || @db.closed?
|
|
end
|
|
|
|
def insert(sql, parameters = [])
|
|
begin_transaction if @statement_counter == 0
|
|
|
|
stmt = @statement_cache.getset(sql) { @db.prepare(sql) }
|
|
stmt.execute(parameters)
|
|
|
|
if (@statement_counter += 1) >= @transaction_batch_size
|
|
commit_transaction
|
|
end
|
|
|
|
nil
|
|
end
|
|
|
|
def query(sql, *parameters, &block)
|
|
@db.query(sql, *parameters, &block)
|
|
end
|
|
|
|
def query_array(sql, *parameters, &block)
|
|
@db.query_array(sql, *parameters, &block)
|
|
end
|
|
|
|
def query_value(sql, *parameters)
|
|
@db.query_single_splat(sql, *parameters)
|
|
end
|
|
|
|
def count(sql, *parameters)
|
|
query_value(sql, *parameters)
|
|
end
|
|
|
|
def execute(sql, *parameters)
|
|
@db.execute(sql, *parameters)
|
|
end
|
|
|
|
def begin_transaction
|
|
@db.execute("BEGIN DEFERRED TRANSACTION") unless @db.transaction_active?
|
|
end
|
|
|
|
def commit_transaction
|
|
if @db.transaction_active?
|
|
@db.execute("COMMIT")
|
|
@statement_counter = 0
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def close_connection(keep_path:)
|
|
return if @db.nil?
|
|
|
|
commit_transaction
|
|
@statement_cache.clear
|
|
@db.close
|
|
|
|
@path = nil unless keep_path
|
|
@db = nil
|
|
@statement_counter = 0
|
|
end
|
|
|
|
def setup_fork_handling
|
|
before_hook = ForkManager.before_fork { close_connection(keep_path: true) }
|
|
|
|
after_hook =
|
|
ForkManager.after_fork_parent { @db = self.class.open_database(path: @path) if @path }
|
|
|
|
[before_hook, after_hook]
|
|
end
|
|
end
|
|
end
|
|
end
|