@syncular/server 0.95.0 → 0.96.0
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/README.md +6 -5
- package/dist/context.d.ts +4 -16
- package/dist/context.js +0 -3
- package/dist/errors.js +8 -0
- package/dist/frame-bytes.js +1 -0
- package/dist/handler.d.ts +20 -2
- package/dist/handler.js +195 -58
- package/dist/postgres-storage.d.ts +3 -2
- package/dist/postgres-storage.js +18 -2
- package/dist/pull.d.ts +2 -2
- package/dist/pull.js +12 -11
- package/dist/push.js +41 -77
- package/dist/realtime.d.ts +5 -11
- package/dist/realtime.js +10 -23
- package/dist/relational-rows.d.ts +4 -2
- package/dist/relational-rows.js +8 -3
- package/dist/schema-window.js +2 -2
- package/dist/schema.d.ts +2 -2
- package/dist/schema.js +2 -2
- package/dist/sqlite-dialect.d.ts +3 -1
- package/dist/sqlite-dialect.js +37 -5
- package/dist/storage.d.ts +12 -0
- package/dist/validate.d.ts +21 -39
- package/dist/validate.js +4 -3
- package/package.json +2 -2
- package/src/context.ts +4 -23
- package/src/errors.ts +8 -0
- package/src/frame-bytes.ts +1 -0
- package/src/handler.ts +251 -72
- package/src/postgres-storage.ts +32 -6
- package/src/pull.ts +22 -12
- package/src/push.ts +51 -129
- package/src/realtime.ts +10 -22
- package/src/relational-rows.ts +10 -3
- package/src/schema-window.ts +2 -2
- package/src/schema.ts +4 -4
- package/src/sqlite-dialect.ts +47 -6
- package/src/storage.ts +13 -0
- package/src/validate.ts +21 -48
package/src/push.ts
CHANGED
|
@@ -71,7 +71,6 @@ import type {
|
|
|
71
71
|
CommitValidationRow,
|
|
72
72
|
CommitValidator,
|
|
73
73
|
ValidateCommitOperation,
|
|
74
|
-
ServerColumnsRegistry,
|
|
75
74
|
ValidateOpKind,
|
|
76
75
|
ValidatorRegistry,
|
|
77
76
|
} from './validate';
|
|
@@ -222,31 +221,36 @@ async function resolveBlobPresence(
|
|
|
222
221
|
}
|
|
223
222
|
|
|
224
223
|
/**
|
|
225
|
-
* §6.7: run the table's
|
|
226
|
-
*
|
|
227
|
-
*
|
|
228
|
-
*
|
|
229
|
-
*
|
|
230
|
-
*
|
|
231
|
-
* keeps
|
|
224
|
+
* §6.7: run the table's row hook, if configured, on the row that will persist
|
|
225
|
+
* (post scope-strip, post CRDT-merge). With `present` (an accepted insert or
|
|
226
|
+
* update of a pushed commit or a registered command) the hook's returned
|
|
227
|
+
* values for server-owned columns land in `values` and are marked present, so
|
|
228
|
+
* their column versions advance; elsewhere (a delete, a rescope, a §6.11
|
|
229
|
+
* cascade) the server ignores them. Returns a terminating outcome iff the
|
|
230
|
+
* hook fails: a `ValidationRejection` keeps its host code, and any other
|
|
231
|
+
* throw, a returned column that is not server-owned, a value the column's
|
|
232
|
+
* type refuses or a changed scope column on an update is
|
|
233
|
+
* `sync.constraint_violation`.
|
|
232
234
|
*/
|
|
233
235
|
async function runValidator(
|
|
234
236
|
validators: ValidatorRegistry | undefined,
|
|
237
|
+
tx: StorageTransaction,
|
|
238
|
+
schema: CompiledSchema,
|
|
235
239
|
table: CompiledTable,
|
|
236
240
|
op: ValidateOpKind,
|
|
237
241
|
rowId: string,
|
|
238
|
-
values:
|
|
242
|
+
values: RowValue[] | undefined,
|
|
239
243
|
storedValues: readonly RowValue[] | undefined,
|
|
240
244
|
opIndex: number,
|
|
241
|
-
tx: StorageTransaction,
|
|
242
245
|
partition: string,
|
|
243
246
|
actorId: string,
|
|
244
247
|
context: string | undefined,
|
|
248
|
+
present?: boolean[],
|
|
245
249
|
): Promise<TerminatingOutcome | undefined> {
|
|
246
250
|
const validator = validators?.[table.name];
|
|
247
251
|
if (validator === undefined) return undefined;
|
|
248
252
|
try {
|
|
249
|
-
await validator(
|
|
253
|
+
const output = await validator(
|
|
250
254
|
{
|
|
251
255
|
op,
|
|
252
256
|
table: table.name,
|
|
@@ -260,69 +264,6 @@ async function runValidator(
|
|
|
260
264
|
? toValidateRow(table.columns, storedValues)
|
|
261
265
|
: undefined,
|
|
262
266
|
},
|
|
263
|
-
{
|
|
264
|
-
actorId,
|
|
265
|
-
partition,
|
|
266
|
-
...(context !== undefined ? { context } : {}),
|
|
267
|
-
queryAuthoritative: (query) => transactionQuery(tx, query),
|
|
268
|
-
},
|
|
269
|
-
);
|
|
270
|
-
} catch (error) {
|
|
271
|
-
if (error instanceof ValidationRejection) {
|
|
272
|
-
return errorRecord(
|
|
273
|
-
opIndex,
|
|
274
|
-
error.code,
|
|
275
|
-
error.message,
|
|
276
|
-
false,
|
|
277
|
-
error.details,
|
|
278
|
-
);
|
|
279
|
-
}
|
|
280
|
-
// §6.7: a non-ValidationRejection throw is still a rejection, mapped to
|
|
281
|
-
// the generic server-side constraint code (§10.2) — the validator's
|
|
282
|
-
// failure never crashes the request or leaks its message as a code.
|
|
283
|
-
return errorRecord(
|
|
284
|
-
opIndex,
|
|
285
|
-
'sync.constraint_violation',
|
|
286
|
-
'write validator failed',
|
|
287
|
-
);
|
|
288
|
-
}
|
|
289
|
-
return undefined;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* §6.12: complete an accepted upsert with the table's server column hook.
|
|
294
|
-
* The returned values land in `values` and are marked present, so their
|
|
295
|
-
* column versions advance. A `ValidationRejection` keeps its host code; any
|
|
296
|
-
* other failure (an undeclared key, a value the column's type refuses, a
|
|
297
|
-
* changed scope column on an update) is `sync.constraint_violation`.
|
|
298
|
-
*/
|
|
299
|
-
async function runServerColumns(
|
|
300
|
-
hooks: ServerColumnsRegistry | undefined,
|
|
301
|
-
tx: StorageTransaction,
|
|
302
|
-
schema: CompiledSchema,
|
|
303
|
-
table: CompiledTable,
|
|
304
|
-
rowId: string,
|
|
305
|
-
values: RowValue[],
|
|
306
|
-
storedValues: readonly RowValue[] | undefined,
|
|
307
|
-
present: boolean[],
|
|
308
|
-
opIndex: number,
|
|
309
|
-
partition: string,
|
|
310
|
-
actorId: string,
|
|
311
|
-
context: string | undefined,
|
|
312
|
-
): Promise<TerminatingOutcome | undefined> {
|
|
313
|
-
const hook = hooks?.[table.name];
|
|
314
|
-
if (hook === undefined) return undefined;
|
|
315
|
-
try {
|
|
316
|
-
const output = await hook(
|
|
317
|
-
{
|
|
318
|
-
table: table.name,
|
|
319
|
-
rowId,
|
|
320
|
-
row: toValidateRow(table.columns, values),
|
|
321
|
-
stored:
|
|
322
|
-
storedValues !== undefined
|
|
323
|
-
? toValidateRow(table.columns, storedValues)
|
|
324
|
-
: undefined,
|
|
325
|
-
},
|
|
326
267
|
{
|
|
327
268
|
actorId,
|
|
328
269
|
partition,
|
|
@@ -330,6 +271,8 @@ async function runServerColumns(
|
|
|
330
271
|
read: commitValidationReader(tx, schema),
|
|
331
272
|
},
|
|
332
273
|
);
|
|
274
|
+
if (output === undefined || present === undefined || values === undefined)
|
|
275
|
+
return undefined;
|
|
333
276
|
for (const [name, value] of Object.entries(output)) {
|
|
334
277
|
const index = table.columnIndex.get(name);
|
|
335
278
|
if (
|
|
@@ -337,7 +280,7 @@ async function runServerColumns(
|
|
|
337
280
|
!table.serverOwnedColumnIndices.includes(index)
|
|
338
281
|
) {
|
|
339
282
|
throw new Error(
|
|
340
|
-
'
|
|
283
|
+
'write validator returned a column that is not server-owned',
|
|
341
284
|
);
|
|
342
285
|
}
|
|
343
286
|
// The column's own codec refuses a value of the wrong type.
|
|
@@ -347,7 +290,7 @@ async function runServerColumns(
|
|
|
347
290
|
table.scopePatterns.some((pattern) => pattern.columnIndex === index) &&
|
|
348
291
|
value !== (storedValues[index] ?? null)
|
|
349
292
|
) {
|
|
350
|
-
throw new Error('
|
|
293
|
+
throw new Error('write validator changed a scope column on update');
|
|
351
294
|
}
|
|
352
295
|
values[index] = value;
|
|
353
296
|
present[index] = true;
|
|
@@ -362,10 +305,13 @@ async function runServerColumns(
|
|
|
362
305
|
error.details,
|
|
363
306
|
);
|
|
364
307
|
}
|
|
308
|
+
// §6.7: any other failure is still a rejection, mapped to the generic
|
|
309
|
+
// server-side constraint code (§10.2); the hook's failure never crashes
|
|
310
|
+
// the request or leaks its message.
|
|
365
311
|
return errorRecord(
|
|
366
312
|
opIndex,
|
|
367
313
|
'sync.constraint_violation',
|
|
368
|
-
'
|
|
314
|
+
'write validator failed',
|
|
369
315
|
);
|
|
370
316
|
}
|
|
371
317
|
return undefined;
|
|
@@ -435,7 +381,6 @@ async function applyOperation(
|
|
|
435
381
|
blobCtx: BlobApplyContext,
|
|
436
382
|
crdtMergers: CrdtMergerRegistry | undefined,
|
|
437
383
|
validators: ValidatorRegistry | undefined,
|
|
438
|
-
serverColumns: ServerColumnsRegistry | undefined,
|
|
439
384
|
partition: string,
|
|
440
385
|
actorId: string,
|
|
441
386
|
context: string | undefined,
|
|
@@ -558,13 +503,14 @@ async function applyOperation(
|
|
|
558
503
|
}
|
|
559
504
|
const deleteReject = await runValidator(
|
|
560
505
|
validators,
|
|
506
|
+
tx,
|
|
507
|
+
schema,
|
|
561
508
|
table,
|
|
562
509
|
'delete',
|
|
563
510
|
op.rowId,
|
|
564
511
|
undefined,
|
|
565
512
|
storedValues,
|
|
566
513
|
opIndex,
|
|
567
|
-
tx,
|
|
568
514
|
partition,
|
|
569
515
|
actorId,
|
|
570
516
|
context,
|
|
@@ -572,13 +518,14 @@ async function applyOperation(
|
|
|
572
518
|
if (deleteReject !== undefined) return deleteReject;
|
|
573
519
|
const upsertReject = await runValidator(
|
|
574
520
|
validators,
|
|
521
|
+
tx,
|
|
522
|
+
schema,
|
|
575
523
|
table,
|
|
576
524
|
'upsert',
|
|
577
525
|
op.rowId,
|
|
578
526
|
values,
|
|
579
527
|
storedValues,
|
|
580
528
|
opIndex,
|
|
581
|
-
tx,
|
|
582
529
|
partition,
|
|
583
530
|
actorId,
|
|
584
531
|
context,
|
|
@@ -715,13 +662,14 @@ async function applyOperation(
|
|
|
715
662
|
const storedValues = decodeRow(table.columns, stored.payload);
|
|
716
663
|
const deleteReject = await runValidator(
|
|
717
664
|
validators,
|
|
665
|
+
tx,
|
|
666
|
+
schema,
|
|
718
667
|
table,
|
|
719
668
|
'delete',
|
|
720
669
|
op.rowId,
|
|
721
670
|
undefined,
|
|
722
671
|
storedValues,
|
|
723
672
|
opIndex,
|
|
724
|
-
tx,
|
|
725
673
|
partition,
|
|
726
674
|
actorId,
|
|
727
675
|
context,
|
|
@@ -783,7 +731,7 @@ async function applyOperation(
|
|
|
783
731
|
`row payload failed row-codec decode (§1.7): ${error instanceof Error ? error.message : String(error)}`,
|
|
784
732
|
);
|
|
785
733
|
}
|
|
786
|
-
// §6.
|
|
734
|
+
// §6.7: the server discards a pushed value of a server-owned column, so an
|
|
787
735
|
// older client that still writes one stays valid.
|
|
788
736
|
for (const index of table.serverOwnedColumnIndices) {
|
|
789
737
|
sparse[index] = stored === undefined ? null : undefined;
|
|
@@ -897,21 +845,24 @@ async function applyOperation(
|
|
|
897
845
|
crdtMergers,
|
|
898
846
|
);
|
|
899
847
|
if ('kind' in mergeOutcome) return mergeOutcome;
|
|
900
|
-
|
|
901
|
-
|
|
848
|
+
// §6.7: validate the merged row that will persist (for a crdt column the
|
|
849
|
+
// MERGED value, §5.10.3) and take its server-owned values.
|
|
850
|
+
const updateReject = await runValidator(
|
|
851
|
+
validators,
|
|
902
852
|
tx,
|
|
903
853
|
schema,
|
|
904
854
|
table,
|
|
855
|
+
'upsert',
|
|
905
856
|
op.rowId,
|
|
906
857
|
values,
|
|
907
858
|
storedValues,
|
|
908
|
-
present,
|
|
909
859
|
opIndex,
|
|
910
860
|
partition,
|
|
911
861
|
actorId,
|
|
912
862
|
context,
|
|
863
|
+
present,
|
|
913
864
|
);
|
|
914
|
-
if (
|
|
865
|
+
if (updateReject !== undefined) return updateReject;
|
|
915
866
|
const newVersion = stored.serverVersion + 1;
|
|
916
867
|
const nextVersions = new Map<number, number>();
|
|
917
868
|
for (let i = 0; i < table.columns.length; i++) {
|
|
@@ -936,23 +887,6 @@ async function applyOperation(
|
|
|
936
887
|
blobCtx,
|
|
937
888
|
);
|
|
938
889
|
if (blobCheck !== undefined) return blobCheck;
|
|
939
|
-
// §6.7: validate the merged row that will persist — for a crdt column
|
|
940
|
-
// the validator sees the MERGED value (§5.10.3), the state the store
|
|
941
|
-
// holds, not the raw pushed update.
|
|
942
|
-
const updateReject = await runValidator(
|
|
943
|
-
validators,
|
|
944
|
-
table,
|
|
945
|
-
'upsert',
|
|
946
|
-
op.rowId,
|
|
947
|
-
values,
|
|
948
|
-
storedValues,
|
|
949
|
-
opIndex,
|
|
950
|
-
tx,
|
|
951
|
-
partition,
|
|
952
|
-
actorId,
|
|
953
|
-
context,
|
|
954
|
-
);
|
|
955
|
-
if (updateReject !== undefined) return updateReject;
|
|
956
890
|
const columnVersions = encodeColumnVersions(nextVersions);
|
|
957
891
|
const newRow = {
|
|
958
892
|
rowId: op.rowId,
|
|
@@ -1078,29 +1012,33 @@ async function applyOperation(
|
|
|
1078
1012
|
crdtMergers,
|
|
1079
1013
|
);
|
|
1080
1014
|
if ('kind' in insertMerge) return insertMerge;
|
|
1081
|
-
|
|
1082
|
-
|
|
1015
|
+
// §6.7: validate the insert row (stored = undefined, so a validator can
|
|
1016
|
+
// distinguish create from update; crdt columns are already merged against
|
|
1017
|
+
// the empty document) and take its server-owned values.
|
|
1018
|
+
const insertReject = await runValidator(
|
|
1019
|
+
validators,
|
|
1083
1020
|
tx,
|
|
1084
1021
|
schema,
|
|
1085
1022
|
table,
|
|
1023
|
+
'upsert',
|
|
1086
1024
|
op.rowId,
|
|
1087
1025
|
values,
|
|
1088
1026
|
undefined,
|
|
1089
|
-
present,
|
|
1090
1027
|
opIndex,
|
|
1091
1028
|
partition,
|
|
1092
1029
|
actorId,
|
|
1093
1030
|
context,
|
|
1031
|
+
present,
|
|
1094
1032
|
);
|
|
1095
|
-
if (
|
|
1096
|
-
// §6.
|
|
1033
|
+
if (insertReject !== undefined) return insertReject;
|
|
1034
|
+
// §6.7: the stored scopes and the change-scope index take the validator's
|
|
1097
1035
|
// values, so the row reaches a server-set scope's units in this commit.
|
|
1098
1036
|
const scoped = storedScopesForRow(table, values);
|
|
1099
1037
|
if ('missing' in scoped) {
|
|
1100
1038
|
return errorRecord(
|
|
1101
1039
|
opIndex,
|
|
1102
1040
|
'sync.constraint_violation',
|
|
1103
|
-
'
|
|
1041
|
+
'write validator failed',
|
|
1104
1042
|
);
|
|
1105
1043
|
}
|
|
1106
1044
|
// The stored payload is the full-row codec (§2.4); every column is
|
|
@@ -1116,23 +1054,6 @@ async function applyOperation(
|
|
|
1116
1054
|
blobCtx,
|
|
1117
1055
|
);
|
|
1118
1056
|
if (blobCheck !== undefined) return blobCheck;
|
|
1119
|
-
// §6.7: validate the insert row (stored = undefined, so a validator can
|
|
1120
|
-
// distinguish create from update); crdt columns are already merged
|
|
1121
|
-
// against the empty document.
|
|
1122
|
-
const insertReject = await runValidator(
|
|
1123
|
-
validators,
|
|
1124
|
-
table,
|
|
1125
|
-
'upsert',
|
|
1126
|
-
op.rowId,
|
|
1127
|
-
values,
|
|
1128
|
-
undefined,
|
|
1129
|
-
opIndex,
|
|
1130
|
-
tx,
|
|
1131
|
-
partition,
|
|
1132
|
-
actorId,
|
|
1133
|
-
context,
|
|
1134
|
-
);
|
|
1135
|
-
if (insertReject !== undefined) return insertReject;
|
|
1136
1057
|
// §2.2, §6.2: every insert starts one above the delete floor, over a
|
|
1137
1058
|
// tombstone or not; without a column-versions blob every column sits at
|
|
1138
1059
|
// that version.
|
|
@@ -1701,13 +1622,14 @@ async function enforceReferences(
|
|
|
1701
1622
|
if (reference.onDelete === 'CASCADE') {
|
|
1702
1623
|
const reject = await runValidator(
|
|
1703
1624
|
validators,
|
|
1625
|
+
tx,
|
|
1626
|
+
schema,
|
|
1704
1627
|
child,
|
|
1705
1628
|
'delete',
|
|
1706
1629
|
childRow.rowId,
|
|
1707
1630
|
undefined,
|
|
1708
1631
|
storedValues,
|
|
1709
1632
|
item.originOpIndex,
|
|
1710
|
-
tx,
|
|
1711
1633
|
partition,
|
|
1712
1634
|
actorId,
|
|
1713
1635
|
context,
|
|
@@ -1737,13 +1659,14 @@ async function enforceReferences(
|
|
|
1737
1659
|
values[reference.columnIndex] = null;
|
|
1738
1660
|
const reject = await runValidator(
|
|
1739
1661
|
validators,
|
|
1662
|
+
tx,
|
|
1663
|
+
schema,
|
|
1740
1664
|
child,
|
|
1741
1665
|
'upsert',
|
|
1742
1666
|
childRow.rowId,
|
|
1743
1667
|
values,
|
|
1744
1668
|
storedValues,
|
|
1745
1669
|
item.originOpIndex,
|
|
1746
|
-
tx,
|
|
1747
1670
|
partition,
|
|
1748
1671
|
actorId,
|
|
1749
1672
|
context,
|
|
@@ -2243,7 +2166,6 @@ async function applyPushCommits(
|
|
|
2243
2166
|
blobCtx,
|
|
2244
2167
|
crdtMergers,
|
|
2245
2168
|
validators,
|
|
2246
|
-
ctx.serverColumns,
|
|
2247
2169
|
partition,
|
|
2248
2170
|
ctx.actorId,
|
|
2249
2171
|
ctx.context,
|
package/src/realtime.ts
CHANGED
|
@@ -734,8 +734,8 @@ export class RealtimeSession {
|
|
|
734
734
|
try {
|
|
735
735
|
step = await iterator.next();
|
|
736
736
|
} catch (error) {
|
|
737
|
-
// The
|
|
738
|
-
// refusal)
|
|
737
|
+
// The first `next()` waits until the response is built (and the
|
|
738
|
+
// read-verify refusal ran) or its first KEEPALIVE is due (§1.6).
|
|
739
739
|
// Nothing has escaped, so a failure there is delivered as the same
|
|
740
740
|
// §8.4 ERROR frame HTTP returns rather than a silent close; a
|
|
741
741
|
// non-catalog exception is reported and answers
|
|
@@ -1563,9 +1563,11 @@ export class RealtimeHub {
|
|
|
1563
1563
|
|
|
1564
1564
|
/**
|
|
1565
1565
|
* Host-initiated, fail-closed registration refresh (§8.7). A host calls this
|
|
1566
|
-
* after it changes an actor's
|
|
1567
|
-
*
|
|
1568
|
-
* without waiting for
|
|
1566
|
+
* after it changes an actor's grants, whether they shrank or grew. Each
|
|
1567
|
+
* matching session re-resolves its registrations, so it stops using grants
|
|
1568
|
+
* the host already revoked without waiting for its client to run a round,
|
|
1569
|
+
* and then receives one `scopes` event, so its client's app runs its
|
|
1570
|
+
* discovery and registers units it did not know.
|
|
1569
1571
|
*
|
|
1570
1572
|
* This differs deliberately from the round-end refresh
|
|
1571
1573
|
* (`RealtimeSession.refreshRegistrations`), which keeps the previous
|
|
@@ -1578,24 +1580,10 @@ export class RealtimeHub {
|
|
|
1578
1580
|
for (const session of [...this.#sessions]) {
|
|
1579
1581
|
if (session.partition !== partition) continue;
|
|
1580
1582
|
if (actorId !== undefined && session.actorId !== actorId) continue;
|
|
1581
|
-
//
|
|
1582
|
-
//
|
|
1583
|
-
// this revocation from being overwritten by a slower round-end reload.
|
|
1583
|
+
// The session's generation guard keeps this revocation from being
|
|
1584
|
+
// overwritten by a slower round-end reload.
|
|
1584
1585
|
await session.refreshRegistrations(true);
|
|
1585
|
-
|
|
1586
|
-
}
|
|
1587
|
-
|
|
1588
|
-
/**
|
|
1589
|
-
* Host announcement (§8.7): tell the connected sessions of `actorIds` on
|
|
1590
|
-
* the partition that their grant widened, so each client's app runs its
|
|
1591
|
-
* discovery and registers the units it did not know. Sends one `scopes`
|
|
1592
|
-
* event per session and changes no registration; a narrowed grant needs
|
|
1593
|
-
* `refreshScopes`.
|
|
1594
|
-
*/
|
|
1595
|
-
announceScopes(partition: string, actorIds: readonly string[]): void {
|
|
1596
|
-
for (const session of this.#sessions) {
|
|
1597
|
-
if (session.partition !== partition) continue;
|
|
1598
|
-
if (actorIds.includes(session.actorId)) session.sendScopes();
|
|
1586
|
+
session.sendScopes();
|
|
1599
1587
|
}
|
|
1600
1588
|
}
|
|
1601
1589
|
|
package/src/relational-rows.ts
CHANGED
|
@@ -1010,8 +1010,10 @@ export function assertStoredScopePatterns(
|
|
|
1010
1010
|
|
|
1011
1011
|
/**
|
|
1012
1012
|
* §3.1 re-key, before the row walk: drop the re-keyed tables' row scope index
|
|
1013
|
-
* entries and tombstones
|
|
1014
|
-
*
|
|
1013
|
+
* entries and tombstones. The re-key takes a commit sequence number of its
|
|
1014
|
+
* own in every partition and records it as each re-keyed table's horizon
|
|
1015
|
+
* (§4.6), past the changes logged under the previous patterns. Empty when
|
|
1016
|
+
* nothing re-keys.
|
|
1015
1017
|
*/
|
|
1016
1018
|
export function rekeyCleanupSql(tables: readonly string[]): string[] {
|
|
1017
1019
|
if (tables.length === 0) return [];
|
|
@@ -1019,7 +1021,12 @@ export function rekeyCleanupSql(tables: readonly string[]): string[] {
|
|
|
1019
1021
|
return [
|
|
1020
1022
|
`DELETE FROM sync_row_scopes WHERE tbl IN (${names.join(',')})`,
|
|
1021
1023
|
`DELETE FROM sync_tombstones WHERE tbl IN (${names.join(',')})`,
|
|
1022
|
-
'UPDATE sync_partitions SET
|
|
1024
|
+
'UPDATE sync_partitions SET max_commit_seq=max_commit_seq+1',
|
|
1025
|
+
...names.map(
|
|
1026
|
+
(name) => `INSERT INTO sync_table_horizons(partition, tbl, commit_seq)
|
|
1027
|
+
SELECT partition, ${name}, max_commit_seq FROM sync_partitions WHERE true
|
|
1028
|
+
ON CONFLICT(partition, tbl) DO UPDATE SET commit_seq=excluded.commit_seq`,
|
|
1029
|
+
),
|
|
1023
1030
|
];
|
|
1024
1031
|
}
|
|
1025
1032
|
|
package/src/schema-window.ts
CHANGED
|
@@ -31,7 +31,7 @@ export function schemaWindowOf(
|
|
|
31
31
|
if (
|
|
32
32
|
next === undefined ||
|
|
33
33
|
table.columns.length > next.columns.length ||
|
|
34
|
-
// §6.
|
|
34
|
+
// §6.7: a version may mark an existing column server-owned; the flag
|
|
35
35
|
// never reaches the wire.
|
|
36
36
|
table.columns.some(
|
|
37
37
|
(column, i) =>
|
|
@@ -42,7 +42,7 @@ export function schemaWindowOf(
|
|
|
42
42
|
.slice(table.columns.length)
|
|
43
43
|
.some((column) => !column.nullable) ||
|
|
44
44
|
table.primaryKeyIndex !== next.primaryKeyIndex ||
|
|
45
|
-
// §6.
|
|
45
|
+
// §6.7: a pattern over an appended server-owned column is new to the
|
|
46
46
|
// current version; an older client neither holds nor writes it.
|
|
47
47
|
JSON.stringify(table.scopePatterns) !==
|
|
48
48
|
JSON.stringify(
|
package/src/schema.ts
CHANGED
|
@@ -154,8 +154,8 @@ export interface CompiledTable {
|
|
|
154
154
|
* image copies ciphertext wholesale with no per-row decrypt pass, so an
|
|
155
155
|
* encrypted table MUST be served via the rows lane. Empty ⇒ no restriction. */
|
|
156
156
|
readonly encryptedColumnIndices: readonly number[];
|
|
157
|
-
/** §6.
|
|
158
|
-
*
|
|
157
|
+
/** §6.7: column indices marked `serverOwned`. Empty ⇒ no server column
|
|
158
|
+
* values are accepted from the table's validator. */
|
|
159
159
|
readonly serverOwnedColumnIndices: readonly number[];
|
|
160
160
|
}
|
|
161
161
|
|
|
@@ -404,7 +404,7 @@ export function compileSchema(schema: ServerSchema): CompiledSchema {
|
|
|
404
404
|
if (column.type === 'blob_ref') blobRefColumnIndices.push(index);
|
|
405
405
|
// §5.11: an encrypted column carries wire type `bytes` + `encrypted`.
|
|
406
406
|
if (column.encrypted === true) encryptedColumnIndices.push(index);
|
|
407
|
-
// §6.
|
|
407
|
+
// §6.7: a validator never changes who may write the row (owner set), and
|
|
408
408
|
// a client's optimistic row holds NULL until the server row arrives.
|
|
409
409
|
if (column.serverOwned === true) {
|
|
410
410
|
if (
|
|
@@ -419,7 +419,7 @@ export function compileSchema(schema: ServerSchema): CompiledSchema {
|
|
|
419
419
|
)
|
|
420
420
|
) {
|
|
421
421
|
throw new Error(
|
|
422
|
-
`table ${table.name}: server-owned column ${JSON.stringify(column.name)} must be nullable and outside the primary key, the owner write set, crdt and encryption (§6.
|
|
422
|
+
`table ${table.name}: server-owned column ${JSON.stringify(column.name)} must be nullable and outside the primary key, the owner write set, crdt and encryption (§6.7)`,
|
|
423
423
|
);
|
|
424
424
|
}
|
|
425
425
|
serverOwnedColumnIndices.push(index);
|
package/src/sqlite-dialect.ts
CHANGED
|
@@ -46,8 +46,10 @@ import type { CompiledTable } from './schema';
|
|
|
46
46
|
import { matchesEffective } from './scopes';
|
|
47
47
|
import type { SqliteValue } from './sqlite-driver';
|
|
48
48
|
import {
|
|
49
|
+
asJson,
|
|
49
50
|
type ClientRowRecord,
|
|
50
51
|
CLIENT_RECORD_COLUMNS,
|
|
52
|
+
type PartitionHorizons,
|
|
51
53
|
PARTITION_TOUCH_INTERVAL_MS,
|
|
52
54
|
pushResultFromJson,
|
|
53
55
|
pushResultToJson,
|
|
@@ -257,6 +259,20 @@ const SQLITE_ADDED_COLUMNS = [
|
|
|
257
259
|
['sync_partitions', 'delete_floor', 'INTEGER NOT NULL DEFAULT 0'],
|
|
258
260
|
] as const;
|
|
259
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Core tables that `sqliteUpgradeDdl` creates, keyed by one of their columns,
|
|
264
|
+
* so a D1 database at an unchanged schema version gains them at its first
|
|
265
|
+
* open. `sync_table_horizons` holds the §4.6 table horizons.
|
|
266
|
+
*/
|
|
267
|
+
const SQLITE_ADDED_TABLES = [
|
|
268
|
+
[
|
|
269
|
+
'sync_table_horizons',
|
|
270
|
+
'commit_seq',
|
|
271
|
+
// One line: D1's `exec` runs each line as a statement.
|
|
272
|
+
'CREATE TABLE IF NOT EXISTS sync_table_horizons(partition TEXT NOT NULL, tbl TEXT NOT NULL, commit_seq INTEGER NOT NULL, PRIMARY KEY(partition, tbl))',
|
|
273
|
+
],
|
|
274
|
+
] as const;
|
|
275
|
+
|
|
260
276
|
/** Core-table columns wire version 4 deleted: the version 3 subscription list. */
|
|
261
277
|
const SQLITE_DROPPED_COLUMNS = [['sync_clients', 'subscriptions']] as const;
|
|
262
278
|
|
|
@@ -294,9 +310,11 @@ export const SQLITE_UPGRADE_PROBE: SqliteStatementSpec = {
|
|
|
294
310
|
params: [
|
|
295
311
|
JSON.stringify([
|
|
296
312
|
...new Set(
|
|
297
|
-
[
|
|
298
|
-
|
|
299
|
-
|
|
313
|
+
[
|
|
314
|
+
...SQLITE_ADDED_COLUMNS,
|
|
315
|
+
...SQLITE_DROPPED_COLUMNS,
|
|
316
|
+
...SQLITE_ADDED_TABLES,
|
|
317
|
+
].map(([table]) => table),
|
|
300
318
|
),
|
|
301
319
|
]),
|
|
302
320
|
JSON.stringify([
|
|
@@ -309,7 +327,7 @@ export const SQLITE_UPGRADE_PROBE: SqliteStatementSpec = {
|
|
|
309
327
|
/**
|
|
310
328
|
* The statements that bring the core tables of a database from an earlier
|
|
311
329
|
* release to the current DDL, given the `SQLITE_UPGRADE_PROBE` rows: missing
|
|
312
|
-
* columns, dropped columns, dropped indexes, then missing indexes. Empty for
|
|
330
|
+
* tables and columns, dropped columns, dropped indexes, then missing indexes. Empty for
|
|
313
331
|
* a current database. Run after `SQLITE_DDL`: on every `SqliteServerStorage`
|
|
314
332
|
* construction, and on D1 in `migrate()`, the core phase of `migrateSchema`
|
|
315
333
|
* and the first open of a database at the current schema version.
|
|
@@ -320,6 +338,9 @@ export function sqliteUpgradeDdl(
|
|
|
320
338
|
const has = (table: string, name: string) =>
|
|
321
339
|
present.some((row) => row.tbl === table && row.name === name);
|
|
322
340
|
return [
|
|
341
|
+
...SQLITE_ADDED_TABLES.filter(([table, name]) => !has(table, name)).map(
|
|
342
|
+
([, , ddl]) => ddl,
|
|
343
|
+
),
|
|
323
344
|
...SQLITE_ADDED_COLUMNS.filter(([table, name]) => !has(table, name)).map(
|
|
324
345
|
([table, name, definition]) =>
|
|
325
346
|
`ALTER TABLE ${table} ADD COLUMN ${name} ${definition}`,
|
|
@@ -605,6 +626,20 @@ export abstract class SqliteFamilyStorage {
|
|
|
605
626
|
return row?.horizon_seq ?? 0;
|
|
606
627
|
}
|
|
607
628
|
|
|
629
|
+
async getHorizons(partition: string): Promise<PartitionHorizons> {
|
|
630
|
+
const [row] = await this.all<{ seq: number; tables: string }>(
|
|
631
|
+
`SELECT coalesce((SELECT horizon_seq FROM sync_partitions WHERE partition=?),0) AS seq,
|
|
632
|
+
(SELECT json_group_object(tbl, commit_seq) FROM sync_table_horizons WHERE partition=?) AS tables`,
|
|
633
|
+
[partition, partition],
|
|
634
|
+
);
|
|
635
|
+
return {
|
|
636
|
+
seq: row?.seq ?? 0,
|
|
637
|
+
tables: new Map(
|
|
638
|
+
Object.entries(asJson<Record<string, number>>(row?.tables ?? '{}')),
|
|
639
|
+
),
|
|
640
|
+
};
|
|
641
|
+
}
|
|
642
|
+
|
|
608
643
|
async setHorizonSeq(partition: string, seq: number): Promise<void> {
|
|
609
644
|
await this.all(
|
|
610
645
|
`INSERT INTO sync_partitions(partition, horizon_seq) VALUES (?,?)
|
|
@@ -649,8 +684,14 @@ export abstract class SqliteFamilyStorage {
|
|
|
649
684
|
AND ${epochGuard}`,
|
|
650
685
|
params: [partition, partition, partition, query.logEpoch],
|
|
651
686
|
},
|
|
652
|
-
// §4.6: tombstones prune in the same pass as the
|
|
653
|
-
|
|
687
|
+
// §4.6: tombstones and table horizons prune in the same pass as the
|
|
688
|
+
// commit log.
|
|
689
|
+
...[
|
|
690
|
+
'sync_commits',
|
|
691
|
+
'sync_changes',
|
|
692
|
+
'sync_tombstones',
|
|
693
|
+
'sync_table_horizons',
|
|
694
|
+
].map((table) => ({
|
|
654
695
|
sql: `DELETE FROM ${table} WHERE partition=? AND commit_seq<=${horizon} AND ${epochGuard}`,
|
|
655
696
|
params: [partition, partition, partition, query.logEpoch],
|
|
656
697
|
})),
|
package/src/storage.ts
CHANGED
|
@@ -792,6 +792,17 @@ export function toClientRecord(record: ClientRowRecord): ClientRecord {
|
|
|
792
792
|
};
|
|
793
793
|
}
|
|
794
794
|
|
|
795
|
+
/** A partition's pruning horizon and its table horizons (§4.6). */
|
|
796
|
+
export interface PartitionHorizons {
|
|
797
|
+
/** `horizonSeq`. */
|
|
798
|
+
readonly seq: number;
|
|
799
|
+
/**
|
|
800
|
+
* Re-keyed table → the commit sequence number its re-key took. The changes
|
|
801
|
+
* of a table at or below it are never served.
|
|
802
|
+
*/
|
|
803
|
+
readonly tables: ReadonlyMap<string, number>;
|
|
804
|
+
}
|
|
805
|
+
|
|
795
806
|
/** One unit of the §4.5 scan set. */
|
|
796
807
|
export interface LiveScanUnit {
|
|
797
808
|
/** The unit's scope map; its first variable (keys sorted) is its index key. */
|
|
@@ -1313,6 +1324,8 @@ export interface ServerStorage {
|
|
|
1313
1324
|
|
|
1314
1325
|
getMaxCommitSeq(partition: string): Promise<number>;
|
|
1315
1326
|
getHorizonSeq(partition: string): Promise<number>;
|
|
1327
|
+
/** `horizonSeq` and the table horizons (§4.6) in one read. */
|
|
1328
|
+
getHorizons(partition: string): Promise<PartitionHorizons>;
|
|
1316
1329
|
/** Monotonic within the current epoch; use atomic pruning for maintenance. */
|
|
1317
1330
|
setHorizonSeq(partition: string, seq: number): Promise<void>;
|
|
1318
1331
|
/**
|