@mistriwork/engine 1.0.0-rc.10 → 1.0.0-rc.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/adapters/adapter.d.ts +33 -0
- package/dist/adapters/postgres.d.ts +21 -16
- package/dist/adapters/postgres.js +174 -223
- package/dist/adapters/postgresCatalog.d.ts +12 -1
- package/dist/adapters/postgresCatalog.js +57 -23
- package/dist/adapters/postgresCatalogDigest.js +40 -34
- package/dist/adapters/postgresCatalogRows.d.ts +2 -0
- package/dist/adapters/postgresWriteAhead.d.ts +47 -0
- package/dist/adapters/postgresWriteAhead.js +209 -0
- package/dist/adapters/scoped/keyRows.js +9 -3
- package/dist/adapters/scoped/keyTypes.d.ts +34 -0
- package/dist/adapters/scoped/keyTypes.js +66 -0
- package/dist/adapters/scoped/postgresScopeCatalog.d.ts +18 -0
- package/dist/adapters/scoped/postgresScopeCatalog.js +353 -13
- package/dist/adapters/scoped/postgresTriggers.d.ts +137 -0
- package/dist/adapters/scoped/postgresTriggers.js +1545 -0
- package/dist/adapters/scoped/scopeTriggers.d.ts +122 -0
- package/dist/adapters/scoped/scopeTriggers.js +131 -0
- package/dist/adapters/scoped/scopedReads.d.ts +3 -1
- package/dist/adapters/scoped/scopedReads.js +23 -5
- package/dist/adapters/scoped/scopedTransaction.d.ts +76 -6
- package/dist/adapters/scoped/scopedTransaction.js +278 -34
- package/dist/adapters/scoped/sqliteScopeCatalog.d.ts +17 -1
- package/dist/adapters/scoped/sqliteScopeCatalog.js +197 -18
- package/dist/adapters/scoped/sqliteTriggerSession.d.ts +64 -0
- package/dist/adapters/scoped/sqliteTriggerSession.js +245 -0
- package/dist/adapters/scoped/sqliteTriggers.d.ts +20 -0
- package/dist/adapters/scoped/sqliteTriggers.js +822 -0
- package/dist/adapters/scoped/storedText.d.ts +45 -0
- package/dist/adapters/scoped/storedText.js +61 -0
- package/dist/adapters/scoped/triggerEffects.d.ts +98 -0
- package/dist/adapters/scoped/triggerEffects.js +189 -0
- package/dist/adapters/scoped/triggerProof.d.ts +124 -0
- package/dist/adapters/scoped/triggerProof.js +366 -0
- package/dist/adapters/snapshotRead.d.ts +8 -0
- package/dist/adapters/snapshotRead.js +37 -0
- package/dist/adapters/sqlite.d.ts +13 -2
- package/dist/adapters/sqlite.js +139 -16
- package/dist/adapters/sqliteDecimals.d.ts +2 -2
- package/dist/adapters/sqliteDecimals.js +2 -8
- package/dist/adapters/storeHost.d.ts +4 -1
- package/dist/adapters/writeCapture.d.ts +7 -0
- package/dist/server/cli/args.d.ts +6 -0
- package/dist/server/cli/args.js +6 -0
- package/dist/server/columnGrants.d.ts +3 -0
- package/dist/server/columnGrants.js +12 -1
- package/dist/server/config/profile.d.ts +123 -0
- package/dist/server/config/profile.js +236 -0
- package/dist/server/connections.d.ts +5 -0
- package/dist/server/connections.js +11 -1
- package/dist/server/hiddenColumns.js +6 -0
- package/dist/server/http/refusalReport.js +16 -4
- package/dist/server/migrate/cli.d.ts +50 -3
- package/dist/server/migrate/cli.js +199 -14
- package/dist/server/migrate/runner.js +5 -1
- package/dist/server/shapeonly.js +5 -0
- package/dist/shared/aggregate.d.ts +49 -0
- package/dist/shared/aggregate.js +107 -0
- package/dist/shared/breakdown.js +17 -4
- package/dist/shared/decimals.d.ts +37 -0
- package/dist/shared/decimals.js +148 -0
- package/dist/shared/displayDefaults.js +15 -1
- package/dist/shared/filter.d.ts +22 -1
- package/dist/shared/filter.js +36 -9
- package/dist/shared/money.d.ts +9 -0
- package/dist/shared/money.js +17 -0
- package/dist/shared/pageModel.d.ts +1 -1
- package/dist/shared/refusal.d.ts +16 -0
- package/dist/shared/refusal.js +40 -0
- package/dist/shared/schema.d.ts +6 -0
- package/dist/shared/scopeCatalog.js +4 -0
- package/dist/shared/servedApp.d.ts +7 -2
- package/package.json +5 -1
|
@@ -32,7 +32,6 @@ import { type SqlStyle } from '../shared/filter.js';
|
|
|
32
32
|
* from 12, where `AS NOT MATERIALIZED` exists; before it, every CTE is
|
|
33
33
|
* materialized anyway and the words would be a syntax error. */
|
|
34
34
|
export declare function postgresScopeStyle(db: PgQuery): Promise<SqlStyle>;
|
|
35
|
-
/** Schemas that are the server's own business, not the user's data. */
|
|
36
35
|
export declare const SYSTEM_SCHEMAS: string[];
|
|
37
36
|
/**
|
|
38
37
|
* A relation that belongs to an extension is the extension's, not the App's:
|
|
@@ -64,7 +63,10 @@ export interface Located {
|
|
|
64
63
|
/** One column, as the write path needs it: name, type, null-ness, key-ness. */
|
|
65
64
|
export interface PgColumnRow {
|
|
66
65
|
name: string;
|
|
66
|
+
/** Its declared type, as format_type says it: a domain by its own name. What a protected edit compares by (updateGuard.ts). */
|
|
67
67
|
type: string;
|
|
68
|
+
/** The type a value is coerced for (writtenType): a domain's base type. Used for input coercion only. */
|
|
69
|
+
written: string;
|
|
68
70
|
notnull: boolean;
|
|
69
71
|
is_pk: boolean;
|
|
70
72
|
}
|
|
@@ -91,6 +93,15 @@ export interface PgColumnRow {
|
|
|
91
93
|
* the connection opened is a column the editor should be able to write, and
|
|
92
94
|
* one dropped since is one it must not.
|
|
93
95
|
*/
|
|
96
|
+
/**
|
|
97
|
+
* A column's type as a write coerces a value for it (shared/schema.ts coerceForColumn), from the column's type and
|
|
98
|
+
* modifier expressions: with its domains unwrapped through pg_type.typbasetype, the modifier the innermost one that
|
|
99
|
+
* has one gives, said by format_type. A domain over integer is then an integer column, so "" is NULL and "7.0" is 7,
|
|
100
|
+
* as they are for a column declared integer; by the domain's own name the write sent them as text, which the domain's
|
|
101
|
+
* input refused. The scope's parent check coerces a reference by the same text (postgresScopeCatalog.ts). Every
|
|
102
|
+
* function and operator in pg_catalog, as the scope's reads name them.
|
|
103
|
+
*/
|
|
104
|
+
export declare const writtenType: (type: string, typmod: string) => string;
|
|
94
105
|
export declare function postgresColumnsOf(schema: string, name: string, db: PgQuery | undefined, epoch: string | undefined, cache: EpochCache<unknown>, connect: () => Promise<PgQuery>): Promise<PgColumnRow[]>;
|
|
95
106
|
export declare function assertPostgresTable(id: string, db: PgQuery | undefined, epoch: string | undefined, cache: EpochCache<unknown>, connect: () => Promise<PgQuery>): Promise<Located>;
|
|
96
107
|
export {};
|
|
@@ -39,6 +39,9 @@ export async function postgresScopeStyle(db) {
|
|
|
39
39
|
return Number(result.rows[0]?.version) >= 120_000 ? POSTGRES_INLINE_STYLE : POSTGRES_STYLE;
|
|
40
40
|
}
|
|
41
41
|
/** Schemas that are the server's own business, not the user's data. */
|
|
42
|
+
/** pg_catalog's own equality, which no operator of the same name elsewhere on the search_path can stand in for. */
|
|
43
|
+
const EQ = 'OPERATOR(pg_catalog.=)';
|
|
44
|
+
const NE = 'OPERATOR(pg_catalog.<>)';
|
|
42
45
|
export const SYSTEM_SCHEMAS = ['pg_catalog', 'information_schema', 'pg_toast'];
|
|
43
46
|
/**
|
|
44
47
|
* A relation that belongs to an extension is the extension's, not the App's:
|
|
@@ -51,8 +54,8 @@ export const SYSTEM_SCHEMAS = ['pg_catalog', 'information_schema', 'pg_toast'];
|
|
|
51
54
|
*/
|
|
52
55
|
export function notExtensionMember(alias) {
|
|
53
56
|
return `NOT EXISTS (SELECT 1 FROM pg_catalog.pg_depend ext
|
|
54
|
-
WHERE ext.classid
|
|
55
|
-
AND ext.refclassid
|
|
57
|
+
WHERE ext.classid ${EQ} 'pg_catalog.pg_class'::regclass AND ext.objid ${EQ} ${alias}.oid
|
|
58
|
+
AND ext.refclassid ${EQ} 'pg_catalog.pg_extension'::regclass AND ext.deptype ${EQ} 'e')`;
|
|
56
59
|
}
|
|
57
60
|
export async function describePostgresCatalog(db, source) {
|
|
58
61
|
const filterSchemas = source.schemas.length ? `AND n.nspname = ANY($1)` : `AND n.nspname <> ALL($1)`;
|
|
@@ -89,7 +92,7 @@ export async function describePostgresCatalog(db, source) {
|
|
|
89
92
|
than `information_schema`, which is why it needs the join. */
|
|
90
93
|
`SELECT c.table_schema, c.table_name, c.column_name, c.data_type,
|
|
91
94
|
c.is_nullable, c.column_default, c.ordinal_position, c.is_identity, c.identity_generation, c.is_generated,
|
|
92
|
-
c.character_maximum_length,
|
|
95
|
+
c.character_maximum_length, c.numeric_scale,
|
|
93
96
|
col_description(pc.oid, c.ordinal_position::int) AS comment,
|
|
94
97
|
/* Cast to text before aggregating: enumlabel has type "name",
|
|
95
98
|
and the driver hands a name[] back as the literal string
|
|
@@ -135,34 +138,47 @@ export async function describePostgresCatalog(db, source) {
|
|
|
135
138
|
`name[]` — an array OID the driver has no parser for, which arrives
|
|
136
139
|
as the raw string "{a,b}" rather than an array. The ::text casts are
|
|
137
140
|
what make these come back as JavaScript arrays. */
|
|
138
|
-
const keys = await db.query(
|
|
141
|
+
const keys = await db.query(
|
|
142
|
+
/* Its functions and operators named in pg_catalog, as the scope's own catalog reads are (scoped/postgresTriggers.ts
|
|
143
|
+
CAT): a `public.unnest(int2[])` or `public.array_agg(text)` would otherwise answer here before the built-in, and
|
|
144
|
+
a foreign key read with no columns is one a scoped write's parent check never makes. */
|
|
145
|
+
`SELECT con.conname AS constraint_name,
|
|
139
146
|
fn.nspname AS from_schema,
|
|
140
147
|
f.relname AS from_table,
|
|
141
|
-
(SELECT array_agg(a.attname::text ORDER BY k.ord)
|
|
142
|
-
FROM unnest(con.conkey) WITH ORDINALITY AS k(attnum, ord)
|
|
143
|
-
JOIN pg_attribute a ON a.attrelid
|
|
148
|
+
(SELECT pg_catalog.array_agg(a.attname::pg_catalog.text ORDER BY k.ord)
|
|
149
|
+
FROM pg_catalog.unnest(con.conkey) WITH ORDINALITY AS k(attnum, ord)
|
|
150
|
+
JOIN pg_catalog.pg_attribute a ON a.attrelid ${EQ} f.oid AND a.attnum ${EQ} k.attnum) AS from_columns,
|
|
144
151
|
tn.nspname AS to_schema,
|
|
145
152
|
t.relname AS to_table,
|
|
146
|
-
(SELECT array_agg(a.attname::text ORDER BY k.ord)
|
|
147
|
-
FROM unnest(con.confkey) WITH ORDINALITY AS k(attnum, ord)
|
|
148
|
-
JOIN pg_attribute a ON a.attrelid
|
|
153
|
+
(SELECT pg_catalog.array_agg(a.attname::pg_catalog.text ORDER BY k.ord)
|
|
154
|
+
FROM pg_catalog.unnest(con.confkey) WITH ORDINALITY AS k(attnum, ord)
|
|
155
|
+
JOIN pg_catalog.pg_attribute a ON a.attrelid ${EQ} t.oid AND a.attnum ${EQ} k.attnum) AS to_columns,
|
|
149
156
|
con.confdeltype AS delete_rule
|
|
150
|
-
FROM pg_constraint con
|
|
151
|
-
JOIN pg_class f ON f.oid
|
|
152
|
-
JOIN pg_namespace fn ON fn.oid
|
|
153
|
-
JOIN pg_class t ON t.oid
|
|
154
|
-
JOIN pg_namespace tn ON tn.oid
|
|
155
|
-
|
|
157
|
+
FROM pg_catalog.pg_constraint con
|
|
158
|
+
JOIN pg_catalog.pg_class f ON f.oid ${EQ} con.conrelid
|
|
159
|
+
JOIN pg_catalog.pg_namespace fn ON fn.oid ${EQ} f.relnamespace
|
|
160
|
+
JOIN pg_catalog.pg_class t ON t.oid ${EQ} con.confrelid
|
|
161
|
+
JOIN pg_catalog.pg_namespace tn ON tn.oid ${EQ} t.relnamespace
|
|
162
|
+
-- Never a clone that points at a partition: a foreign key to a partitioned table is copied once per partition of
|
|
163
|
+
-- it (conparentid names the original), each naming a partition the App and its policy never declare, which a
|
|
164
|
+
-- scoped parent check then counted through and refused. The original is the reference: an ordinary table's
|
|
165
|
+
-- write is checked through the partitioned table it names. A partition of a referencing table keeps its copy,
|
|
166
|
+
-- which names the same parent, so the partition shows its reference and a delete of the parent row sees it. A
|
|
167
|
+
-- key declared on a partition alone, or naming one, is read as declared; no scoped write targets a table in a
|
|
168
|
+
-- partition or inheritance hierarchy (postgresScopeCatalog.ts), so none is written through a table above it.
|
|
169
|
+
WHERE con.contype ${EQ} 'f'
|
|
170
|
+
AND NOT EXISTS (SELECT FROM pg_catalog.pg_constraint pc
|
|
171
|
+
WHERE pc.oid ${EQ} con.conparentid AND pc.confrelid ${NE} con.confrelid)
|
|
156
172
|
ORDER BY fn.nspname, f.relname, con.conname`);
|
|
157
173
|
const pks = await db.query(`SELECT n.nspname AS table_schema,
|
|
158
174
|
c.relname AS table_name,
|
|
159
|
-
(SELECT array_agg(a.attname::text ORDER BY k.ord)
|
|
160
|
-
FROM unnest(con.conkey) WITH ORDINALITY AS k(attnum, ord)
|
|
161
|
-
JOIN pg_attribute a ON a.attrelid
|
|
162
|
-
FROM pg_constraint con
|
|
163
|
-
JOIN pg_class c ON c.oid
|
|
164
|
-
JOIN pg_namespace n ON n.oid
|
|
165
|
-
WHERE con.contype
|
|
175
|
+
(SELECT pg_catalog.array_agg(a.attname::pg_catalog.text ORDER BY k.ord)
|
|
176
|
+
FROM pg_catalog.unnest(con.conkey) WITH ORDINALITY AS k(attnum, ord)
|
|
177
|
+
JOIN pg_catalog.pg_attribute a ON a.attrelid ${EQ} c.oid AND a.attnum ${EQ} k.attnum) AS columns
|
|
178
|
+
FROM pg_catalog.pg_constraint con
|
|
179
|
+
JOIN pg_catalog.pg_class c ON c.oid ${EQ} con.conrelid
|
|
180
|
+
JOIN pg_catalog.pg_namespace n ON n.oid ${EQ} c.relnamespace
|
|
181
|
+
WHERE con.contype ${EQ} 'p'
|
|
166
182
|
ORDER BY n.nspname, c.relname, con.conname`);
|
|
167
183
|
const pkByTable = new Map();
|
|
168
184
|
for (const row of pks.rows) {
|
|
@@ -274,6 +290,8 @@ export async function describePostgresCatalog(db, source) {
|
|
|
274
290
|
primaryKey: pk.has(c.column_name),
|
|
275
291
|
references: refByColumn.get(`${id}.${c.column_name}`),
|
|
276
292
|
default: c.column_default ?? undefined,
|
|
293
|
+
/* The type string stays what the database calls it; the places a sum must say travel beside it. */
|
|
294
|
+
...(c.data_type === 'numeric' && c.numeric_scale !== null && c.numeric_scale !== undefined ? { scale: Number(c.numeric_scale) } : {}),
|
|
277
295
|
identity: c.is_identity === 'YES' ? (c.identity_generation === 'ALWAYS' ? 'always' : 'by-default') : undefined,
|
|
278
296
|
comment: c.comment ?? undefined,
|
|
279
297
|
// The database computes a generated column; nothing may write it (as sqlite.ts says of its own).
|
|
@@ -343,6 +361,21 @@ export async function describePostgresCatalog(db, source) {
|
|
|
343
361
|
* the connection opened is a column the editor should be able to write, and
|
|
344
362
|
* one dropped since is one it must not.
|
|
345
363
|
*/
|
|
364
|
+
/**
|
|
365
|
+
* A column's type as a write coerces a value for it (shared/schema.ts coerceForColumn), from the column's type and
|
|
366
|
+
* modifier expressions: with its domains unwrapped through pg_type.typbasetype, the modifier the innermost one that
|
|
367
|
+
* has one gives, said by format_type. A domain over integer is then an integer column, so "" is NULL and "7.0" is 7,
|
|
368
|
+
* as they are for a column declared integer; by the domain's own name the write sent them as text, which the domain's
|
|
369
|
+
* input refused. The scope's parent check coerces a reference by the same text (postgresScopeCatalog.ts). Every
|
|
370
|
+
* function and operator in pg_catalog, as the scope's reads name them.
|
|
371
|
+
*/
|
|
372
|
+
export const writtenType = (type, typmod) => `(WITH RECURSIVE d(type, typmod, depth) AS (
|
|
373
|
+
SELECT ${type}, ${typmod}, 0
|
|
374
|
+
UNION ALL
|
|
375
|
+
SELECT t.typbasetype, CASE WHEN t.typtypmod OPERATOR(pg_catalog.<>) -1 THEN t.typtypmod ELSE d.typmod END, d.depth OPERATOR(pg_catalog.+) 1
|
|
376
|
+
FROM d JOIN pg_catalog.pg_type t ON t.oid OPERATOR(pg_catalog.=) d.type
|
|
377
|
+
WHERE t.typtype OPERATOR(pg_catalog.=) 'd' AND d.depth OPERATOR(pg_catalog.<) 32)
|
|
378
|
+
SELECT pg_catalog.format_type(d.type, d.typmod) FROM d ORDER BY d.depth DESC LIMIT 1)`;
|
|
346
379
|
export async function postgresColumnsOf(schema, name, db, epoch, cache, connect) {
|
|
347
380
|
const kept = cache.get(epoch, `columns:${schema}.${name}`);
|
|
348
381
|
if (kept)
|
|
@@ -350,6 +383,7 @@ export async function postgresColumnsOf(schema, name, db, epoch, cache, connect)
|
|
|
350
383
|
db ??= await connect();
|
|
351
384
|
const result = await db.query(`SELECT a.attname AS name,
|
|
352
385
|
format_type(a.atttypid, a.atttypmod) AS type,
|
|
386
|
+
${writtenType('a.atttypid', 'a.atttypmod')} AS written,
|
|
353
387
|
a.attnotnull AS notnull,
|
|
354
388
|
COALESCE(k.is_pk, false) AS is_pk
|
|
355
389
|
FROM pg_attribute a
|
|
@@ -8,6 +8,12 @@
|
|
|
8
8
|
*/
|
|
9
9
|
import { Refusal } from './adapter.js';
|
|
10
10
|
import { notExtensionMember } from './postgresCatalog.js';
|
|
11
|
+
/* pg_catalog's operators by name, as the scope's own catalog reads use them (scoped/postgresTriggers.ts CAT): the
|
|
12
|
+
text concatenation the digest is built from is polymorphic, and a `public.||(text, oid)` would otherwise answer one
|
|
13
|
+
digest whatever the catalog said, which reads as nothing having moved. */
|
|
14
|
+
const EQ = 'OPERATOR(pg_catalog.=)';
|
|
15
|
+
const NE = 'OPERATOR(pg_catalog.<>)';
|
|
16
|
+
const CAT = 'OPERATOR(pg_catalog.||)';
|
|
11
17
|
/**
|
|
12
18
|
* One statement that changes whenever anything a full attestation reads
|
|
13
19
|
* changes: the `(oid, xmin)` of every catalog row the whole-catalog
|
|
@@ -39,44 +45,44 @@ export function postgresCatalogDigestStatement(excludedSchemas) {
|
|
|
39
45
|
throw new Refusal('The catalog digest names only plain identifiers.');
|
|
40
46
|
const excludedLiteral = `ARRAY[${excludedSchemas.map((name) => `'${name}'`).join(', ')}]`;
|
|
41
47
|
return `WITH ns AS (SELECT n.oid, n.xmin FROM pg_catalog.pg_namespace n
|
|
42
|
-
WHERE n.nspname
|
|
48
|
+
WHERE n.nspname ${NE} ALL(${excludedLiteral}::pg_catalog.text[]) AND n.nspname OPERATOR(pg_catalog.!~) '^pg_(toast_)?temp_'),
|
|
43
49
|
/* An extension's own relations are left out, as the description leaves them out (see notExtensionMember). */
|
|
44
|
-
rel AS (SELECT c.oid, c.xmin FROM pg_catalog.pg_class c JOIN ns ON ns.oid
|
|
50
|
+
rel AS (SELECT c.oid, c.xmin FROM pg_catalog.pg_class c JOIN ns ON ns.oid ${EQ} c.relnamespace WHERE ${notExtensionMember('c')}),
|
|
45
51
|
seen(v) AS (
|
|
46
|
-
SELECT 'n'
|
|
47
|
-
UNION ALL SELECT 'c'
|
|
48
|
-
UNION ALL SELECT 'a'
|
|
49
|
-
FROM pg_catalog.pg_attribute a JOIN rel ON rel.oid
|
|
50
|
-
UNION ALL SELECT 'd'
|
|
51
|
-
UNION ALL SELECT 'k'
|
|
52
|
-
WHERE k.connamespace
|
|
53
|
-
UNION ALL SELECT 'i'
|
|
54
|
-
UNION ALL SELECT 't'
|
|
55
|
-
UNION ALL SELECT 'r'
|
|
56
|
-
UNION ALL SELECT 'p'
|
|
57
|
-
UNION ALL SELECT 'y'
|
|
58
|
-
UNION ALL SELECT 'e'
|
|
59
|
-
JOIN pg_catalog.pg_type y ON y.oid
|
|
60
|
-
UNION ALL SELECT 'f'
|
|
61
|
-
UNION ALL SELECT 'o'
|
|
62
|
-
UNION ALL SELECT 'q'
|
|
52
|
+
SELECT 'n' ${CAT} ns.oid ${CAT} ':' ${CAT} ns.xmin FROM ns
|
|
53
|
+
UNION ALL SELECT 'c' ${CAT} rel.oid ${CAT} ':' ${CAT} rel.xmin FROM rel
|
|
54
|
+
UNION ALL SELECT 'a' ${CAT} a.attrelid ${CAT} '.' ${CAT} a.attnum ${CAT} ':' ${CAT} a.xmin
|
|
55
|
+
FROM pg_catalog.pg_attribute a JOIN rel ON rel.oid ${EQ} a.attrelid
|
|
56
|
+
UNION ALL SELECT 'd' ${CAT} d.oid ${CAT} ':' ${CAT} d.xmin FROM pg_catalog.pg_attrdef d JOIN rel ON rel.oid ${EQ} d.adrelid
|
|
57
|
+
UNION ALL SELECT 'k' ${CAT} k.oid ${CAT} ':' ${CAT} k.xmin FROM pg_catalog.pg_constraint k
|
|
58
|
+
WHERE k.connamespace ${EQ} ANY (SELECT oid FROM ns) OR k.conrelid ${EQ} ANY (SELECT oid FROM rel) OR k.confrelid ${EQ} ANY (SELECT oid FROM rel)
|
|
59
|
+
UNION ALL SELECT 'i' ${CAT} i.indexrelid ${CAT} ':' ${CAT} i.xmin FROM pg_catalog.pg_index i JOIN rel ON rel.oid ${EQ} i.indrelid
|
|
60
|
+
UNION ALL SELECT 't' ${CAT} g.oid ${CAT} ':' ${CAT} g.xmin FROM pg_catalog.pg_trigger g JOIN rel ON rel.oid ${EQ} g.tgrelid
|
|
61
|
+
UNION ALL SELECT 'r' ${CAT} w.oid ${CAT} ':' ${CAT} w.xmin FROM pg_catalog.pg_rewrite w JOIN rel ON rel.oid ${EQ} w.ev_class
|
|
62
|
+
UNION ALL SELECT 'p' ${CAT} p.oid ${CAT} ':' ${CAT} p.xmin FROM pg_catalog.pg_policy p JOIN rel ON rel.oid ${EQ} p.polrelid
|
|
63
|
+
UNION ALL SELECT 'y' ${CAT} y.oid ${CAT} ':' ${CAT} y.xmin FROM pg_catalog.pg_type y JOIN ns ON ns.oid ${EQ} y.typnamespace
|
|
64
|
+
UNION ALL SELECT 'e' ${CAT} e.oid ${CAT} ':' ${CAT} e.xmin FROM pg_catalog.pg_enum e
|
|
65
|
+
JOIN pg_catalog.pg_type y ON y.oid ${EQ} e.enumtypid JOIN ns ON ns.oid ${EQ} y.typnamespace
|
|
66
|
+
UNION ALL SELECT 'f' ${CAT} f.oid ${CAT} ':' ${CAT} f.xmin FROM pg_catalog.pg_proc f JOIN ns ON ns.oid ${EQ} f.pronamespace
|
|
67
|
+
UNION ALL SELECT 'o' ${CAT} o.oid ${CAT} ':' ${CAT} o.xmin FROM pg_catalog.pg_operator o JOIN ns ON ns.oid ${EQ} o.oprnamespace
|
|
68
|
+
UNION ALL SELECT 'q' ${CAT} q.oid ${CAT} ':' ${CAT} q.xmin FROM pg_catalog.pg_opclass q JOIN ns ON ns.oid ${EQ} q.opcnamespace
|
|
63
69
|
/* What else a store attestation counts in the runtime schema (postgresRuntimeSchemaProblems): one more
|
|
64
70
|
object there refuses the store, so it moves the digest too. */
|
|
65
|
-
UNION ALL SELECT 'v'
|
|
66
|
-
UNION ALL SELECT 'F'
|
|
67
|
-
UNION ALL SELECT 'Tc'
|
|
68
|
-
UNION ALL SELECT 'Td'
|
|
69
|
-
UNION ALL SELECT 'Tp'
|
|
70
|
-
UNION ALL SELECT 'Tt'
|
|
71
|
-
UNION ALL SELECT 'x'
|
|
72
|
-
UNION ALL SELECT 'X'
|
|
73
|
-
UNION ALL SELECT 'l'
|
|
74
|
-
WHERE l.collnamespace
|
|
75
|
-
OR l.oid
|
|
76
|
-
UNION ALL SELECT 'm'
|
|
77
|
-
UNION ALL SELECT 'u'
|
|
78
|
-
UNION ALL SELECT 's'
|
|
79
|
-
SELECT md5(string_agg(v, ',' ORDER BY v)) AS fingerprint FROM seen`;
|
|
71
|
+
UNION ALL SELECT 'v' ${CAT} v.oid ${CAT} ':' ${CAT} v.xmin FROM pg_catalog.pg_conversion v JOIN ns ON ns.oid ${EQ} v.connamespace
|
|
72
|
+
UNION ALL SELECT 'F' ${CAT} o.oid ${CAT} ':' ${CAT} o.xmin FROM pg_catalog.pg_opfamily o JOIN ns ON ns.oid ${EQ} o.opfnamespace
|
|
73
|
+
UNION ALL SELECT 'Tc' ${CAT} t.oid ${CAT} ':' ${CAT} t.xmin FROM pg_catalog.pg_ts_config t JOIN ns ON ns.oid ${EQ} t.cfgnamespace
|
|
74
|
+
UNION ALL SELECT 'Td' ${CAT} t.oid ${CAT} ':' ${CAT} t.xmin FROM pg_catalog.pg_ts_dict t JOIN ns ON ns.oid ${EQ} t.dictnamespace
|
|
75
|
+
UNION ALL SELECT 'Tp' ${CAT} t.oid ${CAT} ':' ${CAT} t.xmin FROM pg_catalog.pg_ts_parser t JOIN ns ON ns.oid ${EQ} t.prsnamespace
|
|
76
|
+
UNION ALL SELECT 'Tt' ${CAT} t.oid ${CAT} ':' ${CAT} t.xmin FROM pg_catalog.pg_ts_template t JOIN ns ON ns.oid ${EQ} t.tmplnamespace
|
|
77
|
+
UNION ALL SELECT 'x' ${CAT} x.oid ${CAT} ':' ${CAT} x.xmin FROM pg_catalog.pg_statistic_ext x JOIN ns ON ns.oid ${EQ} x.stxnamespace
|
|
78
|
+
UNION ALL SELECT 'X' ${CAT} x.oid ${CAT} ':' ${CAT} x.xmin FROM pg_catalog.pg_extension x JOIN ns ON ns.oid ${EQ} x.extnamespace
|
|
79
|
+
UNION ALL SELECT 'l' ${CAT} l.oid ${CAT} ':' ${CAT} l.xmin FROM pg_catalog.pg_collation l
|
|
80
|
+
WHERE l.collnamespace ${EQ} ANY (SELECT oid FROM ns)
|
|
81
|
+
OR l.oid ${EQ} ANY (SELECT a.attcollation FROM pg_catalog.pg_attribute a JOIN rel ON rel.oid ${EQ} a.attrelid)
|
|
82
|
+
UNION ALL SELECT 'm' ${CAT} m.roleid ${CAT} '.' ${CAT} m.member ${CAT} ':' ${CAT} m.xmin FROM pg_catalog.pg_auth_members m
|
|
83
|
+
UNION ALL SELECT 'u' ${CAT} r.oid ${CAT} ':' ${CAT} r.rolname ${CAT} ':' ${CAT} r.rolsuper ${CAT} ':' ${CAT} r.rolinherit FROM pg_catalog.pg_roles r
|
|
84
|
+
UNION ALL SELECT 's' ${CAT} current_user)
|
|
85
|
+
SELECT pg_catalog.md5(pg_catalog.string_agg(v, ',' ORDER BY v)) AS fingerprint FROM seen`;
|
|
80
86
|
}
|
|
81
87
|
/** The digest a digest statement answered, from its result. */
|
|
82
88
|
export function postgresCatalogDigestOf(result) {
|
|
@@ -15,6 +15,8 @@ export interface ColumnRow {
|
|
|
15
15
|
enum_labels: string[] | null;
|
|
16
16
|
/** A `varchar(n)`'s n; null for any other type. */
|
|
17
17
|
character_maximum_length: number | null;
|
|
18
|
+
/** A `numeric(p, s)`'s s; null for an unconstrained numeric and for every other type. */
|
|
19
|
+
numeric_scale?: number | null;
|
|
18
20
|
}
|
|
19
21
|
/** What the checks say of a text column's length and form (vocabulary.ts checkTextRule), the first of each. */
|
|
20
22
|
export declare function checkedText(checks: string[], column: string): {
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a Postgres transaction sends behind its BEGIN, and the small
|
|
3
|
+
* statement builders that send and the transaction share. The ahead statements
|
|
4
|
+
* must be exactly the ones the transaction sends in its turn, so they are built
|
|
5
|
+
* here by the same rules (`plainStatement`, `scopedStatement`,
|
|
6
|
+
* `rowLockStatement`) that `postgres.ts` calls when it sends them; this file
|
|
7
|
+
* holds no other state than the per-handle lock bookkeeping below.
|
|
8
|
+
*/
|
|
9
|
+
import { type Filter, type QueryRequest, type SqlFragment } from './adapter.js';
|
|
10
|
+
import { type AheadReport } from './postgresTransactionHandle.js';
|
|
11
|
+
import { KeyRows } from './scoped/keyRows.js';
|
|
12
|
+
import type { PostgresScopeCatalog } from './scoped/postgresScopeCatalog.js';
|
|
13
|
+
import type { PgQuery } from './postgresPool.js';
|
|
14
|
+
import type { WriteAhead } from './writeAhead.js';
|
|
15
|
+
import type { RowScope } from '../shared/rowScope.js';
|
|
16
|
+
import type { DatabaseSchema, TableMeta } from '../shared/schema.js';
|
|
17
|
+
import type { PostgresTransactionJournal } from './storeHost.js';
|
|
18
|
+
import type { PostgresTransactionVerifier } from './postgres.js';
|
|
19
|
+
export declare const PLAIN_SCOPE: RowScope;
|
|
20
|
+
export declare const heldLocks: WeakMap<PgQuery, Map<string, () => void>>;
|
|
21
|
+
export declare const heldRows: WeakMap<PgQuery, Set<string>>;
|
|
22
|
+
export declare const lockKey: (text: string, values: readonly unknown[]) => string;
|
|
23
|
+
export declare function rowLockStatement(table: TableMeta, key: Readonly<Record<string, string | number>>): [string, unknown[]];
|
|
24
|
+
export declare function plainTable(table: string): {
|
|
25
|
+
schema: string;
|
|
26
|
+
name: string;
|
|
27
|
+
};
|
|
28
|
+
export declare const catalogId: (table: string) => string;
|
|
29
|
+
export declare function plainStatement(request: QueryRequest): SqlFragment | undefined;
|
|
30
|
+
/** A row lock as a scoped transaction keeps it: its table and the filter that names the row. */
|
|
31
|
+
export declare const rowLockKey: (table: TableMeta, filter: Filter) => string;
|
|
32
|
+
export declare function scopedStatement(schema: DatabaseSchema, within: RowScope, request: QueryRequest): SqlFragment | undefined;
|
|
33
|
+
export declare function reportWriteAhead(report: AheadReport | undefined, committed: boolean): void;
|
|
34
|
+
/** The statements openPlain sends behind the BEGIN; each exactly what the transaction sends in its turn (lockRecordNow, queryOn). */
|
|
35
|
+
export declare function sendPlainAhead(owner: object, write: PgQuery, schema: DatabaseSchema, keyRows: KeyRows, journal: PostgresTransactionJournal<unknown> | undefined, hint: WriteAhead): Promise<unknown>[];
|
|
36
|
+
/**
|
|
37
|
+
* The statements a scoped transaction opens with, sent behind its BEGIN
|
|
38
|
+
* (openWrite's `sent`), from what it was told (writeAhead.ts) and the scope
|
|
39
|
+
* catalog read last time: the verifier's own (its fence), the journal's
|
|
40
|
+
* receipt lookup, and for each target its row lock, its row read in full
|
|
41
|
+
* through the scope (kept to answer its reads by key, keyRows.ts), and,
|
|
42
|
+
* where the write is captured for history, the capture's own lock and read
|
|
43
|
+
* (historyWrites). Each is exactly the statement the transaction sends in
|
|
44
|
+
* its turn, built by the same rules; one it never sends is left, counted,
|
|
45
|
+
* and changes nothing. None where the client does not pipeline.
|
|
46
|
+
*/
|
|
47
|
+
export declare function sendWriteAhead(owner: object, write: PgQuery, scope: RowScope, peeked: PostgresScopeCatalog | undefined, keyRows: KeyRows, journal: PostgresTransactionJournal<unknown> | undefined, verify: PostgresTransactionVerifier | undefined, hint: WriteAhead | undefined, locked: Set<string>): Promise<unknown>[];
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What a Postgres transaction sends behind its BEGIN, and the small
|
|
3
|
+
* statement builders that send and the transaction share. The ahead statements
|
|
4
|
+
* must be exactly the ones the transaction sends in its turn, so they are built
|
|
5
|
+
* here by the same rules (`plainStatement`, `scopedStatement`,
|
|
6
|
+
* `rowLockStatement`) that `postgres.ts` calls when it sends them; this file
|
|
7
|
+
* holds no other state than the per-handle lock bookkeeping below.
|
|
8
|
+
*/
|
|
9
|
+
import { POSTGRES_STYLE, buildWhere, clampLimit, clampOffset } from './adapter.js';
|
|
10
|
+
import { holdAhead, sendAhead, sendsAhead } from './postgresTransactionHandle.js';
|
|
11
|
+
import { createScopedReads } from './scoped/scopedReads.js';
|
|
12
|
+
import { KeyRows } from './scoped/keyRows.js';
|
|
13
|
+
import { isRuntimeRelation, qualified } from './postgresValues.js';
|
|
14
|
+
import { capturesWrites } from './writeCapture.js';
|
|
15
|
+
/* The row scope an unscoped transaction's kept rows are filed under (keyRows.ts): none, the same object every time. */
|
|
16
|
+
export const PLAIN_SCOPE = Object.freeze({});
|
|
17
|
+
/* The record locks a transaction took behind its BEGIN (openPlain), by statement, each with what marks it taken.
|
|
18
|
+
Only those: a lock taken later could be let go by a ROLLBACK TO SAVEPOINT, one taken at the opening cannot. */
|
|
19
|
+
export const heldLocks = new WeakMap();
|
|
20
|
+
/* Of those, the ones that found their row: it is the transaction's, unmoving, until it ends (openPlain's `widen`). */
|
|
21
|
+
export const heldRows = new WeakMap();
|
|
22
|
+
export const lockKey = (text, values) => JSON.stringify([text, values]);
|
|
23
|
+
/* lockRecordNow's statement for a row named by its whole primary key, and its values. */
|
|
24
|
+
export function rowLockStatement(table, key) {
|
|
25
|
+
const pk = table.columns.filter((column) => column.primaryKey).map((column) => column.name);
|
|
26
|
+
const where = buildWhere({ groups: [pk.map((column) => ({ column, op: '=', value: key[column] }))] }, POSTGRES_STYLE);
|
|
27
|
+
return [`SELECT ${pk.map(POSTGRES_STYLE.quote).join(', ')} FROM ${qualified(table)}${where.text} FOR UPDATE`, where.params];
|
|
28
|
+
}
|
|
29
|
+
/* A table's schema and name from the name a request gives it, as assertPostgresTable reads it. */
|
|
30
|
+
export function plainTable(table) {
|
|
31
|
+
const dot = table.indexOf('.');
|
|
32
|
+
return dot === -1 ? { schema: 'public', name: table } : { schema: table.slice(0, dot), name: table.slice(dot + 1) };
|
|
33
|
+
}
|
|
34
|
+
/* A table's catalog id from the name a request gives it: a bare name is in the public schema, as assertPostgresTable reads it. */
|
|
35
|
+
export const catalogId = (table) => (table.includes('.') ? table : `public.${table}`);
|
|
36
|
+
/* The statement queryOn sends for an unordered read, without sending it: the table as its id names it
|
|
37
|
+
(assertPostgresTable), the read bounded as every read is. */
|
|
38
|
+
export function plainStatement(request) {
|
|
39
|
+
if (request.orderBy?.length)
|
|
40
|
+
return undefined;
|
|
41
|
+
const at = plainTable(request.table);
|
|
42
|
+
const where = buildWhere(request.filter, POSTGRES_STYLE);
|
|
43
|
+
const cols = request.columns?.length ? request.columns.map((column) => POSTGRES_STYLE.quote(column)).join(', ') : '*';
|
|
44
|
+
const n = where.params.length;
|
|
45
|
+
return {
|
|
46
|
+
text: `SELECT ${cols} FROM ${qualified(at)}${where.text} LIMIT $${n + 1} OFFSET $${n + 2}`,
|
|
47
|
+
params: [...where.params, clampLimit(request.limit), clampOffset(request.offset)],
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
/* A statement a scoped read would send, without sending it: createScopedReads
|
|
51
|
+
compiles before it executes, so the executor is reached synchronously and
|
|
52
|
+
says what it was handed. */
|
|
53
|
+
/** A row lock as a scoped transaction keeps it: its table and the filter that names the row. */
|
|
54
|
+
export const rowLockKey = (table, filter) => `${table.id}\u0000${JSON.stringify(filter)}`;
|
|
55
|
+
export function scopedStatement(schema, within, request) {
|
|
56
|
+
let said;
|
|
57
|
+
void createScopedReads(schema, within, POSTGRES_STYLE, async (sql) => {
|
|
58
|
+
said = sql;
|
|
59
|
+
return { columns: [], rows: [], ms: 0, sql };
|
|
60
|
+
})
|
|
61
|
+
.query(request)
|
|
62
|
+
.catch(() => { });
|
|
63
|
+
return said;
|
|
64
|
+
}
|
|
65
|
+
/* Statements sent ahead that a committed transaction never asked: a hint
|
|
66
|
+
that no longer says what the transaction does. Warned once per statement
|
|
67
|
+
outside production; a refused transaction stops early, and leaves some by
|
|
68
|
+
design. With MISTRI_AHEAD_REPORT set, every transaction's account is a line
|
|
69
|
+
on stderr (e2e/write-storm.spec.ts reads it). */
|
|
70
|
+
const warnedAhead = new Set();
|
|
71
|
+
export function reportWriteAhead(report, committed) {
|
|
72
|
+
if (!report || (!report.sent && !report.missed))
|
|
73
|
+
return;
|
|
74
|
+
const said = (key) => {
|
|
75
|
+
try {
|
|
76
|
+
return String(JSON.parse(key)[0]).slice(0, 160);
|
|
77
|
+
}
|
|
78
|
+
catch {
|
|
79
|
+
return key.slice(0, 160);
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
if (process.env.MISTRI_AHEAD_REPORT)
|
|
83
|
+
process.stderr.write(`mistri-ahead ${JSON.stringify({ at: Date.now(), committed, sent: report.sent, taken: report.taken, answered: report.answered, missed: report.missed, left: report.left.map(said), redone: report.redone, asked: report.asked })}\n`);
|
|
84
|
+
if (!committed || process.env.NODE_ENV === 'production')
|
|
85
|
+
return;
|
|
86
|
+
for (const statement of report.left.map(said)) {
|
|
87
|
+
if (warnedAhead.has(statement))
|
|
88
|
+
continue;
|
|
89
|
+
warnedAhead.add(statement);
|
|
90
|
+
process.emitWarning(`A statement sent ahead was never asked by the transaction it was sent for: ${statement}`, 'MistriWriteAhead');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/** The statements openPlain sends behind the BEGIN; each exactly what the transaction sends in its turn (lockRecordNow, queryOn). */
|
|
94
|
+
export function sendPlainAhead(owner, write, schema, keyRows, journal, hint) {
|
|
95
|
+
if (!sendsAhead(write))
|
|
96
|
+
return [];
|
|
97
|
+
const opening = [];
|
|
98
|
+
try {
|
|
99
|
+
if (hint.receipt !== undefined && journal?.ahead)
|
|
100
|
+
opening.push(...journal.ahead(hint.receipt));
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
/* Asked in its turn. */
|
|
104
|
+
}
|
|
105
|
+
const answers = sendAhead(write, opening);
|
|
106
|
+
for (const target of hint.targets) {
|
|
107
|
+
const table = schema.tables.find((candidate) => candidate.id === catalogId(target.table));
|
|
108
|
+
if (!table || table.isView || isRuntimeRelation(table.id))
|
|
109
|
+
continue;
|
|
110
|
+
const pk = table.columns.filter((column) => column.primaryKey).map((column) => column.name);
|
|
111
|
+
const key = target.key;
|
|
112
|
+
if (!pk.length ||
|
|
113
|
+
Object.keys(key).length !== pk.length ||
|
|
114
|
+
!pk.every((column) => Object.hasOwn(key, column) && (typeof key[column] === 'string' || typeof key[column] === 'number')))
|
|
115
|
+
continue;
|
|
116
|
+
const filter = { groups: [pk.map((column) => ({ column, op: '=', value: key[column] }))] };
|
|
117
|
+
if (!hint.readOnly && (hint.locks || (hint.writes && capturesWrites(owner, table.id)))) {
|
|
118
|
+
/* lockRecordNow's statement, where the transaction will send it (the save's own lock, the history capture's). Held rather than answered once: the row stays locked until the transaction ends,
|
|
119
|
+
so each later lock of it in this transaction (the command's, the history capture's) is answered by it, even
|
|
120
|
+
after a write to another record has moved the statement generation. */
|
|
121
|
+
const [text, values] = rowLockStatement(table, key);
|
|
122
|
+
const lock = holdAhead(write, { text, values });
|
|
123
|
+
if (lock) {
|
|
124
|
+
answers.push(lock.answer);
|
|
125
|
+
const locks = heldLocks.get(write) ?? new Map();
|
|
126
|
+
heldLocks.set(write, locks);
|
|
127
|
+
const rows = heldRows.get(write) ?? new Set();
|
|
128
|
+
heldRows.set(write, rows);
|
|
129
|
+
void lock.answer.then((result) => {
|
|
130
|
+
if (result.rows.length <= 1)
|
|
131
|
+
locks.set(lockKey(text, values), lock.take);
|
|
132
|
+
// Only a row that is there is held: one that is not could be inserted by another transaction meanwhile.
|
|
133
|
+
if (result.rows.length === 1)
|
|
134
|
+
rows.add(lockKey(text, values));
|
|
135
|
+
}, () => { });
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const read = plainStatement({ table: table.id, filter, limit: 2, offset: 0 });
|
|
139
|
+
const row = read && holdAhead(write, { text: read.text, values: read.params });
|
|
140
|
+
if (row) {
|
|
141
|
+
answers.push(row.answer);
|
|
142
|
+
keyRows.keep(PLAIN_SCOPE, table, filter, row.answer.then((result) => result.rows), row.take);
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
return answers;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* The statements a scoped transaction opens with, sent behind its BEGIN
|
|
149
|
+
* (openWrite's `sent`), from what it was told (writeAhead.ts) and the scope
|
|
150
|
+
* catalog read last time: the verifier's own (its fence), the journal's
|
|
151
|
+
* receipt lookup, and for each target its row lock, its row read in full
|
|
152
|
+
* through the scope (kept to answer its reads by key, keyRows.ts), and,
|
|
153
|
+
* where the write is captured for history, the capture's own lock and read
|
|
154
|
+
* (historyWrites). Each is exactly the statement the transaction sends in
|
|
155
|
+
* its turn, built by the same rules; one it never sends is left, counted,
|
|
156
|
+
* and changes nothing. None where the client does not pipeline.
|
|
157
|
+
*/
|
|
158
|
+
export function sendWriteAhead(owner, write, scope, peeked, keyRows, journal, verify, hint, locked) {
|
|
159
|
+
if (!sendsAhead(write) || (!hint && !verify?.ahead))
|
|
160
|
+
return [];
|
|
161
|
+
const opening = [];
|
|
162
|
+
try {
|
|
163
|
+
opening.push(...(verify?.ahead?.() ?? []));
|
|
164
|
+
if (hint?.receipt !== undefined && journal?.ahead)
|
|
165
|
+
opening.push(...journal.ahead(hint.receipt));
|
|
166
|
+
}
|
|
167
|
+
catch {
|
|
168
|
+
/* Asked in its turn. */
|
|
169
|
+
}
|
|
170
|
+
const answers = sendAhead(write, opening);
|
|
171
|
+
for (const target of (peeked && hint?.targets) || []) {
|
|
172
|
+
const table = peeked.schema.tables.find((candidate) => candidate.id === catalogId(target.table));
|
|
173
|
+
if (!table || !peeked.lockable.has(table.id))
|
|
174
|
+
continue;
|
|
175
|
+
const pk = table.columns.filter((column) => column.primaryKey).map((column) => column.name);
|
|
176
|
+
const key = target.key;
|
|
177
|
+
if (!pk.length ||
|
|
178
|
+
Object.keys(key).length !== pk.length ||
|
|
179
|
+
!pk.every((column) => Object.hasOwn(key, column) && (typeof key[column] === 'string' || typeof key[column] === 'number')))
|
|
180
|
+
continue;
|
|
181
|
+
// The scoped transaction's own key filter and lock (scopedTransaction.ts keyFilter, `lock` above).
|
|
182
|
+
const filter = { groups: [pk.map((column) => ({ column, op: '=', value: key[column] }))] };
|
|
183
|
+
const where = buildWhere(filter, POSTGRES_STYLE);
|
|
184
|
+
if (!hint.readOnly) {
|
|
185
|
+
answers.push(...sendAhead(write, [{ text: `SELECT 1 FROM ${qualified(table)}${where.text} FOR UPDATE`, values: where.params }]));
|
|
186
|
+
locked.add(rowLockKey(table, filter));
|
|
187
|
+
}
|
|
188
|
+
// With its reference columns' stored text, as the scoped transaction reads its rows (storedText.ts).
|
|
189
|
+
const read = scopedStatement(peeked.schema, scope, { table: table.id, filter, limit: 2, offset: 0, storedText: true });
|
|
190
|
+
const row = read && holdAhead(write, { text: read.text, values: read.params });
|
|
191
|
+
if (row) {
|
|
192
|
+
answers.push(row.answer);
|
|
193
|
+
keyRows.keep(scope, table, filter, row.answer.then((result) => result.rows), row.take);
|
|
194
|
+
}
|
|
195
|
+
if (hint.writes && !hint.readOnly && capturesWrites(owner, table.id)) {
|
|
196
|
+
// historyWrites' read: lockRecordNow, then queryOn of the whole row by the key as given.
|
|
197
|
+
const given = buildWhere({ groups: [Object.entries(key).map(([column, value]) => ({ column, op: '=', value }))] }, POSTGRES_STYLE);
|
|
198
|
+
const n = given.params.length;
|
|
199
|
+
answers.push(...sendAhead(write, [
|
|
200
|
+
{
|
|
201
|
+
text: `SELECT ${pk.map(POSTGRES_STYLE.quote).join(', ')} FROM ${qualified(table)}${where.text} FOR UPDATE`,
|
|
202
|
+
values: where.params,
|
|
203
|
+
},
|
|
204
|
+
{ text: `SELECT * FROM ${qualified(table)}${given.text} LIMIT $${n + 1} OFFSET $${n + 2}`, values: [...given.params, 2, 0] },
|
|
205
|
+
]));
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
return answers;
|
|
209
|
+
}
|
|
@@ -28,6 +28,7 @@
|
|
|
28
28
|
* the read is asked in its turn rather than answered with no row.
|
|
29
29
|
*/
|
|
30
30
|
import { clampLimit } from '../adapter.js';
|
|
31
|
+
import { storedTextOf } from './storedText.js';
|
|
31
32
|
export class KeyRows {
|
|
32
33
|
generation;
|
|
33
34
|
#entries = new Map();
|
|
@@ -120,7 +121,11 @@ export class KeyRows {
|
|
|
120
121
|
// Asked again after a statement that moved the generation, of a column the row does not carry, or of a value it does not hold exactly: in its turn.
|
|
121
122
|
const answer = async (kept) => {
|
|
122
123
|
const rows = await kept.rows;
|
|
123
|
-
const names =
|
|
124
|
+
const names = [
|
|
125
|
+
...(request.columns?.length ? request.columns : table.columns.map((column) => column.name)),
|
|
126
|
+
// A scoped write's read of the stored texts is answered only by a row read with them (storedText.ts).
|
|
127
|
+
...(request.storedText ? [storedTextOf(schema, table)?.alias ?? []].flat() : []),
|
|
128
|
+
];
|
|
124
129
|
const sql = statementOf(request);
|
|
125
130
|
if (!rows ||
|
|
126
131
|
!sql ||
|
|
@@ -130,7 +135,7 @@ export class KeyRows {
|
|
|
130
135
|
return undefined;
|
|
131
136
|
kept.take?.();
|
|
132
137
|
return {
|
|
133
|
-
columns: [...
|
|
138
|
+
columns: request.columns?.length ? [...request.columns] : table.columns.map((column) => column.name),
|
|
134
139
|
rows: rows
|
|
135
140
|
.slice(0, clampLimit(request.limit))
|
|
136
141
|
.map((row) => format(Object.fromEntries(names.map((name) => [name, row[name]])), request)),
|
|
@@ -146,7 +151,8 @@ export class KeyRows {
|
|
|
146
151
|
return answered;
|
|
147
152
|
}
|
|
148
153
|
else if (key !== undefined && current !== undefined && widen?.held(table, guarded.values)) {
|
|
149
|
-
|
|
154
|
+
// With the stored texts, so the scoped write's own reads of the row are answered from it too.
|
|
155
|
+
const sent = widen.reads.query({ table: request.table, filter: request.filter, limit: 2, offset: 0, storedText: true });
|
|
150
156
|
/* Its generation is the one it was sent under: what waited for the COMMIT leaves in front of it, in its flight
|
|
151
157
|
(postgresTransactionHandle), moving the generation as it goes, and the row it reads is after that. Kept
|
|
152
158
|
under the generation before it was sent, it would answer nothing after it. Reads are sent as they are
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The types a scoped write takes as a row's identity (docs/trigger-effects.md, "Key types").
|
|
3
|
+
*
|
|
4
|
+
* Under a row policy, every check names a row by a value: a write's own row by its primary key, a parent by the
|
|
5
|
+
* reference that points at it, a row a trigger reaches by its effect's key. The check is sound only where the value
|
|
6
|
+
* it binds is the value the database compares. Six reviews found the same defect in new places: the driver's
|
|
7
|
+
* JavaScript reading of a jsonb key (2^53 rounded, "123" read as 123), SQLite's numeric(p,s) said with its scale
|
|
8
|
+
* (19.987 read as "19.99"), a float or timestamp whose text depends on the session's settings. So a column that is a
|
|
9
|
+
* row's identity — a primary key, either side of a foreign key, either side of a trigger effect's key — must be of a
|
|
10
|
+
* type whose value reads back exactly, through the driver and through text, and any other is refused from the
|
|
11
|
+
* catalog, before a value is read:
|
|
12
|
+
*
|
|
13
|
+
* - PostgreSQL, a domain read as the type it is over: text, varchar, char, name, citext; int2, int4, int8; numeric
|
|
14
|
+
* (the driver reads it as its exact text); uuid; date (its text pinned to ISO for the transaction, postgres.ts);
|
|
15
|
+
* boolean; an enum. Never json or jsonb, a float, a time or timestamp, an interval, money, bytea, a network
|
|
16
|
+
* address, an array, a range or a composite.
|
|
17
|
+
* - SQLite, by the affinity its declared type gives: INTEGER or TEXT. Never REAL, NUMERIC (a decimal with a declared
|
|
18
|
+
* scale is said with it), BLOB or a column declared without a type; except that a typeless column may reference an
|
|
19
|
+
* allowed key, which its foreign key compares as the check does (sqliteScopeCatalog.ts affinitiesPart).
|
|
20
|
+
*/
|
|
21
|
+
/** Why a column of `type` cannot be a row's identity in a scoped write, as a refusal says it (no full stop: it is said inside others). */
|
|
22
|
+
export declare const keyTypeRefusal: (dialect: 'postgres' | 'sqlite', table: string, column: string, type: string) => string;
|
|
23
|
+
/** SQLite's affinity for a declared type (datatype3.html, 3.1), and ANY as itself: a STRICT table's ANY converts nothing. */
|
|
24
|
+
export declare function sqliteAffinity(declared: string): string;
|
|
25
|
+
/** Whether a SQLite column declared `declared` may be a row's identity in a scoped write. */
|
|
26
|
+
export declare const sqliteKeyType: (declared: string) => boolean;
|
|
27
|
+
/** A SQLite column's type as a refusal names it. */
|
|
28
|
+
export declare const sqliteTypeSaid: (declared: string) => string;
|
|
29
|
+
/**
|
|
30
|
+
* Whether the base type `type` (an oid expression, its domains already unwrapped) may be a row's identity, as a SQL
|
|
31
|
+
* boolean: one of the built-in types above, an enum, or the citext extension's own type (an extension's member is
|
|
32
|
+
* recorded in pg_depend, so a type merely named citext is not it). Every operator named in pg_catalog.
|
|
33
|
+
*/
|
|
34
|
+
export declare const postgresKeyType: (type: string) => string;
|