Add MySQL support to Lib\Db via multiple simultaneous named connections

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.
This commit is contained in:
code
2026-07-14 03:53:27 +00:00
parent a3b996719a
commit 3a59269eaa
8 changed files with 339 additions and 155 deletions
+22 -10
View File
@@ -39,14 +39,26 @@ return [
'admin_username' => 'admin',
'admin_password_hash' => '',
// Lib\Db (see /admin/docs/database). Only 'sqlite' is implemented today
// — MySQL support is tracked as a separate Backlog item in
// novaconium/ISSUES.md. db_path deliberately lives outside both
// public/ (must never be web-accessible) and novaconium/ (gets wholly
// replaced on a framework update — see /admin/docs/getting-started's
// "Updating the framework" section) — a top-level data/ directory,
// project-owned like App/, is the only safe place for it.
'db_driver' => 'sqlite',
'db_path' => __DIR__ . '/../data/novaconium.sqlite',
'db_migrations_dir' => __DIR__ . '/../App/migrations',
// Lib\Db (see /admin/docs/database) — named, simultaneously-usable
// connections, keyed by name; 'default' is the only one required. A
// sidecar can use more than one at once, e.g. Db::query(...) (default)
// alongside Db::query(..., 'legacy'). Supported drivers: 'sqlite',
// 'mysql'. The default connection's path deliberately lives outside
// both public/ (must never be web-accessible) and novaconium/ (gets
// wholly replaced on a framework update — see
// /admin/docs/getting-started's "Updating the framework" section) — a
// top-level data/ directory, project-owned like App/, is the only safe
// place for it. migrations_dir is optional per connection; omit it to
// never run migrations against that connection (e.g. a read-only
// legacy database). NOTE: unlike every other key here, App/config.php
// merges into db_connections one level deeper than a normal shallow
// override — see the comment on Lib\Db::config() — so adding a second
// connection there doesn't require repeating 'default'.
'db_connections' => [
'default' => [
'driver' => 'sqlite',
'path' => __DIR__ . '/../data/novaconium.sqlite',
'migrations_dir' => __DIR__ . '/../App/migrations',
],
],
];