mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
98533f3dbe
The settings for the database were one flat record whose url and pool size
were meaningful only for Postgres, so every reader had to re-check the URL.
They are now a discriminated union per engine (DbEngineSettings): the
Postgres arm carries its URL and a pool size already capped at the ceiling,
and the SQLite arm carries nothing else. The env schema uses the same ceiling
constant, so the boot check and the derivation cannot drift apart.
The engine was resolved from the environment by every caller that needed it
(the lifecycle, the unit of work, the backup adapters), each building its own
instance. The engine is now provided once per container by
DatabaseEngineModule and injected everywhere, so the state an engine keeps
(Postgres's reopen hook) is the same wherever it is used. resolveEngine stays
for the MikroORM CLI config and the DI factory, and nothing else calls it.
The engine's reopen no longer carries a `reopenSwapsConnection` flag. The
engine itself decides what a reopen needs: SQLite closes and reconnects the
ORM's connection before the schema bootstrap (its handle is swapped under
Kysely's cache), and Postgres only migrates, because a restore replaces the
contents of the database the pool already reaches. The Postgres pool is never
closed on a reopen, since ending it waits for connections lent out during the
restore.
The backup ports answer every method the same way. verify() now throws a
DomainError for a refused archive on both engines, and the restore path maps
it back to the same { success: false, error, status } result, so the HTTP
response of a refused restore (status and body) is unchanged. The restore
step that swaps the database and then always reopens it is shared in
nest/backup/database-swap.ts, with the error-description helper in
nest/common, instead of two copies that could drift.
106 lines
5.0 KiB
TypeScript
106 lines
5.0 KiB
TypeScript
/**
|
|
* Regression test for Plan 3b Task 1's B1 finding
|
|
* (`.superpowers/sdd/2026-09-22-orm-phase3b/task-1-review.md`): a `Users`
|
|
* row read with `fields` + `refresh: true` (no `disableIdentityMap`) left a
|
|
* *partially hydrated, managed* entity in the request `EntityManager`'s
|
|
* identity map. A second such read, with a *different* field set, on the
|
|
* *same* row re-snapshotted only its own fields — the first read's fields
|
|
* stayed on the entity object but fell out of the snapshot MikroORM's
|
|
* change-set computer diffs against, so they looked like a pending change.
|
|
* The next `flush()` (including the implicit one at `UnitOfWork.transactional`
|
|
* commit) then emitted an `UPDATE users SET <those columns> …` carrying the
|
|
* STALE pre-request values, landing *after* an intervening `nativeUpdate`
|
|
* inside the same transaction and silently reverting it.
|
|
*
|
|
* No single-method test can catch this by construction (`Users.repository.test.ts`
|
|
* exercises one method per case): this file drives the two-reads-then-a-write
|
|
* interaction that is the actual trigger, inside `withRequestContext` (every
|
|
* authenticated request's shape) and `uow.transactional` (every multi-write
|
|
* request's shape).
|
|
*
|
|
* The fix (this task): every row-out read passes `disableIdentityMap: true`.
|
|
* MikroORM forks the EM with `keepTransactionContext: true`, loads inside the
|
|
* fork, then clears it — nothing is added to the *request's* identity map, so
|
|
* there is nothing for a later `flush()` to find dirty.
|
|
* See https://mikro-orm.io/docs/entity-manager#disableidentitymap and
|
|
* `node_modules/@mikro-orm/core/EntityManager.js:807-824`.
|
|
*
|
|
* Mutation proof (recorded in `task-1-fix-report.md`, not re-run here): reverting
|
|
* ONE of the two reads below to the pre-fix shape (`fields` + `refresh: true`,
|
|
* no `disableIdentityMap`) makes IMWB-001 fail with the stale write-back this
|
|
* test is built to catch.
|
|
*/
|
|
import { SqliteEngine } from '../../../../src/db/engine/sqlite/sqlite-engine';
|
|
import { Users } from '../../../../src/db/entities/Users.entity';
|
|
import type { UsersRepository } from '../../../../src/db/repositories/Users.repository';
|
|
import { withRequestContext } from '../../../../src/nest/database/request-context';
|
|
import { UnitOfWork } from '../../../../src/nest/database/unit-of-work';
|
|
import { createSnapshotTestDb } from '../../../helpers/db-mock';
|
|
import { createUser } from '../../../helpers/factories';
|
|
import { readUser } from '../../../helpers/factories/users';
|
|
import { resetTestDb } from '../../../helpers/test-db';
|
|
import { createTestOrm, type TestOrm } from '../../../helpers/test-orm';
|
|
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
|
|
|
const testDb = createSnapshotTestDb();
|
|
let t: TestOrm;
|
|
let users: UsersRepository;
|
|
let uow: UnitOfWork;
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestOrm(testDb);
|
|
users = t.repo(Users);
|
|
uow = new UnitOfWork(t.em, new SqliteEngine());
|
|
});
|
|
beforeEach(() => {
|
|
resetTestDb(testDb);
|
|
t.clear();
|
|
});
|
|
afterAll(async () => {
|
|
await t.close();
|
|
testDb.close();
|
|
});
|
|
|
|
describe('identity-map write-back (task-1-review.md B1)', () => {
|
|
it('IMWB-001: findByIdWithPasswordVersion then getApiKeyColumns then patchProfile — the profile write survives the request', async () => {
|
|
const { user } = createUser(testDb, { username: 'before', email: 'before@example.test' });
|
|
const originalPasswordVersion = (await readUser(t, user.id)).password_version;
|
|
|
|
await withRequestContext(t.orm, async () => {
|
|
// Projection A — the JwtAuthGuard/GlobalAuthGuard shape.
|
|
await users.findByIdWithPasswordVersion(user.id);
|
|
// Projection B — a second UsersRepository read with a different field set,
|
|
// the UserProfileService shape (e.g. the MfaPolicyGuard/getApiKeyColumns
|
|
// reads that run on the same request).
|
|
await users.getApiKeyColumns(user.id);
|
|
// The intended write, inside the request's own transaction.
|
|
await uow.transactional(async () => {
|
|
await users.patchProfile(user.id, { username: 'newname' });
|
|
});
|
|
});
|
|
|
|
const row = await readUser(t, user.id);
|
|
expect(row.username).toBe('newname'); // the nativeUpdate must stick
|
|
expect(row.email).toBe('before@example.test'); // untouched by either read
|
|
expect(row.password_version).toBe(originalPasswordVersion); // untouched by either read
|
|
});
|
|
|
|
it('IMWB-002: findByIdWithPasswordVersion then getApiKeyColumns then setPassword — password_version survives (Task 5 session-invalidation case)', async () => {
|
|
const { user } = createUser(testDb, { username: 'someone' });
|
|
|
|
await withRequestContext(t.orm, async () => {
|
|
await users.findByIdWithPasswordVersion(user.id);
|
|
await users.getApiKeyColumns(user.id);
|
|
await uow.transactional(async () => {
|
|
await users.setPassword(user.id, 'NEWHASH', 5);
|
|
});
|
|
});
|
|
|
|
const row = await readUser(t, user.id);
|
|
expect(row.password_hash).toBe('NEWHASH');
|
|
expect(row.password_version).toBe(5); // the session-invalidation gate — must NOT revert to the pre-change value
|
|
expect(row.username).toBe('someone'); // untouched by either read
|
|
});
|
|
});
|