mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-22 13:10:29 +00:00
feat(db): create and migrate the schema on a Postgres database
With TREK_DB_URL set, TREK now boots on Postgres; without it nothing changes. - The database engine contract gains open, close, reopen and the schema bootstrap; DatabaseLifecycle delegates to the engine resolveEngine() picks. - An empty Postgres database gets server/assets/postgres/baseline.sql (generated from the entities by gen:pg-baseline, with the reference rows the migrations up to its cut insert: tour types, provider catalogues, addons, app settings) and every migration up to its cut recorded as executed, in one transaction. A database holding tables without TREK's history is refused before anything is written; a migrated one runs the newer-database refusal and its pending migrations, which refuse without a copy unless TREK_DB_PRE_MIGRATE_SNAPSHOT=false. - INTEGER columns are bigint on Postgres (SQLite's INTEGER is 64-bit: an epoch-millisecond column overflowed int4), and the engine's driver reads bigint and numeric back as numbers, refusing values past 2^53. - The two SQLite triggers are ported to PL/pgSQL; check:pg-baseline fails while a SQLite trigger has no port, and while the baseline drifts from the entities before a later migration freezes it. - Backups and restores on Postgres refuse until they can dump and load one. - Migration helpers read the catalog per engine (dialect/catalog.ts), and the email case index no longer needs group_concat. - CI runs check:pg-baseline and boots the image on postgres:17 twice.
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* The Postgres driver reads `bigint` as a number, on its own pool only
|
||||
* (src/db/engine/postgres/postgres-driver.ts). Nothing here connects.
|
||||
*/
|
||||
import { parseInt8, parseNumeric, TrekPostgreSqlDriver } from '../../../../../src/db/engine/postgres/postgres-driver';
|
||||
import { Configuration } from '@mikro-orm/core';
|
||||
import type { PostgreSqlConnection } from '@mikro-orm/postgresql';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
type Parsers = { getTypeParser(oid: number): (text: string) => unknown };
|
||||
|
||||
describe('postgres driver', () => {
|
||||
it('PGDRIVER-001: a bigint is a number up to 2^53 and refused past it, never rounded', () => {
|
||||
expect(parseInt8('0')).toBe(0);
|
||||
expect(parseInt8('1793636850006')).toBe(1793636850006);
|
||||
expect(parseInt8('-42')).toBe(-42);
|
||||
expect(() => parseInt8('9007199254740993')).toThrow(/beyond what TREK reads exactly/);
|
||||
});
|
||||
|
||||
it('PGDRIVER-003: a numeric (sum of a bigint, avg) is a number like SQLite answers, an integral one past 2^53 refused', () => {
|
||||
expect(parseNumeric('4200')).toBe(4200);
|
||||
expect(parseNumeric('2.5000000000000000')).toBe(2.5);
|
||||
expect(parseNumeric('-7')).toBe(-7);
|
||||
expect(() => parseNumeric('9007199254740993')).toThrow(/beyond what TREK reads exactly/);
|
||||
expect(() => parseNumeric('NaN')).toThrow(/beyond what TREK reads exactly/);
|
||||
});
|
||||
|
||||
it("PGDRIVER-002: the pool parses int8 as a number and keeps MikroORM's text dates", () => {
|
||||
const config = new Configuration(
|
||||
{
|
||||
driver: TrekPostgreSqlDriver,
|
||||
clientUrl: 'postgresql://u@localhost/trek',
|
||||
entities: [],
|
||||
discovery: { warnWhenNoEntities: false },
|
||||
},
|
||||
false,
|
||||
);
|
||||
const driver = new TrekPostgreSqlDriver(config);
|
||||
const options = (driver.getConnection() as PostgreSqlConnection).mapOptions({});
|
||||
const types = options.types as unknown as Parsers;
|
||||
expect(types.getTypeParser(20)('12')).toBe(12);
|
||||
expect(types.getTypeParser(1700)('12.5')).toBe(12.5);
|
||||
expect(types.getTypeParser(1114)('2026-01-01 00:00:00')).toBe('2026-01-01 00:00:00');
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user