mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
feat(db): hold migrations after the Postgres cut to engineDdl in lint:dialect
A migration newer than the baseline's through (read from assets/postgres/baseline.json) fails on a schema statement outside engineDdl, a plain string column for addColumnIfMissing or a SQLite-only helper, and on a SQLite spelling in an engineDdl postgres member. The advice names the two steps, and the baseline cannot hold an allowance for these rules.
This commit is contained in:
@@ -23,12 +23,18 @@ interface ExecError {
|
||||
|
||||
const roots: string[] = [];
|
||||
|
||||
/** The Postgres baseline's cut every throwaway root gets unless a test writes its own manifest. */
|
||||
const THROUGH = 'Migration20200101046440_a_trip_owns_its_tags';
|
||||
const MANIFEST = 'assets/postgres/baseline.json';
|
||||
|
||||
function serverRoot(files: Record<string, string>, baseline: unknown = {}): string {
|
||||
// null writes no baseline file at all.
|
||||
const dir = mkdtempSync(path.join(tmpdir(), 'trek-dialect-lint-'));
|
||||
roots.push(dir);
|
||||
mkdirSync(path.join(dir, 'src'), { recursive: true });
|
||||
mkdirSync(path.join(dir, 'scripts'), { recursive: true });
|
||||
mkdirSync(path.join(dir, 'assets/postgres'), { recursive: true });
|
||||
writeFileSync(path.join(dir, MANIFEST), JSON.stringify({ through: THROUGH, migrations: [THROUGH] }));
|
||||
for (const [file, text] of Object.entries(files)) {
|
||||
mkdirSync(path.dirname(path.join(dir, file)), { recursive: true });
|
||||
writeFileSync(path.join(dir, file), text);
|
||||
@@ -113,8 +119,8 @@ describe('dialect-lint.mjs', () => {
|
||||
expect(run(dir).status).toBe(0);
|
||||
});
|
||||
|
||||
it('DIALECT-005: a newer migration is held to the DML rules but may spell its own DDL and PRAGMA', () => {
|
||||
const migration = 'src/db/migrations/Migration20990101000000_probe.ts';
|
||||
it('DIALECT-005: a newer migration up to the Postgres cut is held to the DML rules but may spell its own DDL', () => {
|
||||
const migration = 'src/db/migrations/Migration20200101043000_probe.ts';
|
||||
const ddl =
|
||||
"export const t = 'CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT)';\nexport const p = 'PRAGMA foreign_keys = OFF';\n";
|
||||
expect(run(serverRoot({ [migration]: ddl })).status).toBe(0);
|
||||
@@ -173,6 +179,103 @@ describe('dialect-lint.mjs', () => {
|
||||
expect(run(serverRoot(files, { [REPO]: { pragma: 0 } })).status).toBe(1);
|
||||
});
|
||||
|
||||
describe('a migration after the Postgres cut', () => {
|
||||
const POST_CUT = 'src/db/migrations/Migration20200101046450_probe.ts';
|
||||
const migration = (body: string) =>
|
||||
[
|
||||
"import { addColumnIfMissing, engineDdl, tableSql } from '../migration-utils';",
|
||||
'export class Probe {',
|
||||
' async up(this: never): Promise<void> {',
|
||||
body,
|
||||
' }',
|
||||
'}',
|
||||
'',
|
||||
].join('\n');
|
||||
const ROUTINE =
|
||||
'engineDdl(this, { sqlite: [...], postgres: [] }) from src/db/migration-utils.ts with the SQLite statements ' +
|
||||
"by hand (a column for addColumnIfMissing as { sqlite: '...', postgres: '...' }); (2) fill postgres with what " +
|
||||
'npm run gen:pg-migration prints';
|
||||
|
||||
it('DIALECT-011: passes schema statements written per engine through engineDdl and addColumnIfMissing', () => {
|
||||
const body = [
|
||||
' engineDdl(this, {',
|
||||
" sqlite: ['CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, at DATETIME DEFAULT CURRENT_TIMESTAMP)'],",
|
||||
' postgres: [\'create table "t" ("id" bigserial primary key, "at" text null)\'],',
|
||||
' });',
|
||||
" await addColumnIfMissing(this, 't', 'c', { sqlite: 'c TEXT', postgres: 'c text null' });",
|
||||
" this.addSql('UPDATE t SET c = ? WHERE c IS NULL');",
|
||||
].join('\n');
|
||||
const { status, out } = run(serverRoot({ [POST_CUT]: migration(body) }));
|
||||
expect(out).toBe('dialect: 0 file(s) hold 0 SQLite-only spelling(s) at their baseline\n');
|
||||
expect(status).toBe(0);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['a CREATE TABLE', "this.addSql('CREATE TABLE t (id INTEGER PRIMARY KEY)');"],
|
||||
['a CREATE INDEX', "this.addSql('CREATE UNIQUE INDEX IF NOT EXISTS i ON t (a)');"],
|
||||
['an ALTER TABLE', 'await this.execute(`ALTER TABLE t ADD COLUMN c TEXT`);'],
|
||||
['a DROP TRIGGER', "this.addSql('DROP TRIGGER IF EXISTS trg');"],
|
||||
['a CREATE VIEW', "this.addSql('CREATE VIEW v AS SELECT 1');"],
|
||||
['a RENAME', "this.addSql('ALTER TABLE t_new RENAME TO t');"],
|
||||
['a column as a plain string', "await addColumnIfMissing(this, 't', 'c', 'c TEXT');"],
|
||||
['a SQLite-only helper', "await tableSql(this, 't');"],
|
||||
])('DIALECT-012: fails %s outside engineDdl and says what to do', (_what, body) => {
|
||||
const { status, out } = run(serverRoot({ [POST_CUT]: migration(` ${body}`) }));
|
||||
expect(status).toBe(1);
|
||||
expect(out).toContain(`FAIL ${POST_CUT}: 1 schema statement(s) outside engineDdl, 0 allowed.`);
|
||||
expect(out).toContain(ROUTINE);
|
||||
});
|
||||
|
||||
it.each([
|
||||
['DATETIME', 'create table "t" ("at" DATETIME)'],
|
||||
['AUTOINCREMENT', 'create table "t" ("id" integer autoincrement)'],
|
||||
['INTEGER PRIMARY KEY', 'create table "t" ("id" INTEGER PRIMARY KEY)'],
|
||||
['WITHOUT ROWID', 'create table "t" ("id" text primary key) without rowid'],
|
||||
['PRAGMA', 'PRAGMA foreign_keys = OFF'],
|
||||
["datetime('now')", 'alter table "t" alter column "at" set default datetime(\'now\')'],
|
||||
])('DIALECT-013: fails %s in a postgres member', (_what, statement) => {
|
||||
const body = ` engineDdl(this, { sqlite: [], postgres: [${JSON.stringify(statement)}] });`;
|
||||
const { status, out } = run(serverRoot({ [POST_CUT]: migration(body) }));
|
||||
expect(status).toBe(1);
|
||||
expect(out).toContain(`FAIL ${POST_CUT}: 1 SQLite spelling(s) in an engineDdl postgres member, 0 allowed.`);
|
||||
expect(out).toContain('npm run gen:pg-migration');
|
||||
});
|
||||
|
||||
it("DIALECT-014: Postgres's own || in a postgres member and SQLite DDL in a sqlite member pass", () => {
|
||||
const body = [
|
||||
" engineDdl(this, { sqlite: ['CREATE TRIGGER t AFTER UPDATE ON a BEGIN SELECT 1; END'], postgres: [",
|
||||
" 'create function f() returns trigger language plpgsql as $$ begin new.a := new.b || new.c; return new; end $$',",
|
||||
' ] });',
|
||||
].join('\n');
|
||||
expect(run(serverRoot({ [POST_CUT]: migration(body) })).status).toBe(0);
|
||||
});
|
||||
|
||||
it('DIALECT-015: the cut is read from the Postgres manifest, and moves with it', () => {
|
||||
const body = " this.addSql('CREATE INDEX i ON t (a)');";
|
||||
const later = JSON.stringify({ through: 'Migration20200101046450_probe', migrations: [] });
|
||||
expect(run(serverRoot({ [POST_CUT]: migration(body), [MANIFEST]: later })).status).toBe(0);
|
||||
expect(run(serverRoot({ [POST_CUT]: migration(body) })).status).toBe(1);
|
||||
});
|
||||
|
||||
it('DIALECT-016: fails closed on a missing or malformed manifest, and on a baselined allowance', () => {
|
||||
const files = { [REPO]: 'export {};\n' };
|
||||
const missing = serverRoot(files);
|
||||
rmSync(path.join(missing, MANIFEST));
|
||||
const gone = run(missing);
|
||||
expect(gone.status).toBe(1);
|
||||
expect(gone.out).toContain('assets/postgres/baseline.json cannot be read');
|
||||
const broken = run(serverRoot({ ...files, [MANIFEST]: JSON.stringify({ through: 42 }) }));
|
||||
expect(broken.status).toBe(1);
|
||||
expect(broken.out).toContain('must name the last migration it stands for');
|
||||
|
||||
for (const rule of ['ddl', 'sqlite-ddl']) {
|
||||
const allowed = run(serverRoot(files, { [POST_CUT]: { [rule]: 1 } }));
|
||||
expect(allowed.status).toBe(1);
|
||||
expect(allowed.out).toContain(`holds an allowance for ${rule}`);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('DIALECT-010: fails closed when src/ is missing', () => {
|
||||
const dir = serverRoot({ [REPO]: 'export {};\n' });
|
||||
rmSync(path.join(dir, 'src'), { recursive: true });
|
||||
|
||||
Reference in New Issue
Block a user