@syncular/server 0.92.0 → 0.94.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 +7 -2
- package/dist/context.d.ts +9 -1
- package/dist/d1-storage.js +10 -3
- package/dist/postgres-storage.js +4 -0
- package/dist/push.js +71 -4
- package/dist/relational-rows.d.ts +31 -2
- package/dist/relational-rows.js +57 -6
- package/dist/schema-window.js +8 -2
- package/dist/schema.d.ts +3 -0
- package/dist/schema.js +15 -0
- package/dist/sqlite-storage.js +14 -1
- package/dist/validate.d.ts +25 -0
- package/package.json +2 -2
- package/src/context.ts +13 -1
- package/src/d1-storage.ts +20 -3
- package/src/postgres-storage.ts +6 -0
- package/src/push.ts +132 -2
- package/src/relational-rows.ts +83 -7
- package/src/schema-window.ts +13 -2
- package/src/schema.ts +25 -0
- package/src/sqlite-storage.ts +23 -0
- package/src/validate.ts +32 -0
package/README.md
CHANGED
|
@@ -194,7 +194,10 @@ UI. Unknown members, free-form tokens, malformed paths, and over-limit data
|
|
|
194
194
|
fail at construction. Diagnostic prose stays in `message`; apps should map
|
|
195
195
|
the stable code/details to localized copy instead of displaying that message.
|
|
196
196
|
|
|
197
|
-
Validators are per-operation. A validator must not mutate the row it receives
|
|
197
|
+
Validators are per-operation. A validator must not mutate the row it receives;
|
|
198
|
+
a value only the server decides belongs in a server-owned column, which a
|
|
199
|
+
`serverColumns` hook sets before the validator runs
|
|
200
|
+
([Server-owned columns](https://syncular.dev/guide-server/#server-owned-columns)).
|
|
198
201
|
|
|
199
202
|
A validator that decides from other rows (a membership, a role, a grant) calls
|
|
200
203
|
`context.queryAuthoritative` with the request `storage.queryAuthoritative`
|
|
@@ -1420,6 +1423,8 @@ Pass `schemaWindow: [compileSchema(schema), compileSchema(previousSchema)]` to
|
|
|
1420
1423
|
serve the prior codec through the current storage and validation rules. Entries
|
|
1421
1424
|
are newest first, with the compiled current schema first. Added tables and
|
|
1422
1425
|
appended nullable columns are supported. Removed/renamed columns, changed codecs,
|
|
1423
|
-
primary keys, references or scopes reject the window before serving
|
|
1426
|
+
primary keys, references or scopes reject the window before serving; a
|
|
1427
|
+
required column relaxed to nullable is a changed column, so the window cannot
|
|
1428
|
+
hold the version before the relaxation (SPEC §2.4). Hosts must
|
|
1424
1429
|
exclude semantically incompatible prior versions. The [schema upgrade guide](https://syncular.dev/concepts-schema-upgrades/#serving-a-compatibility-window)
|
|
1425
1430
|
owns the pull, segment, push and realtime contract.
|
package/dist/context.d.ts
CHANGED
|
@@ -16,7 +16,7 @@ import type { SegmentStore } from './segment-store.js';
|
|
|
16
16
|
import type { BlobPresignConfig, BlobUploadPresignConfig, SegmentUrlConfig } from './signed-url.js';
|
|
17
17
|
import type { SqliteImageBuilder } from './sqlite-image.js';
|
|
18
18
|
import type { CheckpointDeclaration, PartitionTouch, ServerStorage, StoredCommit } from './storage.js';
|
|
19
|
-
import type { CommitValidator, ValidatorRegistry } from './validate.js';
|
|
19
|
+
import type { CommitValidator, ServerColumnsRegistry, ValidatorRegistry } from './validate.js';
|
|
20
20
|
/** SSP2 body content type (§1.1). */
|
|
21
21
|
export declare const SSP2_CONTENT_TYPE = "application/vnd.syncular.sync.v2";
|
|
22
22
|
/** Internal idempotency namespace. Ordinary SSP2 client IDs cannot use it. */
|
|
@@ -154,6 +154,14 @@ export interface SyncServerConfig {
|
|
|
154
154
|
* process, never on the wire.
|
|
155
155
|
*/
|
|
156
156
|
readonly validators?: ValidatorRegistry;
|
|
157
|
+
/**
|
|
158
|
+
* §6.12 per-table server column hooks (`table` → hook). Each accepted
|
|
159
|
+
* upsert on a listed table runs its hook inside the push transaction,
|
|
160
|
+
* after §3.4 authorization and before §6.7 validation; the returned values
|
|
161
|
+
* of the table's server-owned columns are stored and fanned out in the
|
|
162
|
+
* same commit.
|
|
163
|
+
*/
|
|
164
|
+
readonly serverColumns?: ServerColumnsRegistry;
|
|
157
165
|
/**
|
|
158
166
|
* §6.8 whole-commit validator. When present, the storage serializes this
|
|
159
167
|
* partition before operation reads/writes, then invokes the callback once
|
package/dist/d1-storage.js
CHANGED
|
@@ -42,7 +42,7 @@ var _a;
|
|
|
42
42
|
* rather than a lock D1 does not expose.
|
|
43
43
|
*/
|
|
44
44
|
import { decodeRow } from '@syncular/core';
|
|
45
|
-
import { assertAppendOnlyMigration, assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, changedScopePatterns, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, rekeyCleanupSql, rekeyedScopes, rekeyRowScopesSql, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, scanProbeSql, scanRowPageSql, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowSql, selectRowsSql, selectRowsForRewriteSql, tableColumnNames, toSqlValue, } from './relational-rows.js';
|
|
45
|
+
import { assertAppendOnlyMigration, assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, changedScopePatterns, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, rekeyCleanupSql, rekeyedScopes, rekeyRowScopesSql, retiredTableNames, rewritePlan, SQLITE_TABLE_REBUILD_SQL, sqliteRelaxRebuildDdl, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, scanProbeSql, scanRowPageSql, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowSql, selectRowsSql, selectRowsForRewriteSql, tableColumnNames, toSqlValue, } from './relational-rows.js';
|
|
46
46
|
import { matchesEffective } from './scopes.js';
|
|
47
47
|
import { deserializePushResult, placeholders, SqliteFamilyStorage, SqliteFamilyTransaction, sqliteDdlStatements, SQLITE_UPGRADE_PROBE, sqliteUpgradeDdl, } from './sqlite-dialect.js';
|
|
48
48
|
import { asUint8Array, commitDeleteFloor, toStoredRow, toTombstone, } from './storage.js';
|
|
@@ -904,18 +904,23 @@ export class D1ServerStorage extends SqliteFamilyStorage {
|
|
|
904
904
|
}
|
|
905
905
|
statementsExecuted += 2;
|
|
906
906
|
const columns = await this.#db
|
|
907
|
-
.prepare(
|
|
907
|
+
.prepare(SQLITE_TABLE_REBUILD_SQL)
|
|
908
|
+
.bind(table.name)
|
|
908
909
|
.all();
|
|
909
910
|
const indexes = await this.#db
|
|
910
911
|
.prepare(SQLITE_TABLE_INDEXES_SQL)
|
|
911
912
|
.bind(table.name)
|
|
912
913
|
.all();
|
|
913
914
|
// Validate the old codec before any application DDL or row rewrite.
|
|
914
|
-
rewritePlan(table, sourceLayouts[table.name], columns.results.length > 0
|
|
915
|
+
const { relaxed } = rewritePlan(table, sourceLayouts[table.name], columns.results.length > 0
|
|
915
916
|
? new Set(columns.results.map((column) => column.name))
|
|
916
917
|
: undefined);
|
|
917
918
|
if (!(await save({
|
|
918
919
|
...state,
|
|
920
|
+
rebuilds: [
|
|
921
|
+
...(state.rebuilds ?? []),
|
|
922
|
+
...sqliteRelaxRebuildDdl(table.name, columns.results, relaxed),
|
|
923
|
+
],
|
|
919
924
|
offset: state.offset + 1,
|
|
920
925
|
afterPartition: '',
|
|
921
926
|
afterRowId: '',
|
|
@@ -964,6 +969,7 @@ export class D1ServerStorage extends SqliteFamilyStorage {
|
|
|
964
969
|
`DELETE FROM sync_blob_refs WHERE tbl='${name.replaceAll("'", "''")}'`,
|
|
965
970
|
dropTableDdl(name),
|
|
966
971
|
]),
|
|
972
|
+
...(state.rebuilds ?? []),
|
|
967
973
|
...schemaDdl(schema, columns, 'sqlite', indexes),
|
|
968
974
|
...rekeyCleanupSql(rekeyed),
|
|
969
975
|
];
|
|
@@ -972,6 +978,7 @@ export class D1ServerStorage extends SqliteFamilyStorage {
|
|
|
972
978
|
phase: 'ddl',
|
|
973
979
|
offset: 0,
|
|
974
980
|
ddl,
|
|
981
|
+
rebuilds: [],
|
|
975
982
|
rewrites,
|
|
976
983
|
columns: {},
|
|
977
984
|
indexes: {},
|
package/dist/postgres-storage.js
CHANGED
|
@@ -1751,6 +1751,10 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
1751
1751
|
for (const table of schema.tables.values()) {
|
|
1752
1752
|
const oldLayout = layouts[table.name];
|
|
1753
1753
|
const plan = rewritePlan(table, oldLayout, existing.get(table.name));
|
|
1754
|
+
// SPEC §2.4: payload bytes are unchanged; only the constraint goes.
|
|
1755
|
+
for (const column of plan.relaxed) {
|
|
1756
|
+
await client.query(`ALTER TABLE ${quoteIdent(table.name)} ALTER COLUMN ${quoteIdent(column)} DROP NOT NULL`);
|
|
1757
|
+
}
|
|
1754
1758
|
const rekey = rekeyed.includes(table.name);
|
|
1755
1759
|
if (!plan.migrate && !plan.backfill && !rekey)
|
|
1756
1760
|
continue;
|
package/dist/push.js
CHANGED
|
@@ -133,6 +133,56 @@ async function runValidator(validators, table, op, rowId, values, storedValues,
|
|
|
133
133
|
}
|
|
134
134
|
return undefined;
|
|
135
135
|
}
|
|
136
|
+
/**
|
|
137
|
+
* §6.12: complete an accepted upsert with the table's server column hook.
|
|
138
|
+
* The returned values land in `values` and are marked present, so their
|
|
139
|
+
* column versions advance. A `ValidationRejection` keeps its host code; any
|
|
140
|
+
* other failure (an undeclared key, a value the column's type refuses, a
|
|
141
|
+
* changed scope column on an update) is `sync.constraint_violation`.
|
|
142
|
+
*/
|
|
143
|
+
async function runServerColumns(hooks, tx, schema, table, rowId, values, storedValues, present, opIndex, partition, actorId, context) {
|
|
144
|
+
const hook = hooks?.[table.name];
|
|
145
|
+
if (hook === undefined)
|
|
146
|
+
return undefined;
|
|
147
|
+
try {
|
|
148
|
+
const output = await hook({
|
|
149
|
+
table: table.name,
|
|
150
|
+
rowId,
|
|
151
|
+
row: toValidateRow(table.columns, values),
|
|
152
|
+
stored: storedValues !== undefined
|
|
153
|
+
? toValidateRow(table.columns, storedValues)
|
|
154
|
+
: undefined,
|
|
155
|
+
}, {
|
|
156
|
+
actorId,
|
|
157
|
+
partition,
|
|
158
|
+
...(context !== undefined ? { context } : {}),
|
|
159
|
+
read: commitValidationReader(tx, schema),
|
|
160
|
+
});
|
|
161
|
+
for (const [name, value] of Object.entries(output)) {
|
|
162
|
+
const index = table.columnIndex.get(name);
|
|
163
|
+
if (index === undefined ||
|
|
164
|
+
!table.serverOwnedColumnIndices.includes(index)) {
|
|
165
|
+
throw new Error('server column hook returned a column that is not server-owned');
|
|
166
|
+
}
|
|
167
|
+
// The column's own codec refuses a value of the wrong type.
|
|
168
|
+
encodeRow([table.columns[index]], [value]);
|
|
169
|
+
if (storedValues !== undefined &&
|
|
170
|
+
table.scopePatterns.some((pattern) => pattern.columnIndex === index) &&
|
|
171
|
+
value !== (storedValues[index] ?? null)) {
|
|
172
|
+
throw new Error('server column hook changed a scope column on update');
|
|
173
|
+
}
|
|
174
|
+
values[index] = value;
|
|
175
|
+
present[index] = true;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
catch (error) {
|
|
179
|
+
if (error instanceof ValidationRejection) {
|
|
180
|
+
return errorRecord(opIndex, error.code, error.message, false, error.details);
|
|
181
|
+
}
|
|
182
|
+
return errorRecord(opIndex, 'sync.constraint_violation', 'server column hook failed');
|
|
183
|
+
}
|
|
184
|
+
return undefined;
|
|
185
|
+
}
|
|
136
186
|
/**
|
|
137
187
|
* §5.10.3: merge the row's `crdt` columns in place. For each crdt column,
|
|
138
188
|
* replace the incoming value with `merge(stored, incoming)` (§5.10.2) —
|
|
@@ -175,7 +225,7 @@ async function mergeCrdtColumns(table, values, storedValues, present, opIndex, m
|
|
|
175
225
|
}
|
|
176
226
|
return { changed };
|
|
177
227
|
}
|
|
178
|
-
async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, actorId, context,
|
|
228
|
+
async function applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, serverColumns, partition, actorId, context,
|
|
179
229
|
/** §2.2: the partition's delete floor, raised by this commit's deletes. */
|
|
180
230
|
deleteFloor) {
|
|
181
231
|
const table = schema.tables.get(op.table);
|
|
@@ -410,6 +460,11 @@ deleteFloor) {
|
|
|
410
460
|
catch (error) {
|
|
411
461
|
return errorRecord(opIndex, 'sync.invalid_request', `row payload failed row-codec decode (§1.7): ${error instanceof Error ? error.message : String(error)}`);
|
|
412
462
|
}
|
|
463
|
+
// §6.12: the server discards a pushed value of a server-owned column, so an
|
|
464
|
+
// older client that still writes one stays valid.
|
|
465
|
+
for (const index of table.serverOwnedColumnIndices) {
|
|
466
|
+
sparse[index] = stored === undefined ? null : undefined;
|
|
467
|
+
}
|
|
413
468
|
const pkValue = renderScopeValue(sparse[table.primaryKeyIndex] ?? null);
|
|
414
469
|
if (pkValue !== op.rowId) {
|
|
415
470
|
return errorRecord(opIndex, 'sync.invalid_request', 'payload primary key does not match rowId');
|
|
@@ -483,6 +538,9 @@ deleteFloor) {
|
|
|
483
538
|
const mergeOutcome = await mergeCrdtColumns(table, values, storedValues, present, opIndex, crdtMergers);
|
|
484
539
|
if ('kind' in mergeOutcome)
|
|
485
540
|
return mergeOutcome;
|
|
541
|
+
const serverReject = await runServerColumns(serverColumns, tx, schema, table, op.rowId, values, storedValues, present, opIndex, partition, actorId, context);
|
|
542
|
+
if (serverReject !== undefined)
|
|
543
|
+
return serverReject;
|
|
486
544
|
const newVersion = stored.serverVersion + 1;
|
|
487
545
|
const nextVersions = new Map();
|
|
488
546
|
for (let i = 0; i < table.columns.length; i++) {
|
|
@@ -598,6 +656,15 @@ deleteFloor) {
|
|
|
598
656
|
const insertMerge = await mergeCrdtColumns(table, values, undefined, present, opIndex, crdtMergers);
|
|
599
657
|
if ('kind' in insertMerge)
|
|
600
658
|
return insertMerge;
|
|
659
|
+
const serverReject = await runServerColumns(serverColumns, tx, schema, table, op.rowId, values, undefined, present, opIndex, partition, actorId, context);
|
|
660
|
+
if (serverReject !== undefined)
|
|
661
|
+
return serverReject;
|
|
662
|
+
// §6.12: the stored scopes and the change-scope index take the hook's
|
|
663
|
+
// values, so the row reaches a server-set scope's units in this commit.
|
|
664
|
+
const scoped = storedScopesForRow(table, values);
|
|
665
|
+
if ('missing' in scoped) {
|
|
666
|
+
return errorRecord(opIndex, 'sync.constraint_violation', 'server column hook failed');
|
|
667
|
+
}
|
|
601
668
|
// The stored payload is the full-row codec (§2.4); every column is
|
|
602
669
|
// present on an insert.
|
|
603
670
|
const insertPayload = encodeRow(table.columns, values);
|
|
@@ -620,7 +687,7 @@ deleteFloor) {
|
|
|
620
687
|
const newRow = {
|
|
621
688
|
rowId: op.rowId,
|
|
622
689
|
serverVersion,
|
|
623
|
-
scopes:
|
|
690
|
+
scopes: scoped.scopes,
|
|
624
691
|
payload: insertPayload,
|
|
625
692
|
};
|
|
626
693
|
await tx.upsertRow(op.table, newRow, { opIndex });
|
|
@@ -632,7 +699,7 @@ deleteFloor) {
|
|
|
632
699
|
rowId: op.rowId,
|
|
633
700
|
op: 'upsert',
|
|
634
701
|
rowVersion: serverVersion,
|
|
635
|
-
scopes:
|
|
702
|
+
scopes: scoped.scopes,
|
|
636
703
|
payload: insertPayload,
|
|
637
704
|
},
|
|
638
705
|
],
|
|
@@ -1401,7 +1468,7 @@ async function applyPushCommits(ctx, schema, resolved, clientId, commits, wireSc
|
|
|
1401
1468
|
const op = operations[opIndex];
|
|
1402
1469
|
if (op === undefined)
|
|
1403
1470
|
continue;
|
|
1404
|
-
const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, partition, ctx.actorId, ctx.context, deleteFloor);
|
|
1471
|
+
const outcome = await applyOperation(tx, schema, wireSchema, resolved, op, opIndex, blobCtx, crdtMergers, validators, ctx.serverColumns, partition, ctx.actorId, ctx.context, deleteFloor);
|
|
1405
1472
|
if (outcome.kind === 'terminate') {
|
|
1406
1473
|
terminated = outcome.record;
|
|
1407
1474
|
break;
|
|
@@ -374,8 +374,9 @@ export declare function retiredTableNames(schema: CompiledSchema, storedLayouts:
|
|
|
374
374
|
export declare function dropTableDdl(tableName: string): string;
|
|
375
375
|
/**
|
|
376
376
|
* Enforce the migration subset on a table's column list: the old layout
|
|
377
|
-
* must be
|
|
378
|
-
*
|
|
377
|
+
* must be a prefix (same name and type) of the new one, a column may change
|
|
378
|
+
* only from required to nullable (SPEC §2.4, relaxing a required column), and
|
|
379
|
+
* appended columns must be nullable (there is no default to backfill).
|
|
379
380
|
* Returns `true` iff columns were appended.
|
|
380
381
|
*/
|
|
381
382
|
export declare function assertAppendOnlyMigration(tableName: string, oldLayout: readonly StoredColumnLayout[], table: CompiledTable): boolean;
|
|
@@ -418,11 +419,39 @@ export declare function rewriteValues(table: CompiledTable, partition: string, r
|
|
|
418
419
|
* - `backfill`: the table just gained physical projection columns whose
|
|
419
420
|
* values exist in stored payloads (materialization flipped on) — the
|
|
420
421
|
* projection refreshes from the payload, no re-encode.
|
|
422
|
+
* - `relaxed`: existing projection columns the bump turns from required to
|
|
423
|
+
* nullable (SPEC §2.4). Their payload bytes are unchanged; only the
|
|
424
|
+
* physical `NOT NULL` goes.
|
|
421
425
|
*/
|
|
422
426
|
export declare function rewritePlan(table: CompiledTable, oldLayout: readonly StoredColumnLayout[] | undefined, physicalColumnsBefore: ReadonlySet<string> | undefined): {
|
|
423
427
|
migrate: boolean;
|
|
424
428
|
backfill: boolean;
|
|
429
|
+
relaxed: string[];
|
|
425
430
|
};
|
|
431
|
+
/**
|
|
432
|
+
* One physical column of a SQLite table plus the SQL of every index and
|
|
433
|
+
* trigger on it, read by {@link SQLITE_TABLE_REBUILD_SQL}.
|
|
434
|
+
*/
|
|
435
|
+
export interface SqliteRebuildColumnRow {
|
|
436
|
+
readonly name: string;
|
|
437
|
+
readonly type: string;
|
|
438
|
+
readonly not_null: number;
|
|
439
|
+
readonly dflt_value: string | null;
|
|
440
|
+
readonly pk: number;
|
|
441
|
+
/** JSON array of the table's index and trigger SQL (same on every row). */
|
|
442
|
+
readonly dependents: string;
|
|
443
|
+
}
|
|
444
|
+
/** Binds the table name once (`?1`); one statement, so D1 spends one query. */
|
|
445
|
+
export declare const SQLITE_TABLE_REBUILD_SQL = "SELECT name, type, \"notnull\" AS not_null, dflt_value, pk,\n (SELECT json_group_array(sql) FROM sqlite_master\n WHERE tbl_name=?1 AND type IN ('index', 'trigger') AND sql IS NOT NULL) AS dependents\n FROM pragma_table_info(?1) ORDER BY cid";
|
|
446
|
+
/**
|
|
447
|
+
* SQLite has no `ALTER COLUMN … DROP NOT NULL` (SPEC §2.4, relaxing a
|
|
448
|
+
* required column): rebuild the table as a copy that keeps every physical
|
|
449
|
+
* column declaration except `NOT NULL` on `relaxed`, copy every row, swap
|
|
450
|
+
* it in, and recreate the indexes and triggers the original carried
|
|
451
|
+
* (operator-added ones included). Rows, payloads and stored scopes are copied
|
|
452
|
+
* verbatim. Empty when no relaxed column is physically `NOT NULL`.
|
|
453
|
+
*/
|
|
454
|
+
export declare function sqliteRelaxRebuildDdl(tableName: string, columns: readonly SqliteRebuildColumnRow[], relaxed: readonly string[]): string[];
|
|
426
455
|
/**
|
|
427
456
|
* Every DDL statement to bring a database from the introspected relational
|
|
428
457
|
* projections to `schema`: CREATE TABLE for absent tables, ADD COLUMN for
|
package/dist/relational-rows.js
CHANGED
|
@@ -897,8 +897,9 @@ export function dropTableDdl(tableName) {
|
|
|
897
897
|
}
|
|
898
898
|
/**
|
|
899
899
|
* Enforce the migration subset on a table's column list: the old layout
|
|
900
|
-
* must be
|
|
901
|
-
*
|
|
900
|
+
* must be a prefix (same name and type) of the new one, a column may change
|
|
901
|
+
* only from required to nullable (SPEC §2.4, relaxing a required column), and
|
|
902
|
+
* appended columns must be nullable (there is no default to backfill).
|
|
902
903
|
* Returns `true` iff columns were appended.
|
|
903
904
|
*/
|
|
904
905
|
export function assertAppendOnlyMigration(tableName, oldLayout, table) {
|
|
@@ -912,8 +913,8 @@ export function assertAppendOnlyMigration(tableName, oldLayout, table) {
|
|
|
912
913
|
after === undefined ||
|
|
913
914
|
before.name !== after.name ||
|
|
914
915
|
before.type !== after.type ||
|
|
915
|
-
before.nullable
|
|
916
|
-
throw new Error(`table ${JSON.stringify(tableName)}: column ${i} changed (${JSON.stringify(before)} → ${JSON.stringify({ name: after?.name, type: after?.type, nullable: after?.nullable })}) — only appending nullable columns
|
|
916
|
+
(before.nullable && !after.nullable)) {
|
|
917
|
+
throw new Error(`table ${JSON.stringify(tableName)}: column ${i} changed (${JSON.stringify(before)} → ${JSON.stringify({ name: after?.name, type: after?.type, nullable: after?.nullable })}) — only appending nullable columns and relaxing a required column are supported`);
|
|
917
918
|
}
|
|
918
919
|
}
|
|
919
920
|
for (let i = oldLayout.length; i < table.columns.length; i++) {
|
|
@@ -1009,18 +1010,68 @@ export function rewriteValues(table, partition, rowId, payload, dialect, scopes)
|
|
|
1009
1010
|
* - `backfill`: the table just gained physical projection columns whose
|
|
1010
1011
|
* values exist in stored payloads (materialization flipped on) — the
|
|
1011
1012
|
* projection refreshes from the payload, no re-encode.
|
|
1013
|
+
* - `relaxed`: existing projection columns the bump turns from required to
|
|
1014
|
+
* nullable (SPEC §2.4). Their payload bytes are unchanged; only the
|
|
1015
|
+
* physical `NOT NULL` goes.
|
|
1012
1016
|
*/
|
|
1013
1017
|
export function rewritePlan(table, oldLayout, physicalColumnsBefore) {
|
|
1014
1018
|
if (oldLayout === undefined || physicalColumnsBefore === undefined) {
|
|
1015
1019
|
// Fresh table (or a pre-layout database, which cannot be reasoned
|
|
1016
1020
|
// about): nothing stored to rewrite.
|
|
1017
|
-
return { migrate: false, backfill: false };
|
|
1021
|
+
return { migrate: false, backfill: false, relaxed: [] };
|
|
1018
1022
|
}
|
|
1019
1023
|
const migrate = assertAppendOnlyMigration(table.name, oldLayout, table);
|
|
1020
1024
|
const backfill = table.materialize &&
|
|
1021
1025
|
table.columns.some((column, index) => index < oldLayout.length && // value exists in old payloads
|
|
1022
1026
|
!physicalColumnsBefore.has(column.name));
|
|
1023
|
-
|
|
1027
|
+
const relaxed = table.materialize
|
|
1028
|
+
? table.columns
|
|
1029
|
+
.filter((column, index) => oldLayout[index]?.nullable === false &&
|
|
1030
|
+
column.nullable &&
|
|
1031
|
+
physicalColumnsBefore.has(column.name))
|
|
1032
|
+
.map((column) => column.name)
|
|
1033
|
+
: [];
|
|
1034
|
+
return { migrate, backfill, relaxed };
|
|
1035
|
+
}
|
|
1036
|
+
/** Binds the table name once (`?1`); one statement, so D1 spends one query. */
|
|
1037
|
+
export const SQLITE_TABLE_REBUILD_SQL = `SELECT name, type, "notnull" AS not_null, dflt_value, pk,
|
|
1038
|
+
(SELECT json_group_array(sql) FROM sqlite_master
|
|
1039
|
+
WHERE tbl_name=?1 AND type IN ('index', 'trigger') AND sql IS NOT NULL) AS dependents
|
|
1040
|
+
FROM pragma_table_info(?1) ORDER BY cid`;
|
|
1041
|
+
/**
|
|
1042
|
+
* SQLite has no `ALTER COLUMN … DROP NOT NULL` (SPEC §2.4, relaxing a
|
|
1043
|
+
* required column): rebuild the table as a copy that keeps every physical
|
|
1044
|
+
* column declaration except `NOT NULL` on `relaxed`, copy every row, swap
|
|
1045
|
+
* it in, and recreate the indexes and triggers the original carried
|
|
1046
|
+
* (operator-added ones included). Rows, payloads and stored scopes are copied
|
|
1047
|
+
* verbatim. Empty when no relaxed column is physically `NOT NULL`.
|
|
1048
|
+
*/
|
|
1049
|
+
export function sqliteRelaxRebuildDdl(tableName, columns, relaxed) {
|
|
1050
|
+
const relax = (column) => column.not_null === 1 && relaxed.includes(column.name);
|
|
1051
|
+
if (!columns.some(relax))
|
|
1052
|
+
return [];
|
|
1053
|
+
const target = quoteIdent(tableName);
|
|
1054
|
+
// The `sync_` prefix is reserved for server storage, so no app table
|
|
1055
|
+
// collides with the copy.
|
|
1056
|
+
const copy = quoteIdent(`sync_rebuild_${tableName}`);
|
|
1057
|
+
const names = columns.map((column) => quoteIdent(column.name)).join(', ');
|
|
1058
|
+
const definitions = columns.map((column) => [
|
|
1059
|
+
quoteIdent(column.name),
|
|
1060
|
+
...(column.type === '' ? [] : [column.type]),
|
|
1061
|
+
...(column.not_null === 1 && !relax(column) ? ['NOT NULL'] : []),
|
|
1062
|
+
...(column.dflt_value === null ? [] : [`DEFAULT ${column.dflt_value}`]),
|
|
1063
|
+
].join(' '));
|
|
1064
|
+
const primaryKey = columns
|
|
1065
|
+
.filter((column) => column.pk > 0)
|
|
1066
|
+
.sort((a, b) => a.pk - b.pk)
|
|
1067
|
+
.map((column) => quoteIdent(column.name));
|
|
1068
|
+
return [
|
|
1069
|
+
`CREATE TABLE ${copy} (${[...definitions, `PRIMARY KEY (${primaryKey.join(', ')})`].join(', ')})`,
|
|
1070
|
+
`INSERT INTO ${copy} (${names}) SELECT ${names} FROM ${target}`,
|
|
1071
|
+
`DROP TABLE ${target}`,
|
|
1072
|
+
`ALTER TABLE ${copy} RENAME TO ${target}`,
|
|
1073
|
+
...JSON.parse(columns[0]?.dependents ?? '[]'),
|
|
1074
|
+
];
|
|
1024
1075
|
}
|
|
1025
1076
|
/**
|
|
1026
1077
|
* Every DDL statement to bring a database from the introspected relational
|
package/dist/schema-window.js
CHANGED
|
@@ -19,13 +19,19 @@ export function schemaWindowOf(current, configured) {
|
|
|
19
19
|
const next = head.tables.get(table.name);
|
|
20
20
|
if (next === undefined ||
|
|
21
21
|
table.columns.length > next.columns.length ||
|
|
22
|
-
|
|
22
|
+
// §6.12: a version may mark an existing column server-owned; the flag
|
|
23
|
+
// never reaches the wire.
|
|
24
|
+
table.columns.some((column, i) => JSON.stringify({ ...column, serverOwned: undefined }) !==
|
|
25
|
+
JSON.stringify({ ...next.columns[i], serverOwned: undefined })) ||
|
|
23
26
|
next.columns
|
|
24
27
|
.slice(table.columns.length)
|
|
25
28
|
.some((column) => !column.nullable) ||
|
|
26
29
|
table.primaryKeyIndex !== next.primaryKeyIndex ||
|
|
30
|
+
// §6.12: a pattern over an appended server-owned column is new to the
|
|
31
|
+
// current version; an older client neither holds nor writes it.
|
|
27
32
|
JSON.stringify(table.scopePatterns) !==
|
|
28
|
-
JSON.stringify(next.scopePatterns) ||
|
|
33
|
+
JSON.stringify(next.scopePatterns.filter((pattern) => pattern.columnIndex < table.columns.length ||
|
|
34
|
+
!next.serverOwnedColumnIndices.includes(pattern.columnIndex))) ||
|
|
29
35
|
JSON.stringify(table.references) !== JSON.stringify(next.references)) {
|
|
30
36
|
throw new Error('sync.schema_window.incompatible_schema');
|
|
31
37
|
}
|
package/dist/schema.d.ts
CHANGED
|
@@ -141,6 +141,9 @@ export interface CompiledTable {
|
|
|
141
141
|
* image copies ciphertext wholesale with no per-row decrypt pass, so an
|
|
142
142
|
* encrypted table MUST be served via the rows lane. Empty ⇒ no restriction. */
|
|
143
143
|
readonly encryptedColumnIndices: readonly number[];
|
|
144
|
+
/** §6.12: column indices marked `serverOwned`. Empty ⇒ no server column
|
|
145
|
+
* hook output is accepted for the table. */
|
|
146
|
+
readonly serverOwnedColumnIndices: readonly number[];
|
|
144
147
|
}
|
|
145
148
|
export interface CompiledSchema {
|
|
146
149
|
readonly version: number;
|
package/dist/schema.js
CHANGED
|
@@ -154,6 +154,7 @@ export function compileSchema(schema) {
|
|
|
154
154
|
const blobRefColumnIndices = [];
|
|
155
155
|
const crdtColumns = [];
|
|
156
156
|
const encryptedColumnIndices = [];
|
|
157
|
+
const serverOwnedColumnIndices = [];
|
|
157
158
|
const references = [];
|
|
158
159
|
for (const reference of table.references ?? []) {
|
|
159
160
|
const index = columnIndex.get(reference.column);
|
|
@@ -173,6 +174,19 @@ export function compileSchema(schema) {
|
|
|
173
174
|
// §5.11: an encrypted column carries wire type `bytes` + `encrypted`.
|
|
174
175
|
if (column.encrypted === true)
|
|
175
176
|
encryptedColumnIndices.push(index);
|
|
177
|
+
// §6.12: the hook never changes who may write the row (owner set), and
|
|
178
|
+
// a client's optimistic row holds NULL until the server row arrives.
|
|
179
|
+
if (column.serverOwned === true) {
|
|
180
|
+
if (index === primaryKeyIndex ||
|
|
181
|
+
!column.nullable ||
|
|
182
|
+
column.type === 'crdt' ||
|
|
183
|
+
column.encrypted === true ||
|
|
184
|
+
scopePatterns.some((pattern) => pattern.columnIndex === index &&
|
|
185
|
+
writeScopeSets[0].includes(pattern.variable))) {
|
|
186
|
+
throw new Error(`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.12)`);
|
|
187
|
+
}
|
|
188
|
+
serverOwnedColumnIndices.push(index);
|
|
189
|
+
}
|
|
176
190
|
if (column.type === 'crdt') {
|
|
177
191
|
// §5.10.1: a crdt column MUST name a crdtType (schema-compile-time
|
|
178
192
|
// requirement — a crdt column without one is a server bug).
|
|
@@ -207,6 +221,7 @@ export function compileSchema(schema) {
|
|
|
207
221
|
blobRefColumnIndices,
|
|
208
222
|
crdtColumns,
|
|
209
223
|
encryptedColumnIndices,
|
|
224
|
+
serverOwnedColumnIndices,
|
|
210
225
|
});
|
|
211
226
|
}
|
|
212
227
|
// §6.11: resolve incoming references and enforce the declaration rules the
|
package/dist/sqlite-storage.js
CHANGED
|
@@ -13,7 +13,7 @@ import { decodeRow } from '@syncular/core';
|
|
|
13
13
|
import { isSqliteConstraintError, retainRollbackFailure, rollbackAfterError, StorageConstraintError, StorageQueryError, } from './storage-errors.js';
|
|
14
14
|
import { commitDeleteFloor, serveGateRefusal } from './storage.js';
|
|
15
15
|
import { serveNotReadyError } from './readiness.js';
|
|
16
|
-
import { assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, changedScopePatterns, dropTableDdl, layoutsOf, migratePayload, parseLayouts, rekeyCleanupSql, rekeyedScopes, rekeyRowScopesSql, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowsForRewriteSql, } from './relational-rows.js';
|
|
16
|
+
import { assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, changedScopePatterns, dropTableDdl, layoutsOf, migratePayload, parseLayouts, rekeyCleanupSql, rekeyedScopes, rekeyRowScopesSql, retiredTableNames, rewritePlan, SQLITE_TABLE_REBUILD_SQL, sqliteRelaxRebuildDdl, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowsForRewriteSql, } from './relational-rows.js';
|
|
17
17
|
import { SQLITE_DDL, SQLITE_UPGRADE_PROBE, SqliteFamilyStorage, SqliteFamilyTransaction, sqliteUpgradeDdl, } from './sqlite-dialect.js';
|
|
18
18
|
import { SqliteAdapterRequiredError, } from './sqlite-driver.js';
|
|
19
19
|
function toStoredCheckpoint(record) {
|
|
@@ -434,6 +434,19 @@ END`);
|
|
|
434
434
|
.run(tableName);
|
|
435
435
|
this.db.exec(dropTableDdl(tableName));
|
|
436
436
|
}
|
|
437
|
+
// A relaxed column loses its NOT NULL before the bump's other DDL
|
|
438
|
+
// (SPEC §2.4), so the index diff below sees the rebuilt table.
|
|
439
|
+
for (const table of schema.tables.values()) {
|
|
440
|
+
const { relaxed } = rewritePlan(table, layouts[table.name], existing.get(table.name));
|
|
441
|
+
if (relaxed.length === 0)
|
|
442
|
+
continue;
|
|
443
|
+
const columns = this.db
|
|
444
|
+
.query(SQLITE_TABLE_REBUILD_SQL)
|
|
445
|
+
.all(table.name);
|
|
446
|
+
for (const statement of sqliteRelaxRebuildDdl(table.name, columns, relaxed)) {
|
|
447
|
+
this.db.exec(statement);
|
|
448
|
+
}
|
|
449
|
+
}
|
|
437
450
|
for (const statement of schemaDdl(schema, existing, 'sqlite', existingIndexes)) {
|
|
438
451
|
this.db.exec(statement);
|
|
439
452
|
}
|
package/dist/validate.d.ts
CHANGED
|
@@ -222,3 +222,28 @@ export declare class CommitAuthorizationRejection extends SyncError {
|
|
|
222
222
|
}
|
|
223
223
|
/** Build the column-keyed row object a validator inspects (§6.7). */
|
|
224
224
|
export declare function toValidateRow(columns: readonly RowColumn[], values: readonly RowValue[]): ValidateRow;
|
|
225
|
+
/** §6.12: the accepted upsert a server column hook completes. `row` is the
|
|
226
|
+
* merged row (present columns over stored values, crdt columns merged) with
|
|
227
|
+
* every server-owned column as stored, or NULL on an insert. */
|
|
228
|
+
export interface ServerColumnsInput {
|
|
229
|
+
readonly table: string;
|
|
230
|
+
readonly rowId: string;
|
|
231
|
+
readonly row: ValidateRow;
|
|
232
|
+
readonly stored: ValidateRow | undefined;
|
|
233
|
+
}
|
|
234
|
+
export interface ServerColumnsContext {
|
|
235
|
+
readonly actorId: string;
|
|
236
|
+
readonly partition: string;
|
|
237
|
+
readonly context?: string;
|
|
238
|
+
/** The §6.8 candidate-state reader of the push transaction. */
|
|
239
|
+
readonly read: CommitValidationReader;
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* §6.12: returns values for the table's server-owned columns; a column it
|
|
243
|
+
* omits keeps its value in `row`. A key that names no server-owned column, a
|
|
244
|
+
* value the column's type refuses, or a changed scope column on an update
|
|
245
|
+
* rejects the commit with `sync.constraint_violation`, like any other throw
|
|
246
|
+
* that is not a {@link ValidationRejection}.
|
|
247
|
+
*/
|
|
248
|
+
export type ServerColumnsHook = (input: ServerColumnsInput, ctx: ServerColumnsContext) => ValidateRow | Promise<ValidateRow>;
|
|
249
|
+
export type ServerColumnsRegistry = Readonly<Record<string, ServerColumnsHook>>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/server",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.94.0",
|
|
4
4
|
"description": "Syncular server: handleSyncRequest + storage/auth interfaces for the sync protocol",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -68,7 +68,7 @@
|
|
|
68
68
|
"verify:node": "cd ../.. && bun build ./packages/server/test/sqlite-runtime/verify-node.mjs --target=node --conditions=bun --outfile=./packages/server/.verify-node.built.mjs && node ./packages/server/.verify-node.built.mjs"
|
|
69
69
|
},
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"@syncular/core": "0.
|
|
71
|
+
"@syncular/core": "0.94.0"
|
|
72
72
|
},
|
|
73
73
|
"devDependencies": {
|
|
74
74
|
"@electric-sql/pglite": "^0.5.4"
|
package/src/context.ts
CHANGED
|
@@ -25,7 +25,11 @@ import type {
|
|
|
25
25
|
ServerStorage,
|
|
26
26
|
StoredCommit,
|
|
27
27
|
} from './storage';
|
|
28
|
-
import type {
|
|
28
|
+
import type {
|
|
29
|
+
CommitValidator,
|
|
30
|
+
ServerColumnsRegistry,
|
|
31
|
+
ValidatorRegistry,
|
|
32
|
+
} from './validate';
|
|
29
33
|
|
|
30
34
|
/** SSP2 body content type (§1.1). */
|
|
31
35
|
export const SSP2_CONTENT_TYPE = 'application/vnd.syncular.sync.v2';
|
|
@@ -192,6 +196,14 @@ export interface SyncServerConfig {
|
|
|
192
196
|
* process, never on the wire.
|
|
193
197
|
*/
|
|
194
198
|
readonly validators?: ValidatorRegistry;
|
|
199
|
+
/**
|
|
200
|
+
* §6.12 per-table server column hooks (`table` → hook). Each accepted
|
|
201
|
+
* upsert on a listed table runs its hook inside the push transaction,
|
|
202
|
+
* after §3.4 authorization and before §6.7 validation; the returned values
|
|
203
|
+
* of the table's server-owned columns are stored and fanned out in the
|
|
204
|
+
* same commit.
|
|
205
|
+
*/
|
|
206
|
+
readonly serverColumns?: ServerColumnsRegistry;
|
|
195
207
|
/**
|
|
196
208
|
* §6.8 whole-commit validator. When present, the storage serializes this
|
|
197
209
|
* partition before operation reads/writes, then invokes the callback once
|
package/src/d1-storage.ts
CHANGED
|
@@ -59,6 +59,9 @@ import {
|
|
|
59
59
|
rekeyRowScopesSql,
|
|
60
60
|
retiredTableNames,
|
|
61
61
|
rewritePlan,
|
|
62
|
+
SQLITE_TABLE_REBUILD_SQL,
|
|
63
|
+
type SqliteRebuildColumnRow,
|
|
64
|
+
sqliteRelaxRebuildDdl,
|
|
62
65
|
rewriteRowSql,
|
|
63
66
|
rewriteValues,
|
|
64
67
|
SCHEMA_META_DDL_SQLITE,
|
|
@@ -870,6 +873,9 @@ interface D1MigrationState {
|
|
|
870
873
|
columns: Record<string, string[]>;
|
|
871
874
|
indexes: Record<string, Record<string, string>>;
|
|
872
875
|
ddl: string[];
|
|
876
|
+
/** Rebuilds of tables with a relaxed column (SPEC §2.4), run before the
|
|
877
|
+
* other DDL. */
|
|
878
|
+
rebuilds?: string[];
|
|
873
879
|
rewrites: Array<{
|
|
874
880
|
table: string;
|
|
875
881
|
oldLayout?: readonly StoredColumnLayout[];
|
|
@@ -1350,14 +1356,15 @@ export class D1ServerStorage
|
|
|
1350
1356
|
}
|
|
1351
1357
|
statementsExecuted += 2;
|
|
1352
1358
|
const columns = await this.#db
|
|
1353
|
-
.prepare(
|
|
1354
|
-
.
|
|
1359
|
+
.prepare(SQLITE_TABLE_REBUILD_SQL)
|
|
1360
|
+
.bind(table.name)
|
|
1361
|
+
.all<SqliteRebuildColumnRow>();
|
|
1355
1362
|
const indexes = await this.#db
|
|
1356
1363
|
.prepare(SQLITE_TABLE_INDEXES_SQL)
|
|
1357
1364
|
.bind(table.name)
|
|
1358
1365
|
.all<SqliteTableIndexRow>();
|
|
1359
1366
|
// Validate the old codec before any application DDL or row rewrite.
|
|
1360
|
-
rewritePlan(
|
|
1367
|
+
const { relaxed } = rewritePlan(
|
|
1361
1368
|
table,
|
|
1362
1369
|
sourceLayouts[table.name],
|
|
1363
1370
|
columns.results.length > 0
|
|
@@ -1368,6 +1375,14 @@ export class D1ServerStorage
|
|
|
1368
1375
|
!(await save(
|
|
1369
1376
|
{
|
|
1370
1377
|
...state,
|
|
1378
|
+
rebuilds: [
|
|
1379
|
+
...(state.rebuilds ?? []),
|
|
1380
|
+
...sqliteRelaxRebuildDdl(
|
|
1381
|
+
table.name,
|
|
1382
|
+
columns.results,
|
|
1383
|
+
relaxed,
|
|
1384
|
+
),
|
|
1385
|
+
],
|
|
1371
1386
|
offset: state.offset + 1,
|
|
1372
1387
|
afterPartition: '',
|
|
1373
1388
|
afterRowId: '',
|
|
@@ -1429,6 +1444,7 @@ export class D1ServerStorage
|
|
|
1429
1444
|
`DELETE FROM sync_blob_refs WHERE tbl='${name.replaceAll("'", "''")}'`,
|
|
1430
1445
|
dropTableDdl(name),
|
|
1431
1446
|
]),
|
|
1447
|
+
...(state.rebuilds ?? []),
|
|
1432
1448
|
...schemaDdl(schema, columns, 'sqlite', indexes),
|
|
1433
1449
|
...rekeyCleanupSql(rekeyed),
|
|
1434
1450
|
];
|
|
@@ -1439,6 +1455,7 @@ export class D1ServerStorage
|
|
|
1439
1455
|
phase: 'ddl',
|
|
1440
1456
|
offset: 0,
|
|
1441
1457
|
ddl,
|
|
1458
|
+
rebuilds: [],
|
|
1442
1459
|
rewrites,
|
|
1443
1460
|
columns: {},
|
|
1444
1461
|
indexes: {},
|
package/src/postgres-storage.ts
CHANGED
|
@@ -2516,6 +2516,12 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
|
|
|
2516
2516
|
for (const table of schema.tables.values()) {
|
|
2517
2517
|
const oldLayout = layouts[table.name];
|
|
2518
2518
|
const plan = rewritePlan(table, oldLayout, existing.get(table.name));
|
|
2519
|
+
// SPEC §2.4: payload bytes are unchanged; only the constraint goes.
|
|
2520
|
+
for (const column of plan.relaxed) {
|
|
2521
|
+
await client.query(
|
|
2522
|
+
`ALTER TABLE ${quoteIdent(table.name)} ALTER COLUMN ${quoteIdent(column)} DROP NOT NULL`,
|
|
2523
|
+
);
|
|
2524
|
+
}
|
|
2519
2525
|
const rekey = rekeyed.includes(table.name);
|
|
2520
2526
|
if (!plan.migrate && !plan.backfill && !rekey) continue;
|
|
2521
2527
|
await rewriteRowsOn(
|
package/src/push.ts
CHANGED
|
@@ -71,6 +71,7 @@ import type {
|
|
|
71
71
|
CommitValidationRow,
|
|
72
72
|
CommitValidator,
|
|
73
73
|
ValidateCommitOperation,
|
|
74
|
+
ServerColumnsRegistry,
|
|
74
75
|
ValidateOpKind,
|
|
75
76
|
ValidatorRegistry,
|
|
76
77
|
} from './validate';
|
|
@@ -288,6 +289,88 @@ async function runValidator(
|
|
|
288
289
|
return undefined;
|
|
289
290
|
}
|
|
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
|
+
{
|
|
327
|
+
actorId,
|
|
328
|
+
partition,
|
|
329
|
+
...(context !== undefined ? { context } : {}),
|
|
330
|
+
read: commitValidationReader(tx, schema),
|
|
331
|
+
},
|
|
332
|
+
);
|
|
333
|
+
for (const [name, value] of Object.entries(output)) {
|
|
334
|
+
const index = table.columnIndex.get(name);
|
|
335
|
+
if (
|
|
336
|
+
index === undefined ||
|
|
337
|
+
!table.serverOwnedColumnIndices.includes(index)
|
|
338
|
+
) {
|
|
339
|
+
throw new Error(
|
|
340
|
+
'server column hook returned a column that is not server-owned',
|
|
341
|
+
);
|
|
342
|
+
}
|
|
343
|
+
// The column's own codec refuses a value of the wrong type.
|
|
344
|
+
encodeRow([table.columns[index]!], [value]);
|
|
345
|
+
if (
|
|
346
|
+
storedValues !== undefined &&
|
|
347
|
+
table.scopePatterns.some((pattern) => pattern.columnIndex === index) &&
|
|
348
|
+
value !== (storedValues[index] ?? null)
|
|
349
|
+
) {
|
|
350
|
+
throw new Error('server column hook changed a scope column on update');
|
|
351
|
+
}
|
|
352
|
+
values[index] = value;
|
|
353
|
+
present[index] = true;
|
|
354
|
+
}
|
|
355
|
+
} catch (error) {
|
|
356
|
+
if (error instanceof ValidationRejection) {
|
|
357
|
+
return errorRecord(
|
|
358
|
+
opIndex,
|
|
359
|
+
error.code,
|
|
360
|
+
error.message,
|
|
361
|
+
false,
|
|
362
|
+
error.details,
|
|
363
|
+
);
|
|
364
|
+
}
|
|
365
|
+
return errorRecord(
|
|
366
|
+
opIndex,
|
|
367
|
+
'sync.constraint_violation',
|
|
368
|
+
'server column hook failed',
|
|
369
|
+
);
|
|
370
|
+
}
|
|
371
|
+
return undefined;
|
|
372
|
+
}
|
|
373
|
+
|
|
291
374
|
/**
|
|
292
375
|
* §5.10.3: merge the row's `crdt` columns in place. For each crdt column,
|
|
293
376
|
* replace the incoming value with `merge(stored, incoming)` (§5.10.2) —
|
|
@@ -352,6 +435,7 @@ async function applyOperation(
|
|
|
352
435
|
blobCtx: BlobApplyContext,
|
|
353
436
|
crdtMergers: CrdtMergerRegistry | undefined,
|
|
354
437
|
validators: ValidatorRegistry | undefined,
|
|
438
|
+
serverColumns: ServerColumnsRegistry | undefined,
|
|
355
439
|
partition: string,
|
|
356
440
|
actorId: string,
|
|
357
441
|
context: string | undefined,
|
|
@@ -699,6 +783,11 @@ async function applyOperation(
|
|
|
699
783
|
`row payload failed row-codec decode (§1.7): ${error instanceof Error ? error.message : String(error)}`,
|
|
700
784
|
);
|
|
701
785
|
}
|
|
786
|
+
// §6.12: the server discards a pushed value of a server-owned column, so an
|
|
787
|
+
// older client that still writes one stays valid.
|
|
788
|
+
for (const index of table.serverOwnedColumnIndices) {
|
|
789
|
+
sparse[index] = stored === undefined ? null : undefined;
|
|
790
|
+
}
|
|
702
791
|
const pkValue = renderScopeValue(sparse[table.primaryKeyIndex] ?? null);
|
|
703
792
|
if (pkValue !== op.rowId) {
|
|
704
793
|
return errorRecord(
|
|
@@ -808,6 +897,21 @@ async function applyOperation(
|
|
|
808
897
|
crdtMergers,
|
|
809
898
|
);
|
|
810
899
|
if ('kind' in mergeOutcome) return mergeOutcome;
|
|
900
|
+
const serverReject = await runServerColumns(
|
|
901
|
+
serverColumns,
|
|
902
|
+
tx,
|
|
903
|
+
schema,
|
|
904
|
+
table,
|
|
905
|
+
op.rowId,
|
|
906
|
+
values,
|
|
907
|
+
storedValues,
|
|
908
|
+
present,
|
|
909
|
+
opIndex,
|
|
910
|
+
partition,
|
|
911
|
+
actorId,
|
|
912
|
+
context,
|
|
913
|
+
);
|
|
914
|
+
if (serverReject !== undefined) return serverReject;
|
|
811
915
|
const newVersion = stored.serverVersion + 1;
|
|
812
916
|
const nextVersions = new Map<number, number>();
|
|
813
917
|
for (let i = 0; i < table.columns.length; i++) {
|
|
@@ -974,6 +1078,31 @@ async function applyOperation(
|
|
|
974
1078
|
crdtMergers,
|
|
975
1079
|
);
|
|
976
1080
|
if ('kind' in insertMerge) return insertMerge;
|
|
1081
|
+
const serverReject = await runServerColumns(
|
|
1082
|
+
serverColumns,
|
|
1083
|
+
tx,
|
|
1084
|
+
schema,
|
|
1085
|
+
table,
|
|
1086
|
+
op.rowId,
|
|
1087
|
+
values,
|
|
1088
|
+
undefined,
|
|
1089
|
+
present,
|
|
1090
|
+
opIndex,
|
|
1091
|
+
partition,
|
|
1092
|
+
actorId,
|
|
1093
|
+
context,
|
|
1094
|
+
);
|
|
1095
|
+
if (serverReject !== undefined) return serverReject;
|
|
1096
|
+
// §6.12: the stored scopes and the change-scope index take the hook's
|
|
1097
|
+
// values, so the row reaches a server-set scope's units in this commit.
|
|
1098
|
+
const scoped = storedScopesForRow(table, values);
|
|
1099
|
+
if ('missing' in scoped) {
|
|
1100
|
+
return errorRecord(
|
|
1101
|
+
opIndex,
|
|
1102
|
+
'sync.constraint_violation',
|
|
1103
|
+
'server column hook failed',
|
|
1104
|
+
);
|
|
1105
|
+
}
|
|
977
1106
|
// The stored payload is the full-row codec (§2.4); every column is
|
|
978
1107
|
// present on an insert.
|
|
979
1108
|
const insertPayload = encodeRow(table.columns, values);
|
|
@@ -1012,7 +1141,7 @@ async function applyOperation(
|
|
|
1012
1141
|
const newRow = {
|
|
1013
1142
|
rowId: op.rowId,
|
|
1014
1143
|
serverVersion,
|
|
1015
|
-
scopes:
|
|
1144
|
+
scopes: scoped.scopes,
|
|
1016
1145
|
payload: insertPayload,
|
|
1017
1146
|
};
|
|
1018
1147
|
await tx.upsertRow(op.table, newRow, { opIndex });
|
|
@@ -1024,7 +1153,7 @@ async function applyOperation(
|
|
|
1024
1153
|
rowId: op.rowId,
|
|
1025
1154
|
op: 'upsert',
|
|
1026
1155
|
rowVersion: serverVersion,
|
|
1027
|
-
scopes:
|
|
1156
|
+
scopes: scoped.scopes,
|
|
1028
1157
|
payload: insertPayload,
|
|
1029
1158
|
},
|
|
1030
1159
|
],
|
|
@@ -2114,6 +2243,7 @@ async function applyPushCommits(
|
|
|
2114
2243
|
blobCtx,
|
|
2115
2244
|
crdtMergers,
|
|
2116
2245
|
validators,
|
|
2246
|
+
ctx.serverColumns,
|
|
2117
2247
|
partition,
|
|
2118
2248
|
ctx.actorId,
|
|
2119
2249
|
ctx.context,
|
package/src/relational-rows.ts
CHANGED
|
@@ -1106,8 +1106,9 @@ export function dropTableDdl(tableName: string): string {
|
|
|
1106
1106
|
|
|
1107
1107
|
/**
|
|
1108
1108
|
* Enforce the migration subset on a table's column list: the old layout
|
|
1109
|
-
* must be
|
|
1110
|
-
*
|
|
1109
|
+
* must be a prefix (same name and type) of the new one, a column may change
|
|
1110
|
+
* only from required to nullable (SPEC §2.4, relaxing a required column), and
|
|
1111
|
+
* appended columns must be nullable (there is no default to backfill).
|
|
1111
1112
|
* Returns `true` iff columns were appended.
|
|
1112
1113
|
*/
|
|
1113
1114
|
export function assertAppendOnlyMigration(
|
|
@@ -1128,10 +1129,10 @@ export function assertAppendOnlyMigration(
|
|
|
1128
1129
|
after === undefined ||
|
|
1129
1130
|
before.name !== after.name ||
|
|
1130
1131
|
before.type !== after.type ||
|
|
1131
|
-
before.nullable
|
|
1132
|
+
(before.nullable && !after.nullable)
|
|
1132
1133
|
) {
|
|
1133
1134
|
throw new Error(
|
|
1134
|
-
`table ${JSON.stringify(tableName)}: column ${i} changed (${JSON.stringify(before)} → ${JSON.stringify({ name: after?.name, type: after?.type, nullable: after?.nullable })}) — only appending nullable columns
|
|
1135
|
+
`table ${JSON.stringify(tableName)}: column ${i} changed (${JSON.stringify(before)} → ${JSON.stringify({ name: after?.name, type: after?.type, nullable: after?.nullable })}) — only appending nullable columns and relaxing a required column are supported`,
|
|
1135
1136
|
);
|
|
1136
1137
|
}
|
|
1137
1138
|
}
|
|
@@ -1251,16 +1252,19 @@ export function rewriteValues(
|
|
|
1251
1252
|
* - `backfill`: the table just gained physical projection columns whose
|
|
1252
1253
|
* values exist in stored payloads (materialization flipped on) — the
|
|
1253
1254
|
* projection refreshes from the payload, no re-encode.
|
|
1255
|
+
* - `relaxed`: existing projection columns the bump turns from required to
|
|
1256
|
+
* nullable (SPEC §2.4). Their payload bytes are unchanged; only the
|
|
1257
|
+
* physical `NOT NULL` goes.
|
|
1254
1258
|
*/
|
|
1255
1259
|
export function rewritePlan(
|
|
1256
1260
|
table: CompiledTable,
|
|
1257
1261
|
oldLayout: readonly StoredColumnLayout[] | undefined,
|
|
1258
1262
|
physicalColumnsBefore: ReadonlySet<string> | undefined,
|
|
1259
|
-
): { migrate: boolean; backfill: boolean } {
|
|
1263
|
+
): { migrate: boolean; backfill: boolean; relaxed: string[] } {
|
|
1260
1264
|
if (oldLayout === undefined || physicalColumnsBefore === undefined) {
|
|
1261
1265
|
// Fresh table (or a pre-layout database, which cannot be reasoned
|
|
1262
1266
|
// about): nothing stored to rewrite.
|
|
1263
|
-
return { migrate: false, backfill: false };
|
|
1267
|
+
return { migrate: false, backfill: false, relaxed: [] };
|
|
1264
1268
|
}
|
|
1265
1269
|
const migrate = assertAppendOnlyMigration(table.name, oldLayout, table);
|
|
1266
1270
|
const backfill =
|
|
@@ -1270,7 +1274,79 @@ export function rewritePlan(
|
|
|
1270
1274
|
index < oldLayout.length && // value exists in old payloads
|
|
1271
1275
|
!physicalColumnsBefore.has(column.name), // column was just added
|
|
1272
1276
|
);
|
|
1273
|
-
|
|
1277
|
+
const relaxed = table.materialize
|
|
1278
|
+
? table.columns
|
|
1279
|
+
.filter(
|
|
1280
|
+
(column, index) =>
|
|
1281
|
+
oldLayout[index]?.nullable === false &&
|
|
1282
|
+
column.nullable &&
|
|
1283
|
+
physicalColumnsBefore.has(column.name),
|
|
1284
|
+
)
|
|
1285
|
+
.map((column) => column.name)
|
|
1286
|
+
: [];
|
|
1287
|
+
return { migrate, backfill, relaxed };
|
|
1288
|
+
}
|
|
1289
|
+
|
|
1290
|
+
/**
|
|
1291
|
+
* One physical column of a SQLite table plus the SQL of every index and
|
|
1292
|
+
* trigger on it, read by {@link SQLITE_TABLE_REBUILD_SQL}.
|
|
1293
|
+
*/
|
|
1294
|
+
export interface SqliteRebuildColumnRow {
|
|
1295
|
+
readonly name: string;
|
|
1296
|
+
readonly type: string;
|
|
1297
|
+
readonly not_null: number;
|
|
1298
|
+
readonly dflt_value: string | null;
|
|
1299
|
+
readonly pk: number;
|
|
1300
|
+
/** JSON array of the table's index and trigger SQL (same on every row). */
|
|
1301
|
+
readonly dependents: string;
|
|
1302
|
+
}
|
|
1303
|
+
|
|
1304
|
+
/** Binds the table name once (`?1`); one statement, so D1 spends one query. */
|
|
1305
|
+
export const SQLITE_TABLE_REBUILD_SQL = `SELECT name, type, "notnull" AS not_null, dflt_value, pk,
|
|
1306
|
+
(SELECT json_group_array(sql) FROM sqlite_master
|
|
1307
|
+
WHERE tbl_name=?1 AND type IN ('index', 'trigger') AND sql IS NOT NULL) AS dependents
|
|
1308
|
+
FROM pragma_table_info(?1) ORDER BY cid`;
|
|
1309
|
+
|
|
1310
|
+
/**
|
|
1311
|
+
* SQLite has no `ALTER COLUMN … DROP NOT NULL` (SPEC §2.4, relaxing a
|
|
1312
|
+
* required column): rebuild the table as a copy that keeps every physical
|
|
1313
|
+
* column declaration except `NOT NULL` on `relaxed`, copy every row, swap
|
|
1314
|
+
* it in, and recreate the indexes and triggers the original carried
|
|
1315
|
+
* (operator-added ones included). Rows, payloads and stored scopes are copied
|
|
1316
|
+
* verbatim. Empty when no relaxed column is physically `NOT NULL`.
|
|
1317
|
+
*/
|
|
1318
|
+
export function sqliteRelaxRebuildDdl(
|
|
1319
|
+
tableName: string,
|
|
1320
|
+
columns: readonly SqliteRebuildColumnRow[],
|
|
1321
|
+
relaxed: readonly string[],
|
|
1322
|
+
): string[] {
|
|
1323
|
+
const relax = (column: SqliteRebuildColumnRow): boolean =>
|
|
1324
|
+
column.not_null === 1 && relaxed.includes(column.name);
|
|
1325
|
+
if (!columns.some(relax)) return [];
|
|
1326
|
+
const target = quoteIdent(tableName);
|
|
1327
|
+
// The `sync_` prefix is reserved for server storage, so no app table
|
|
1328
|
+
// collides with the copy.
|
|
1329
|
+
const copy = quoteIdent(`sync_rebuild_${tableName}`);
|
|
1330
|
+
const names = columns.map((column) => quoteIdent(column.name)).join(', ');
|
|
1331
|
+
const definitions = columns.map((column) =>
|
|
1332
|
+
[
|
|
1333
|
+
quoteIdent(column.name),
|
|
1334
|
+
...(column.type === '' ? [] : [column.type]),
|
|
1335
|
+
...(column.not_null === 1 && !relax(column) ? ['NOT NULL'] : []),
|
|
1336
|
+
...(column.dflt_value === null ? [] : [`DEFAULT ${column.dflt_value}`]),
|
|
1337
|
+
].join(' '),
|
|
1338
|
+
);
|
|
1339
|
+
const primaryKey = columns
|
|
1340
|
+
.filter((column) => column.pk > 0)
|
|
1341
|
+
.sort((a, b) => a.pk - b.pk)
|
|
1342
|
+
.map((column) => quoteIdent(column.name));
|
|
1343
|
+
return [
|
|
1344
|
+
`CREATE TABLE ${copy} (${[...definitions, `PRIMARY KEY (${primaryKey.join(', ')})`].join(', ')})`,
|
|
1345
|
+
`INSERT INTO ${copy} (${names}) SELECT ${names} FROM ${target}`,
|
|
1346
|
+
`DROP TABLE ${target}`,
|
|
1347
|
+
`ALTER TABLE ${copy} RENAME TO ${target}`,
|
|
1348
|
+
...(JSON.parse(columns[0]?.dependents ?? '[]') as string[]),
|
|
1349
|
+
];
|
|
1274
1350
|
}
|
|
1275
1351
|
|
|
1276
1352
|
/**
|
package/src/schema-window.ts
CHANGED
|
@@ -31,16 +31,27 @@ export function schemaWindowOf(
|
|
|
31
31
|
if (
|
|
32
32
|
next === undefined ||
|
|
33
33
|
table.columns.length > next.columns.length ||
|
|
34
|
+
// §6.12: a version may mark an existing column server-owned; the flag
|
|
35
|
+
// never reaches the wire.
|
|
34
36
|
table.columns.some(
|
|
35
37
|
(column, i) =>
|
|
36
|
-
JSON.stringify(column) !==
|
|
38
|
+
JSON.stringify({ ...column, serverOwned: undefined }) !==
|
|
39
|
+
JSON.stringify({ ...next.columns[i], serverOwned: undefined }),
|
|
37
40
|
) ||
|
|
38
41
|
next.columns
|
|
39
42
|
.slice(table.columns.length)
|
|
40
43
|
.some((column) => !column.nullable) ||
|
|
41
44
|
table.primaryKeyIndex !== next.primaryKeyIndex ||
|
|
45
|
+
// §6.12: a pattern over an appended server-owned column is new to the
|
|
46
|
+
// current version; an older client neither holds nor writes it.
|
|
42
47
|
JSON.stringify(table.scopePatterns) !==
|
|
43
|
-
JSON.stringify(
|
|
48
|
+
JSON.stringify(
|
|
49
|
+
next.scopePatterns.filter(
|
|
50
|
+
(pattern) =>
|
|
51
|
+
pattern.columnIndex < table.columns.length ||
|
|
52
|
+
!next.serverOwnedColumnIndices.includes(pattern.columnIndex),
|
|
53
|
+
),
|
|
54
|
+
) ||
|
|
44
55
|
JSON.stringify(table.references) !== JSON.stringify(next.references)
|
|
45
56
|
) {
|
|
46
57
|
throw new Error('sync.schema_window.incompatible_schema');
|
package/src/schema.ts
CHANGED
|
@@ -154,6 +154,9 @@ 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.12: column indices marked `serverOwned`. Empty ⇒ no server column
|
|
158
|
+
* hook output is accepted for the table. */
|
|
159
|
+
readonly serverOwnedColumnIndices: readonly number[];
|
|
157
160
|
}
|
|
158
161
|
|
|
159
162
|
export interface CompiledSchema {
|
|
@@ -381,6 +384,7 @@ export function compileSchema(schema: ServerSchema): CompiledSchema {
|
|
|
381
384
|
const blobRefColumnIndices: number[] = [];
|
|
382
385
|
const crdtColumns: { index: number; crdtType: string }[] = [];
|
|
383
386
|
const encryptedColumnIndices: number[] = [];
|
|
387
|
+
const serverOwnedColumnIndices: number[] = [];
|
|
384
388
|
const references: CompiledReference[] = [];
|
|
385
389
|
for (const reference of table.references ?? []) {
|
|
386
390
|
const index = columnIndex.get(reference.column);
|
|
@@ -400,6 +404,26 @@ export function compileSchema(schema: ServerSchema): CompiledSchema {
|
|
|
400
404
|
if (column.type === 'blob_ref') blobRefColumnIndices.push(index);
|
|
401
405
|
// §5.11: an encrypted column carries wire type `bytes` + `encrypted`.
|
|
402
406
|
if (column.encrypted === true) encryptedColumnIndices.push(index);
|
|
407
|
+
// §6.12: the hook never changes who may write the row (owner set), and
|
|
408
|
+
// a client's optimistic row holds NULL until the server row arrives.
|
|
409
|
+
if (column.serverOwned === true) {
|
|
410
|
+
if (
|
|
411
|
+
index === primaryKeyIndex ||
|
|
412
|
+
!column.nullable ||
|
|
413
|
+
column.type === 'crdt' ||
|
|
414
|
+
column.encrypted === true ||
|
|
415
|
+
scopePatterns.some(
|
|
416
|
+
(pattern) =>
|
|
417
|
+
pattern.columnIndex === index &&
|
|
418
|
+
writeScopeSets[0]!.includes(pattern.variable),
|
|
419
|
+
)
|
|
420
|
+
) {
|
|
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.12)`,
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
serverOwnedColumnIndices.push(index);
|
|
426
|
+
}
|
|
403
427
|
if (column.type === 'crdt') {
|
|
404
428
|
// §5.10.1: a crdt column MUST name a crdtType (schema-compile-time
|
|
405
429
|
// requirement — a crdt column without one is a server bug).
|
|
@@ -443,6 +467,7 @@ export function compileSchema(schema: ServerSchema): CompiledSchema {
|
|
|
443
467
|
blobRefColumnIndices,
|
|
444
468
|
crdtColumns,
|
|
445
469
|
encryptedColumnIndices,
|
|
470
|
+
serverOwnedColumnIndices,
|
|
446
471
|
});
|
|
447
472
|
}
|
|
448
473
|
// §6.11: resolve incoming references and enforce the declaration rules the
|
package/src/sqlite-storage.ts
CHANGED
|
@@ -33,6 +33,9 @@ import {
|
|
|
33
33
|
rekeyRowScopesSql,
|
|
34
34
|
retiredTableNames,
|
|
35
35
|
rewritePlan,
|
|
36
|
+
SQLITE_TABLE_REBUILD_SQL,
|
|
37
|
+
type SqliteRebuildColumnRow,
|
|
38
|
+
sqliteRelaxRebuildDdl,
|
|
36
39
|
rewriteRowSql,
|
|
37
40
|
rewriteValues,
|
|
38
41
|
SCHEMA_META_DDL_SQLITE,
|
|
@@ -655,6 +658,26 @@ END`);
|
|
|
655
658
|
.run(tableName);
|
|
656
659
|
this.db.exec(dropTableDdl(tableName));
|
|
657
660
|
}
|
|
661
|
+
// A relaxed column loses its NOT NULL before the bump's other DDL
|
|
662
|
+
// (SPEC §2.4), so the index diff below sees the rebuilt table.
|
|
663
|
+
for (const table of schema.tables.values()) {
|
|
664
|
+
const { relaxed } = rewritePlan(
|
|
665
|
+
table,
|
|
666
|
+
layouts[table.name],
|
|
667
|
+
existing.get(table.name),
|
|
668
|
+
);
|
|
669
|
+
if (relaxed.length === 0) continue;
|
|
670
|
+
const columns = this.db
|
|
671
|
+
.query<SqliteRebuildColumnRow, [string]>(SQLITE_TABLE_REBUILD_SQL)
|
|
672
|
+
.all(table.name);
|
|
673
|
+
for (const statement of sqliteRelaxRebuildDdl(
|
|
674
|
+
table.name,
|
|
675
|
+
columns,
|
|
676
|
+
relaxed,
|
|
677
|
+
)) {
|
|
678
|
+
this.db.exec(statement);
|
|
679
|
+
}
|
|
680
|
+
}
|
|
658
681
|
for (const statement of schemaDdl(
|
|
659
682
|
schema,
|
|
660
683
|
existing,
|
package/src/validate.ts
CHANGED
|
@@ -315,3 +315,35 @@ export function toValidateRow(
|
|
|
315
315
|
});
|
|
316
316
|
return row;
|
|
317
317
|
}
|
|
318
|
+
|
|
319
|
+
/** §6.12: the accepted upsert a server column hook completes. `row` is the
|
|
320
|
+
* merged row (present columns over stored values, crdt columns merged) with
|
|
321
|
+
* every server-owned column as stored, or NULL on an insert. */
|
|
322
|
+
export interface ServerColumnsInput {
|
|
323
|
+
readonly table: string;
|
|
324
|
+
readonly rowId: string;
|
|
325
|
+
readonly row: ValidateRow;
|
|
326
|
+
readonly stored: ValidateRow | undefined;
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
export interface ServerColumnsContext {
|
|
330
|
+
readonly actorId: string;
|
|
331
|
+
readonly partition: string;
|
|
332
|
+
readonly context?: string;
|
|
333
|
+
/** The §6.8 candidate-state reader of the push transaction. */
|
|
334
|
+
readonly read: CommitValidationReader;
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* §6.12: returns values for the table's server-owned columns; a column it
|
|
339
|
+
* omits keeps its value in `row`. A key that names no server-owned column, a
|
|
340
|
+
* value the column's type refuses, or a changed scope column on an update
|
|
341
|
+
* rejects the commit with `sync.constraint_violation`, like any other throw
|
|
342
|
+
* that is not a {@link ValidationRejection}.
|
|
343
|
+
*/
|
|
344
|
+
export type ServerColumnsHook = (
|
|
345
|
+
input: ServerColumnsInput,
|
|
346
|
+
ctx: ServerColumnsContext,
|
|
347
|
+
) => ValidateRow | Promise<ValidateRow>;
|
|
348
|
+
|
|
349
|
+
export type ServerColumnsRegistry = Readonly<Record<string, ServerColumnsHook>>;
|