mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-20 11:06:06 +00:00
fix(db): hold SQLite-only functions, LIKE and the sqlite member of a post-cut migration in lint:dialect
lint:dialect missed several classes of SQL that SQLite reads and Postgres does not, or reads differently: - group_concat, ifnull, instr, iif, printf, typeof, total(), changes() and json_object/json_array/json_valid/json_type/json_quote, as calls in SQL text and as the bare name handed to eb.fn. - LIKE in SQL text, Kysely's 'like' operator and MikroORM's $like, which ignore case on SQLite only. A LIKE that is exact on purpose now says so in a like-exact comment; the six existing ones (setting keys, storage paths, the lowercased reference search, sqlite_master) carry it. - The sqlite member of engineDdl was not read at all, so a trigger or a data backfill put there never reached Postgres. It may now hold only the schema change and a rebuild's copy and sqlite_sequence bookkeeping, and SQLite statements with an empty postgres member need a sqlite-only reason. - execBestEffort, bestEffort and execUnlessTableMissing in a post-cut migration: on Postgres a swallowed failure aborts the migration transaction. - The post-cut rules follow POSTGRES_FIRST_CUT as well as the manifest, so regenerating the baseline over a new migration no longer releases it. src/db/engine/sqlite/ is SQLite's own code and is no longer scanned.
This commit is contained in:
@@ -90,6 +90,14 @@ describe('dialect-lint.mjs', () => {
|
||||
['concat', 'void "SELECT first || \' \' || last FROM users";'],
|
||||
['insert-id', 'declare const r: { insertId: bigint };\nvoid Number(r.insertId);'],
|
||||
['insert-id', 'declare const r: { insertId: bigint };\nconst { insertId } = r;\nvoid insertId;'],
|
||||
['json', "void 'SELECT json_object(?, a) FROM t';"],
|
||||
['sqlite-function', 'void "SELECT trip_id, group_concat(name, \', \') FROM places GROUP BY trip_id";'],
|
||||
['sqlite-function', "void 'SELECT ifnull(SUM(cost), 0) AS total FROM places WHERE trip_id = ?';"],
|
||||
['sqlite-function', "declare function fn(name: string, args: string[]): void;\nfn('ifnull', ['a', 'b']);"],
|
||||
['sqlite-function', "void 'SELECT total(cost) FROM places';"],
|
||||
['like', "declare function find(where: unknown): void;\nfind({ title: { $like: '%paris%' } });"],
|
||||
['like', "declare function where(c: string, op: string, v: string): void;\nwhere('title', 'like', '%paris%');"],
|
||||
['like', "void 'SELECT id FROM collections WHERE title LIKE ?';"],
|
||||
])('DIALECT-002: fails a new %s hit and names the rule', (rule, code) => {
|
||||
const { status, out } = run(serverRoot({ [REPO]: `${code}\n` }));
|
||||
expect(status).toBe(1);
|
||||
@@ -109,20 +117,49 @@ describe('dialect-lint.mjs', () => {
|
||||
expect(run(serverRoot({ 'src/nest/probe/probe.controller.ts': code })).status).toBe(0);
|
||||
});
|
||||
|
||||
it('DIALECT-017: leaves prose that reads like SQL alone, and a LIKE marked exact', () => {
|
||||
const prose = [
|
||||
'export const a = \'For a question like "what if it rains", look up the weather from the forecast\';',
|
||||
"export const b = 'Pass the total (the places the filter matches) and the changes (name, route)';",
|
||||
"export const c = { type: 'json_object' };",
|
||||
'',
|
||||
].join('\n');
|
||||
expect(run(serverRoot({ 'src/nest/probe/probe.mcp.ts': prose })).status).toBe(0);
|
||||
const exact = [
|
||||
'declare function find(where: unknown): void;',
|
||||
'// like-exact: setting keys match their exact prefix on both engines.',
|
||||
"find({ key: { $like: 'perm_%' } });",
|
||||
"find({ key: { $like: 'plugin:%' } }); // like-exact: a plugin's key prefix",
|
||||
'',
|
||||
].join('\n');
|
||||
expect(run(serverRoot({ [REPO]: exact })).status).toBe(0);
|
||||
// A marker without a reason excuses nothing.
|
||||
const bare = "declare function find(where: unknown): void;\n// like-exact:\nfind({ key: { $like: 'a%' } });\n";
|
||||
expect(run(serverRoot({ [REPO]: bare })).status).toBe(1);
|
||||
});
|
||||
|
||||
it('DIALECT-004: never scans the dialect layer or a migration that has shipped', () => {
|
||||
const dir = serverRoot({
|
||||
'src/db/dialect/sql-functions.ts': "export const now = `datetime('now')`;\nexport const s = 'strftime';\n",
|
||||
'src/db/dialect/kysely-functions.ts': "export const g = 'glob';\nexport const s = 'strftime';\n",
|
||||
'src/db/migrations/Migration20200101000000_baseline_schema.ts':
|
||||
"export const t = 'CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT, at TEXT DEFAULT (datetime(\\'now\\')))';\n",
|
||||
'src/db/engine/sqlite/sqlite-source.ts': "export const q = 'select distinct typeof(a) from t';\n",
|
||||
});
|
||||
expect(run(dir).status).toBe(0);
|
||||
});
|
||||
|
||||
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";
|
||||
// It runs on SQLite alone, so a LIKE and a best-effort step are SQLite's business there.
|
||||
const ddl = [
|
||||
"export const t = 'CREATE TABLE t (id INTEGER PRIMARY KEY AUTOINCREMENT)';",
|
||||
"export const p = 'PRAGMA foreign_keys = OFF';",
|
||||
"export const l = 'UPDATE t SET a = 1 WHERE b LIKE ?';",
|
||||
'declare function execBestEffort(m: unknown, s: string): void;',
|
||||
"execBestEffort(null, 'UPDATE t SET a = 1');",
|
||||
'',
|
||||
].join('\n');
|
||||
expect(run(serverRoot({ [migration]: ddl })).status).toBe(0);
|
||||
|
||||
const dml = "export const s = 'INSERT OR IGNORE INTO t (a) VALUES (1)';\n";
|
||||
@@ -183,7 +220,7 @@ describe('dialect-lint.mjs', () => {
|
||||
const POST_CUT = 'src/db/migrations/Migration20200101046450_probe.ts';
|
||||
const migration = (body: string) =>
|
||||
[
|
||||
"import { addColumnIfMissing, engineDdl, tableSql } from '../migration-utils';",
|
||||
"import { addColumnIfMissing, bestEffort, engineDdl, execBestEffort, execUnlessTableMissing, tableSql } from '../migration-utils';",
|
||||
'export class Probe {',
|
||||
' async up(this: never): Promise<void> {',
|
||||
body,
|
||||
@@ -243,18 +280,106 @@ describe('dialect-lint.mjs', () => {
|
||||
|
||||
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: [",
|
||||
" engineDdl(this, { sqlite: ['CREATE TABLE t (a INTEGER PRIMARY KEY AUTOINCREMENT, b TEXT)'], 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);
|
||||
it("DIALECT-015: moving the manifest's cut over a migration does not take it off the post-cut rules", () => {
|
||||
const body = " this.addSql('ALTER TABLE trips ADD COLUMN archived_at DATETIME');";
|
||||
// npm run gen:pg-baseline after the migration landed: the manifest now stands for it.
|
||||
const moved = JSON.stringify({ through: 'Migration20200101046450_probe', migrations: [] });
|
||||
const { status, out } = run(serverRoot({ [POST_CUT]: migration(body), [MANIFEST]: moved }));
|
||||
expect(status).toBe(1);
|
||||
expect(out).toContain(`FAIL ${POST_CUT}: 1 schema statement(s) outside engineDdl, 0 allowed.`);
|
||||
expect(run(serverRoot({ [POST_CUT]: migration(body) })).status).toBe(1);
|
||||
// A cut moved back holds more migrations to the post-cut rules, never fewer.
|
||||
const earlier = JSON.stringify({ through: 'Migration20200101043000_probe', migrations: [] });
|
||||
const between = 'src/db/migrations/Migration20200101044000_probe.ts';
|
||||
expect(run(serverRoot({ [between]: migration(body), [MANIFEST]: earlier })).status).toBe(1);
|
||||
});
|
||||
|
||||
it('DIALECT-018: a table rebuild in a sqlite member passes, its counter bookkeeping included', () => {
|
||||
const body = [
|
||||
' engineDdl(this, {',
|
||||
' sqlite: [',
|
||||
" 'CREATE TABLE t_new (id INTEGER PRIMARY KEY AUTOINCREMENT, a REAL)',",
|
||||
" 'INSERT INTO t_new (id, a) SELECT id, a FROM t',",
|
||||
" \"UPDATE sqlite_sequence SET seq = (SELECT seq FROM sqlite_sequence WHERE name = 't') WHERE name = 't_new'\",",
|
||||
" \"INSERT INTO sqlite_sequence (name, seq) SELECT 't_new', seq FROM sqlite_sequence WHERE name = 't'\",",
|
||||
" 'DROP TABLE t',",
|
||||
" 'ALTER TABLE t_new RENAME TO t',",
|
||||
" 'CREATE INDEX idx_t_a ON t(a)',",
|
||||
' ],',
|
||||
' postgres: [\'alter table "t" alter column "a" type double precision\'],',
|
||||
' });',
|
||||
].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 trigger',
|
||||
"'CREATE TRIGGER trg AFTER UPDATE ON places BEGIN UPDATE trips SET updated_at = 1 WHERE id = NEW.trip_id; END'",
|
||||
],
|
||||
['a data backfill', '"UPDATE reservations SET day = strftime(\'%Y-%m-%d\', reservation_time)"'],
|
||||
['a delete', "'DELETE FROM t WHERE a IS NULL'"],
|
||||
['an insert of values', '"INSERT INTO t (a) VALUES (\'x\')"'],
|
||||
['a copy into a table the member does not create', "'INSERT INTO other SELECT * FROM t'"],
|
||||
['a schema change with a data change behind it', "'CREATE INDEX i ON t(a); UPDATE t SET a = 1'"],
|
||||
['a statement built elsewhere', 'STATEMENT'],
|
||||
])('DIALECT-019: fails %s in a sqlite member and says where it belongs', (_what, statement) => {
|
||||
const body = [
|
||||
" const STATEMENT = 'UPDATE t SET a = 1';",
|
||||
` engineDdl(this, { sqlite: [${statement}], postgres: ['create index "i" on "t" ("a")'] });`,
|
||||
].join('\n');
|
||||
const { status, out } = run(serverRoot({ [POST_CUT]: migration(body) }));
|
||||
expect(status).toBe(1);
|
||||
expect(out).toContain(
|
||||
`FAIL ${POST_CUT}: 1 statement(s) in an engineDdl sqlite member that are not a schema change, 0 allowed.`,
|
||||
);
|
||||
expect(out).toContain('into this.addSql after the engineDdl call');
|
||||
});
|
||||
|
||||
it('DIALECT-020: SQLite statements with an empty postgres member need a sqlite-only reason', () => {
|
||||
const empty = " engineDdl(this, { sqlite: ['CREATE INDEX i ON t(a)'], postgres: [] });";
|
||||
const { status, out } = run(serverRoot({ [POST_CUT]: migration(empty) }));
|
||||
expect(status).toBe(1);
|
||||
expect(out).toContain(
|
||||
`FAIL ${POST_CUT}: 1 engineDdl call(s) with SQLite statements and an empty postgres member, 0 allowed.`,
|
||||
);
|
||||
expect(out).toContain('// sqlite-only: <reason>');
|
||||
const explained = [
|
||||
" engineDdl(this, { sqlite: ['CREATE INDEX i ON t(a)'],",
|
||||
' // sqlite-only: the index exists on Postgres since the baseline.',
|
||||
' postgres: [] });',
|
||||
].join('\n');
|
||||
expect(run(serverRoot({ [POST_CUT]: migration(explained) })).status).toBe(0);
|
||||
});
|
||||
|
||||
it.each([
|
||||
[
|
||||
'execBestEffort',
|
||||
"await execBestEffort(this, 'UPDATE budget_items SET amount_cents = CAST(amount AS INTEGER) * 100');",
|
||||
],
|
||||
['bestEffort', "await bestEffort(async () => { this.addSql('UPDATE t SET a = 1'); });"],
|
||||
['execUnlessTableMissing', "await execUnlessTableMissing(this, 'UPDATE t SET a = 1');"],
|
||||
])('DIALECT-021: fails %s, which aborts every pending migration on Postgres', (_what, body) => {
|
||||
const { status, out } = run(serverRoot({ [POST_CUT]: migration(` ${body}`) }));
|
||||
expect(status).toBe(1);
|
||||
expect(out).toContain(`FAIL ${POST_CUT}: 1 best-effort step(s)`);
|
||||
expect(out).toContain('guard it in its WHERE clause');
|
||||
});
|
||||
|
||||
it('DIALECT-022: a LIKE in a post-cut migration counts, as everywhere that runs on Postgres', () => {
|
||||
const body = " this.addSql('UPDATE t SET a = 1 WHERE b LIKE ?');";
|
||||
const { status, out } = run(serverRoot({ [POST_CUT]: migration(body) }));
|
||||
expect(status).toBe(1);
|
||||
expect(out).toContain(`FAIL ${POST_CUT}: 1 SQLite-only like hit(s), 0 allowed.`);
|
||||
});
|
||||
|
||||
it('DIALECT-016: fails closed on a missing or malformed manifest, and on a baselined allowance', () => {
|
||||
@@ -268,7 +393,7 @@ describe('dialect-lint.mjs', () => {
|
||||
expect(broken.status).toBe(1);
|
||||
expect(broken.out).toContain('must name the last migration it stands for');
|
||||
|
||||
for (const rule of ['ddl', 'sqlite-ddl']) {
|
||||
for (const rule of ['ddl', 'sqlite-ddl', 'sqlite-member', 'empty-postgres', 'best-effort']) {
|
||||
const allowed = run(serverRoot(files, { [POST_CUT]: { [rule]: 1 } }));
|
||||
expect(allowed.status).toBe(1);
|
||||
expect(allowed.out).toContain(`holds an allowance for ${rule}`);
|
||||
|
||||
Reference in New Issue
Block a user