Redesigns Lib\Db from a single global connection to a config-driven map
of named connections (config['db_connections']), each independently
lazy-connected, each with its own optional migrations_dir and its own
schema_migrations table. A sidecar can use more than one connection in
the same request (e.g. Db::query(...) against SQLite alongside
Db::query(..., 'legacy') against MySQL) — sidecars have full access to
any Lib\ class, so nothing stops a request from needing two databases
at once, which the original single-driver spec didn't account for.
Db::query()/Db::connection() both default to the 'default' connection
name so the common single-database case is unchanged at the call site.
Supersedes the flat db_driver/db_path/db_migrations_dir keys shipped in
the SQLite groundwork commit (a3b9967) — no downstream consumers yet,
so no migration path needed.
db_connections needed one deliberate exception to the project's usual
shallow config-merge rule: merged one level deeper, by connection name,
so App/config.php adding a connection doesn't delete 'default'. Verified
end-to-end against a real local MariaDB instance running alongside the
existing SQLite connection, which caught a real ordering bug in the
initial merge implementation (capturing defaults after they'd already
been overwritten) before it shipped.
Closes the "MySQL support" backlog item in novaconium/ISSUES.md.
26 lines
877 B
PHP
26 lines
877 B
PHP
<?php
|
|
|
|
use Lib\Db;
|
|
|
|
require __DIR__ . '/../autoload.php';
|
|
|
|
// Db::connection() applies any pending migrations for that connection as a
|
|
// side effect of opening it (see Lib\Db::migrate()) — this script triggers
|
|
// that explicitly for every configured connection, e.g. from a deploy
|
|
// script, without serving a request first.
|
|
$config = require __DIR__ . '/../config.php';
|
|
|
|
$appConfigFile = __DIR__ . '/../../App/config.php';
|
|
if (is_file($appConfigFile)) {
|
|
$appConfig = require $appConfigFile;
|
|
$defaultConnections = $config['db_connections'];
|
|
$appConnections = $appConfig['db_connections'] ?? [];
|
|
$config = array_merge($config, $appConfig);
|
|
$config['db_connections'] = array_merge($defaultConnections, $appConnections);
|
|
}
|
|
|
|
foreach (array_keys($config['db_connections']) as $name) {
|
|
Db::connection($name);
|
|
echo "Migrated connection '{$name}'.\n";
|
|
}
|