mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-05 10:24:05 +00:00
feat(db): run TREK on PostgreSQL
TREK runs on PostgreSQL when TREK_DB_URL is set, and on SQLite as before otherwise: an engine port under src/db/engine, the dialect helpers under src/db/dialect, a Postgres baseline for the migrations up to the cut, pg_dump/pg_restore backups, a probe of every repository method against a real server and a Postgres test lane in CI.
This commit is contained in:
@@ -396,6 +396,34 @@ describe('deriveDb: the pre-migration snapshot', () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe('deriveDb: the engine', () => {
|
||||
it('DB-ENGINE-001: SQLite unless TREK_DB_URL names a database', () => {
|
||||
expect(deriveDb({})).toMatchObject({ engine: 'sqlite' });
|
||||
expect(deriveDb({ TREK_DB_URL: ' ' })).toMatchObject({ engine: 'sqlite' });
|
||||
expect(deriveDb({ TREK_DB_URL: ' postgres://u:p@db:5432/trek ' })).toMatchObject({
|
||||
engine: 'postgres',
|
||||
url: 'postgres://u:p@db:5432/trek',
|
||||
});
|
||||
});
|
||||
|
||||
it('DB-ENGINE-002: a pool of ten connections unless told otherwise, from 1 to 100', () => {
|
||||
const poolMaxOf = (raw: Record<string, string>) => {
|
||||
const db = deriveDb({ TREK_DB_URL: 'postgres://u:p@db:5432/trek', ...raw });
|
||||
return db.engine === 'postgres' ? db.poolMax : null;
|
||||
};
|
||||
expect(deriveDb({ TREK_DB_URL: 'postgres://u:p@db:5432/trek' })).toMatchObject({ engine: 'postgres' });
|
||||
expect(poolMaxOf({})).toBe(10);
|
||||
expect(poolMaxOf({ TREK_DB_POOL_MAX: '25' })).toBe(25);
|
||||
expect(poolMaxOf({ TREK_DB_POOL_MAX: '0' })).toBe(10);
|
||||
expect(poolMaxOf({ TREK_DB_POOL_MAX: '500' })).toBe(100);
|
||||
});
|
||||
|
||||
it('DB-ENGINE-003: no pool size on SQLite, which has no pool', () => {
|
||||
expect(deriveDb({ TREK_DB_POOL_MAX: '25' })).toMatchObject({ engine: 'sqlite' });
|
||||
expect(deriveDb({})).not.toHaveProperty('poolMax');
|
||||
});
|
||||
});
|
||||
|
||||
describe('deriveNet', () => {
|
||||
it('ALLOW_INTERNAL_NETWORK coerces the boolean-like family (ssrfGuard)', () => {
|
||||
expect(deriveNet({ ALLOW_INTERNAL_NETWORK: 'True' }).allowInternalNetwork).toBe(true);
|
||||
|
||||
@@ -341,3 +341,40 @@ describe('validateEnvAtBoot: the pre-migration snapshot switches', () => {
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('validateEnvAtBoot: the database engine', () => {
|
||||
afterEach(() => {
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
it('DB-BOOT-001: refuses a TREK_DB_URL that is not a postgres:// or postgresql:// URL, without printing it', () => {
|
||||
for (const value of ['mysql://u:secret@db/trek', 'not a url', 'sqlite:///data/travel.db']) {
|
||||
const { printed, thrown } = bootReport({ TREK_DB_URL: value });
|
||||
expect(thrown, value).toMatch(/Invalid environment configuration/);
|
||||
expect(printed, value).toContain('TREK_DB_URL=***');
|
||||
expect(printed, value).not.toContain('secret');
|
||||
}
|
||||
});
|
||||
|
||||
it('DB-BOOT-002: masks TREK_DB_URL like every other secret', () => {
|
||||
expect(SECRET_ENV_KEYS.has('TREK_DB_URL')).toBe(true);
|
||||
});
|
||||
|
||||
it('DB-BOOT-003: refuses TREK_DB_URL together with a SQLite setting, naming both', () => {
|
||||
for (const key of ['TREK_DB_FILE', 'TREK_DB_JOURNAL_MODE', 'TREK_DB_SYNCHRONOUS']) {
|
||||
const { thrown, printed } = bootReport({ TREK_DB_URL: 'postgres://u:p@db/trek', [key]: 'x' });
|
||||
expect(thrown, key).toMatch(/Invalid environment configuration/);
|
||||
expect(printed, key).toContain('ambiguous database configuration');
|
||||
expect(printed, key).toContain(key);
|
||||
}
|
||||
});
|
||||
|
||||
it('DB-BOOT-004: accepts a Postgres database on its own', () => {
|
||||
expect(() => validateEnvAtBoot({ TREK_DB_URL: 'postgresql://u:p@db:5432/trek?sslmode=require' })).not.toThrow();
|
||||
});
|
||||
|
||||
it('DB-BOOT-005: refuses a pool size outside 1 to 100', () => {
|
||||
expect(bootReport({ TREK_DB_POOL_MAX: '0' }).thrown).toMatch(/Invalid environment configuration/);
|
||||
expect(() => validateEnvAtBoot({ TREK_DB_POOL_MAX: '20' })).not.toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user