mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 16:10:07 +00:00
test(db): the Postgres engine migrates in a context of its own
SonarCloud counted the two new lines in PostgresEngine.schemaBootstrap as uncovered: the Postgres lane runs them but does not feed the coverage it reads. PGENGINE-BOOT-001/002 run the bootstrap against a stand-in ORM whose first statement records the request context it runs in, on its own and inside a caller's request.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
/**
|
||||
* PostgresEngine.schemaBootstrap: the migrations run in a request context of
|
||||
* their own (#3264). A restore migrates inside the admin's HTTP request, and a
|
||||
* migration pins its transaction on the EntityManager of the context it runs
|
||||
* in; run in the request's own context, every later read of that request
|
||||
* failed with "Transaction is already committed".
|
||||
*
|
||||
* The ORM is a stand-in whose connection notes the context its first statement
|
||||
* runs in and stops there; the real Postgres migration runs in the Postgres
|
||||
* lane (tests/postgres).
|
||||
*/
|
||||
import { PostgresEngine } from '../../../../../src/db/engine/postgres/postgres-engine';
|
||||
import type { PostgresBootstrapDeps } from '../../../../../src/db/engine/postgres/schema-bootstrap';
|
||||
import type { AnyOrm } from '../../../../../src/db/orm';
|
||||
import { RequestContext } from '@mikro-orm/core';
|
||||
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const SETTINGS = { engine: 'postgres' as const, url: 'postgres://trek:pw@db/trek', poolMax: 10 };
|
||||
const DEPS = {} as PostgresBootstrapDeps;
|
||||
|
||||
/** An ORM whose EntityManager forks one recognisable EntityManager, and whose first statement records its context. */
|
||||
function ormStub() {
|
||||
const seen: { em: unknown } = { em: 'no statement ran' };
|
||||
const fork = { name: 'default', kind: 'the migration context' };
|
||||
const connection = {
|
||||
execute: vi.fn(async () => {
|
||||
seen.em = RequestContext.getEntityManager();
|
||||
throw new Error('stop after the first statement');
|
||||
}),
|
||||
};
|
||||
const em = { name: 'default', fork: vi.fn(() => fork), getConnection: () => connection };
|
||||
const orm = { em, config: { getExtension: () => ({}) } } as unknown as AnyOrm;
|
||||
return { orm, em, fork, seen };
|
||||
}
|
||||
|
||||
describe('PostgresEngine.schemaBootstrap', () => {
|
||||
it('PGENGINE-BOOT-001: migrates in a request context of its own (#3264)', async () => {
|
||||
const { orm, em, fork, seen } = ormStub();
|
||||
const engine = new PostgresEngine(SETTINGS, () => DEPS);
|
||||
|
||||
await expect(engine.schemaBootstrap(orm, { snapshot: true })).rejects.toThrow('stop after the first statement');
|
||||
|
||||
expect(em.fork).toHaveBeenCalledWith({ useContext: true });
|
||||
expect(seen.em).toBe(fork);
|
||||
// Nothing of that context is left once the bootstrap returns.
|
||||
expect(RequestContext.getEntityManager()).toBeUndefined();
|
||||
});
|
||||
|
||||
it("PGENGINE-BOOT-002: inside a caller's request, the migrations still get a context of their own", async () => {
|
||||
const { orm, fork, seen } = ormStub();
|
||||
const engine = new PostgresEngine(SETTINGS, () => DEPS);
|
||||
const callers = { name: 'default', kind: "the restore request's", fork: () => callers };
|
||||
|
||||
await RequestContext.create(callers as never, async () => {
|
||||
await expect(engine.schemaBootstrap(orm, { snapshot: false })).rejects.toThrow();
|
||||
expect(seen.em).toBe(fork);
|
||||
expect(seen.em).not.toBe(callers);
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user