mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 05:26:47 +00:00
0a4c6fbace
db/database.ts opened the connection as a side effect of being imported, so the moment anything reached it the database was open. index.ts had to defer loading the app until a first-boot restore was done, and nothing in the container decided when the connection opened or closed. The module now opens the connection on first use and opens nothing at import. A connection closed on purpose (a restore, the shutdown) stays closed until it is reopened, as before. The new DatabaseLifecycle provider owns the decisions: buildApp() opens through it, which also binds the ORM to later swaps (the hook attachOrm used to register) and runs the schema bootstrap in the same order as before, and the shutdown closes through it. Its file property is the one resolved database path. The module keeps every export, so the test suites that replace it with vi.mock keep working; buildDbMock gains the new openDb.
40 lines
1.7 KiB
TypeScript
40 lines
1.7 KiB
TypeScript
import { execFileSync } from 'node:child_process';
|
|
import path from 'node:path';
|
|
|
|
/**
|
|
* Runs ONCE per `vitest run`, before any worker starts.
|
|
*
|
|
* `src/db/database.ts` used to build the schema itself at module load, so merely
|
|
* importing it handed a suite a migrated database. That is MikroORM's job now
|
|
* and its migrator is async, which the synchronous `openDb()` cannot await. This
|
|
* is the only hook in the test lifecycle that can: setup files are side-effect
|
|
* imports and every other hook is per-file.
|
|
*
|
|
* So one database is migrated here and serialised; each worker opens a copy.
|
|
* `better-sqlite3` deserialises the ~1.5 MiB snapshot in well under a
|
|
* millisecond, against ~800ms to replay 242 migrations, which is also what keeps
|
|
* the suite fast.
|
|
*
|
|
* Most suites never touch it — they mock `db/database` or build their own
|
|
* database with `createTables`/`runMigrations`. It is there for the handful that
|
|
* use the singleton directly.
|
|
*/
|
|
export async function setup(): Promise<void> {
|
|
const script = path.join(__dirname, 'build-schema-snapshot.ts');
|
|
// A child process under tsx, rather than importing the builder: globalSetup
|
|
// runs outside vitest's transform pipeline, and Node's own resolver cannot
|
|
// load these CommonJS-target modules' extensionless imports.
|
|
execFileSync(process.execPath, ['--import', 'tsx', script], {
|
|
cwd: path.join(__dirname, '..'),
|
|
stdio: ['ignore', 'inherit', 'inherit'],
|
|
env: {
|
|
...process.env,
|
|
NODE_ENV: 'test',
|
|
// The same fixed values tests/setup.ts pins, for the same reasons: the
|
|
// seeders encrypt with ENCRYPTION_KEY, and the migrations are chatty.
|
|
ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
|
|
LOG_LEVEL: 'error',
|
|
},
|
|
});
|
|
}
|