mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +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:
@@ -1,70 +0,0 @@
|
||||
/**
|
||||
* How the Postgres probe turns the entities into a Postgres schema
|
||||
* (scripts/pg-probe/schema.ts): SQLite-only defaults, TEXT timestamps, the
|
||||
* NOCASE collation, and splitting the generated DDL so one refused statement
|
||||
* does not take the rest with it.
|
||||
*/
|
||||
import {
|
||||
adjustColumns,
|
||||
NOCASE_COLLATION_SQL,
|
||||
PG_NOW_TEXT_DEFAULT,
|
||||
portableDefault,
|
||||
splitStatements,
|
||||
type ColumnMeta,
|
||||
} from '../../../../scripts/pg-probe/schema';
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
class DbTimestampType {}
|
||||
class TextType {}
|
||||
|
||||
describe('pg-probe schema', () => {
|
||||
it('PGPROBE-040: maps the SQLite clock defaults and leaves every other default alone', () => {
|
||||
expect(PG_NOW_TEXT_DEFAULT).toBe("to_char((CURRENT_TIMESTAMP AT TIME ZONE 'UTC'), 'YYYY-MM-DD HH24:MI:SS')");
|
||||
expect(portableDefault('CURRENT_TIMESTAMP')).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("(datetime('now'))")).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("datetime( 'now' )")).toBe(PG_NOW_TEXT_DEFAULT);
|
||||
expect(portableDefault("(strftime('%s','now'))")).toBe('CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)');
|
||||
for (const kept of ['0', '1', 'NULL', "'none'", "'#10b981'", "'{}'"]) expect(portableDefault(kept)).toBe(kept);
|
||||
});
|
||||
|
||||
it('PGPROBE-041: makes timestamp columns text, rewrites defaults and the NOCASE collation, and names what it changed', () => {
|
||||
const columns: ColumnMeta[] = [
|
||||
{
|
||||
name: 'created_at',
|
||||
customType: new DbTimestampType(),
|
||||
columnTypes: ['timestamptz'],
|
||||
defaultRaw: 'CURRENT_TIMESTAMP',
|
||||
},
|
||||
{ name: 'title', customType: new TextType(), columnTypes: ['text'], defaultRaw: "'x'" },
|
||||
{ name: 'stamp', columnTypes: ['int'], defaultRaw: "(strftime('%s','now'))" },
|
||||
{ name: 'region', columnTypes: ['text'], collation: 'NOCASE' },
|
||||
{ name: 'id', columnTypes: ['int'] },
|
||||
];
|
||||
expect(adjustColumns(columns)).toEqual(['created_at', 'stamp', 'region']);
|
||||
expect(columns[0]).toMatchObject({ columnTypes: ['text'], defaultRaw: PG_NOW_TEXT_DEFAULT });
|
||||
expect(columns[1]).toMatchObject({ columnTypes: ['text'], defaultRaw: "'x'" });
|
||||
expect(columns[2]).toMatchObject({
|
||||
columnTypes: ['int'],
|
||||
defaultRaw: 'CAST(EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) AS bigint)',
|
||||
});
|
||||
expect(columns[3]).toMatchObject({ collation: 'nocase' });
|
||||
expect(NOCASE_COLLATION_SQL).toContain('CREATE COLLATION IF NOT EXISTS nocase');
|
||||
});
|
||||
|
||||
it('PGPROBE-042: splits DDL on semicolons outside quotes and drops session settings', () => {
|
||||
const ddl = [
|
||||
"set names 'utf8';",
|
||||
'create table "a" ("id" serial primary key, "note" text default \'a;b\');',
|
||||
'create index "a_x" on "a" ("note") where "note" <> \';\';',
|
||||
'',
|
||||
'create table "b ;" ("id" int)',
|
||||
].join('\n');
|
||||
expect(splitStatements(ddl)).toEqual([
|
||||
'create table "a" ("id" serial primary key, "note" text default \'a;b\')',
|
||||
'create index "a_x" on "a" ("note") where "note" <> \';\'',
|
||||
'create table "b ;" ("id" int)',
|
||||
]);
|
||||
expect(splitStatements(' ; ; ')).toEqual([]);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user