Files
TREK/server/tests/unit/plugins/sql-script.test.ts
T
Maurice c970245e0d fix(plugins): hold plugin data on Postgres to its own schema and to one contract on both engines
The plugin SQL guard now refuses what still reached outside a plugin's
schema or depended on the server:
- reads of the system columns (xmin, xmax, cmin, cmax, ctid, tableoid),
  which show transaction ids and storage layout and do not exist on
  SQLite; a value of the plugin's own may still be named xmin with AS
- the xid and xid8 types wherever the word stands for the type, since
  age() of an xid is the cluster's transaction counter
- the text search functions that coerce a text argument to regconfig, a
  catalog lookup by name, which leave the allowlist
- collations and access methods outside the set every server has, which
  could tell whether a locale or an extension exists

Every plugin transaction pins, with SET LOCAL, the settings that decide
how values print and parse (TimeZone UTC, DateStyle, the C locales, float,
bytea and xml output, the text search configuration), so a plugin's output
neither depends on nor reveals the server's configuration.

Both stores now keep one contract. Its texts and caps live once, in
plugin-data-contract.ts, and both stores throw only those: NUL in SQL,
an argument or a migration id is refused on SQLite as on Postgres,
transaction control is refused in every call on both engines (tx() is
how a batch becomes atomic), a closed store refuses every call, and the
Postgres store answers the cases better-sqlite3 names with its words.
The SQLite store moves beside the Postgres one as SqlitePluginDataStore
in db/engine/sqlite, takes its file from the backend so src/db never
imports src/nest, and no longer needs the driver exemption. A contract
test runs every violation on both stores and fails when they answer
differently. The Postgres lane holds the allowlist, the pinned settings
and the SQL plugins commonly write against a real database.
2026-11-18 13:20:07 +01:00

45 lines
2.2 KiB
TypeScript

/**
* splitAfterSemicolons: where a plugin's exec() script is cut into statements, so
* the time budget can be read between them (src/db/engine/sqlite/plugin-data-store.ts).
*/
import { splitAfterSemicolons } from '../../../src/db/engine/sqlite/sql-script';
import { describe, expect, it } from 'vitest';
describe('splitAfterSemicolons', () => {
it('SQLSCRIPT-001 cuts after each statement and keeps every character', () => {
const sql = 'CREATE TABLE a (x); INSERT INTO a VALUES (1);\nSELECT 1';
const pieces = splitAfterSemicolons(sql);
expect(pieces).toEqual(['CREATE TABLE a (x);', ' INSERT INTO a VALUES (1);', '\nSELECT 1']);
expect(pieces.join('')).toBe(sql);
});
it('SQLSCRIPT-002 leaves the semicolons in strings, quoted identifiers and comments alone', () => {
expect(splitAfterSemicolons("INSERT INTO t VALUES ('a;b', 'it''s;');")).toEqual([
"INSERT INTO t VALUES ('a;b', 'it''s;');",
]);
expect(splitAfterSemicolons('SELECT "x;y", `p;q`, [r;s] FROM t;')).toEqual(['SELECT "x;y", `p;q`, [r;s] FROM t;']);
expect(splitAfterSemicolons('SELECT 1; -- one; two\nSELECT 2;')).toEqual(['SELECT 1;', ' -- one; two\nSELECT 2;']);
expect(splitAfterSemicolons('SELECT 1 /* a; b */;')).toEqual(['SELECT 1 /* a; b */;']);
});
it('SQLSCRIPT-003 cuts inside a trigger body, which the caller joins back up', () => {
expect(splitAfterSemicolons('CREATE TRIGGER t AFTER INSERT ON a BEGIN SELECT 1; END;')).toEqual([
'CREATE TRIGGER t AFTER INSERT ON a BEGIN SELECT 1;',
' END;',
]);
});
it('SQLSCRIPT-004 runs an unterminated string, identifier or comment to the end', () => {
expect(splitAfterSemicolons("SELECT 'open; still")).toEqual(["SELECT 'open; still"]);
expect(splitAfterSemicolons('SELECT [open; still')).toEqual(['SELECT [open; still']);
expect(splitAfterSemicolons('SELECT 1 -- trailing; comment')).toEqual(['SELECT 1 -- trailing; comment']);
expect(splitAfterSemicolons('SELECT 1 /* open; comment')).toEqual(['SELECT 1 /* open; comment']);
});
it('SQLSCRIPT-005 gives nothing for an empty script and a piece per stray semicolon', () => {
expect(splitAfterSemicolons('')).toEqual([]);
expect(splitAfterSemicolons(';;')).toEqual([';', ';']);
});
});