Files
TREK/server/tests/postgres/schema-gaps.test.ts
T
Maurice 771dab7c35 feat(db): name the tables and columns the database lacks on every start
After the migrations, both engines compare the declared entities with the
live schema in one catalog statement and log a warning naming each table or
column that is missing, what fails because of it, and that it should be
reported. It never refuses a start, not even when the check itself fails,
and says nothing on a database the migrations built; a Postgres lane suite
holds that on a real server.
2026-11-18 15:22:38 +01:00

48 lines
1.8 KiB
TypeScript

/**
* PGGAP-001..002: the boot's check for tables and columns the entities map
* (src/db/schema-gaps.ts) on the lane's real Postgres database. Every
* Postgres start runs it, so a catalog that reads differently from the
* entities would warn about a healthy install on every start; the SQLite
* suite (tests/unit/db/schema-gaps.test.ts) cannot see that.
*/
import { buildApp } from '../../src/bootstrap';
import { schemaGaps } from '../../src/db/schema-gaps';
import { withRequestContext } from '../../src/nest/database/request-context';
import { MikroORM } from '@mikro-orm/core';
import { Logger, type INestApplication } from '@nestjs/common';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
let app: INestApplication;
let orm: MikroORM;
let bootWarnings: string[];
beforeAll(async () => {
const warn = vi.spyOn(Logger.prototype, 'warn').mockImplementation(() => {});
try {
app = await buildApp();
} finally {
bootWarnings = warn.mock.calls.map(([line]) => String(line));
warn.mockRestore();
}
orm = app.get(MikroORM);
}, 60_000);
afterAll(async () => {
await app?.close();
});
describe('the schema gap check on Postgres', () => {
it('PGGAP-001: a bootstrapped database lacks nothing, and the start says nothing about it', async () => {
expect(await withRequestContext(orm, () => schemaGaps(orm))).toEqual([]);
expect(bootWarnings.filter((line) => line.includes('this TREK version uses'))).toEqual([]);
});
it('PGGAP-002: a column the database lacks is named by table and column', async () => {
await withRequestContext(orm, () =>
orm.em.getConnection().execute('alter table users drop column mfa_backup_codes'),
);
expect(await withRequestContext(orm, () => schemaGaps(orm))).toEqual(['users.mfa_backup_codes']);
});
});