@syncular/server 0.62.0 → 0.63.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.
@@ -40,7 +40,7 @@ var _a;
40
40
  * rather than a lock D1 does not expose.
41
41
  */
42
42
  import { decodeRow } from '@syncular/core';
43
- import { assertAppendOnlyMigration, assertPhysicalColumns, assertStoredLayouts, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, scanRowPageSql, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowSql, selectRowsSql, selectRowsForRewriteSql, tableColumnNames, toSqlValue, } from './relational-rows.js';
43
+ import { assertAppendOnlyMigration, assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, scanRowPageSql, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowSql, selectRowsSql, selectRowsForRewriteSql, tableColumnNames, toSqlValue, } from './relational-rows.js';
44
44
  import { matchesEffective } from './scopes.js';
45
45
  import { asUint8Array, deserializePushResult, placeholders, SqliteFamilyStorage, SqliteFamilyTransaction, sqliteDdlStatements, SQLITE_UPGRADE_PROBE, sqliteUpgradeDdl, toStoredRow, toTombstone, } from './sqlite-dialect.js';
46
46
  import { commitDeleteFloor } from './storage.js';
@@ -678,6 +678,15 @@ export class D1ServerStorage extends SqliteFamilyStorage {
678
678
  let claim = await read(this.#db.prepare('SELECT * FROM sync_schema_migration WHERE id=1'));
679
679
  if (claim === null && marker?.schema_version === schema.version) {
680
680
  assertStoredLayouts(schema, marker.layouts);
681
+ if (!assertStoredScopePatterns(schema, marker.layouts)) {
682
+ // A marker from before 0.63 records no scope patterns: record the
683
+ // configured ones, which every later start compares against.
684
+ statementsExecuted++;
685
+ await this.#db
686
+ .prepare('UPDATE sync_schema_meta SET layouts=? WHERE id=1 AND schema_version=?')
687
+ .bind(layoutsOf(schema), schema.version)
688
+ .run();
689
+ }
681
690
  statementsExecuted++;
682
691
  const present = await this.#db
683
692
  .prepare(`SELECT name FROM sqlite_master WHERE type='table' AND name IN (${placeholders(D1_CORE_TABLES.length)})`)
@@ -735,6 +744,7 @@ export class D1ServerStorage extends SqliteFamilyStorage {
735
744
  throw new StorageQueryError('sync.storage.schema_changed');
736
745
  }
737
746
  if (claim === null) {
747
+ assertStoredScopePatterns(schema, marker?.layouts);
738
748
  const layouts = parseLayouts(marker?.layouts);
739
749
  for (const table of tables) {
740
750
  const oldLayout = layouts[table.name];
@@ -5,7 +5,7 @@ import { serveNotReadyError } from './readiness.js';
5
5
  import { bindAuthoritativePartition, postgresPlaceholders, prepareAuthoritativeQuery, } from './authoritative-query.js';
6
6
  import { syncError } from './errors.js';
7
7
  import { asBytes, asNumber, } from './pg-executor.js';
8
- import { assertPhysicalColumns, assertStoredLayouts, deleteRowSql, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, tableColumnNames, retiredTableNames, rewritePlan, columnSqlType, rewriteValues, SCHEMA_META_DDL_POSTGRES, SYNC_COLUMN_VERSIONS_COLUMN, SYNC_PARTITION_COLUMN, SYNC_PAYLOAD_COLUMN, SYNC_ROW_ID_COLUMN, SYNC_SCOPES_COLUMN, SYNC_VERSION_COLUMN, scanRowBatchSql, scanRowPageSql, indexSignature, schemaDdl, serverIndexDdl, rowsReferencingBlobSql, selectRowSql, selectRowsSql, prefetchRowsBatchSql, selectRowsForRewriteSql, upsertSql, upsertValues, } from './relational-rows.js';
8
+ import { assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, deleteRowSql, dropTableDdl, indexRowPageStatement, layoutsOf, migratePayload, parseLayouts, quoteIdent, tableColumnNames, retiredTableNames, rewritePlan, columnSqlType, rewriteValues, SCHEMA_META_DDL_POSTGRES, SYNC_COLUMN_VERSIONS_COLUMN, SYNC_PARTITION_COLUMN, SYNC_PAYLOAD_COLUMN, SYNC_ROW_ID_COLUMN, SYNC_SCOPES_COLUMN, SYNC_VERSION_COLUMN, scanRowBatchSql, scanRowPageSql, indexSignature, schemaDdl, serverIndexDdl, rowsReferencingBlobSql, selectRowSql, selectRowsSql, prefetchRowsBatchSql, selectRowsForRewriteSql, upsertSql, upsertValues, } from './relational-rows.js';
9
9
  import { matchesEffective, sha256Hex } from './scopes.js';
10
10
  import { isPostgresConstraintError, StorageConstraintError, } from './storage-errors.js';
11
11
  import { assertScopeIndexedScan, readLiveWindowPages, resolveIndexRowScan, } from './storage-query.js';
@@ -1619,7 +1619,13 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
1619
1619
  throw new Error(`stored schema version ${stored} is newer than the configured schema (${schema.version}) — refusing to run an older server against a migrated database`);
1620
1620
  }
1621
1621
  if (stored !== undefined && stored === schema.version) {
1622
- assertStoredLayouts(schema, typeof marker.layouts === 'string' ? marker.layouts : undefined);
1622
+ const storedLayouts = typeof marker.layouts === 'string' ? marker.layouts : undefined;
1623
+ assertStoredLayouts(schema, storedLayouts);
1624
+ if (!assertStoredScopePatterns(schema, storedLayouts)) {
1625
+ // A marker from before 0.63 records no scope patterns: record the
1626
+ // configured ones, which every later start compares against.
1627
+ await this.#exec.query('UPDATE sync_schema_meta SET layouts=$1 WHERE id=1 AND schema_version=$2', [layoutsOf(schema), schema.version]);
1628
+ }
1623
1629
  const columns = JSON.parse(marker.columns);
1624
1630
  for (const table of schema.tables.values()) {
1625
1631
  assertPhysicalColumns(table, columns
@@ -1678,9 +1684,11 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
1678
1684
  }
1679
1685
  if (lockedVersion === schema.version)
1680
1686
  return;
1681
- const layouts = parseLayouts(typeof lockedMarker[0]?.layouts === 'string'
1687
+ const lockedLayouts = typeof lockedMarker[0]?.layouts === 'string'
1682
1688
  ? lockedMarker[0].layouts
1683
- : undefined);
1689
+ : undefined;
1690
+ assertStoredScopePatterns(schema, lockedLayouts);
1691
+ const layouts = parseLayouts(lockedLayouts);
1684
1692
  const retiredTables = retiredTableNames(schema, layouts);
1685
1693
  const existing = new Map();
1686
1694
  const existingIndexes = new Map();
@@ -298,6 +298,13 @@ export type StoredLayouts = Record<string, readonly StoredColumnLayout[]>;
298
298
  /** The layouts JSON persisted alongside the schema version marker. */
299
299
  export declare function layoutsOf(schema: CompiledSchema): string;
300
300
  export declare function parseLayouts(json: string | null | undefined): StoredLayouts;
301
+ /**
302
+ * §3.1: stored scopes are keyed by the scope patterns a row was written
303
+ * under, and no schema change re-keys them. Refuse a configured table whose
304
+ * patterns differ from the stored ones. Returns false when the marker
305
+ * predates recorded patterns (nothing to compare), so the caller records them.
306
+ */
307
+ export declare function assertStoredScopePatterns(schema: CompiledSchema, storedJson: string | null | undefined): boolean;
301
308
  /**
302
309
  * Version equality is not layout equality: a marker written by another build
303
310
  * at the same version describes rows the running codec cannot decode. Refuse
@@ -701,19 +701,54 @@ export const SCHEMA_META_DDL_POSTGRES = `CREATE TABLE IF NOT EXISTS sync_schema_
701
701
  /** The layouts JSON persisted alongside the schema version marker. */
702
702
  export function layoutsOf(schema) {
703
703
  const out = {};
704
+ const scopes = {};
704
705
  for (const table of schema.tables.values()) {
705
706
  out[table.name] = table.columns.map((column) => ({
706
707
  name: column.name,
707
708
  type: column.type,
708
709
  nullable: column.nullable,
709
710
  }));
711
+ scopes[table.name] = scopePatternsOf(table);
710
712
  }
713
+ // `sync_` is a reserved table-name prefix, so this key names no table.
714
+ out.sync_scopes = scopes;
711
715
  return JSON.stringify(out);
712
716
  }
717
+ function scopePatternsOf(table) {
718
+ return table.scopePatterns
719
+ .map(({ prefix, variable, column }) => `${prefix}:{${variable}}@${column}`)
720
+ .sort();
721
+ }
713
722
  export function parseLayouts(json) {
714
723
  if (json === null || json === undefined || json.length === 0)
715
724
  return {};
716
- return JSON.parse(json);
725
+ const { sync_scopes: _scopes, ...layouts } = JSON.parse(json);
726
+ return layouts;
727
+ }
728
+ /**
729
+ * §3.1: stored scopes are keyed by the scope patterns a row was written
730
+ * under, and no schema change re-keys them. Refuse a configured table whose
731
+ * patterns differ from the stored ones. Returns false when the marker
732
+ * predates recorded patterns (nothing to compare), so the caller records them.
733
+ */
734
+ export function assertStoredScopePatterns(schema, storedJson) {
735
+ if (storedJson === null || storedJson === undefined || storedJson === '')
736
+ return true;
737
+ const stored = JSON.parse(storedJson).sync_scopes;
738
+ if (stored === undefined)
739
+ return false;
740
+ for (const table of schema.tables.values()) {
741
+ const before = stored[table.name];
742
+ const after = scopePatternsOf(table);
743
+ if (before !== undefined && before.join('\n') !== after.join('\n')) {
744
+ throw new StorageQueryError('sync.storage.scope_patterns_changed', {
745
+ table: table.name,
746
+ stored: before.join(' '),
747
+ configured: after.join(' '),
748
+ });
749
+ }
750
+ }
751
+ return true;
717
752
  }
718
753
  /**
719
754
  * Version equality is not layout equality: a marker written by another build
@@ -12,7 +12,7 @@
12
12
  import { isSqliteConstraintError, retainRollbackFailure, rollbackAfterError, StorageConstraintError, StorageQueryError, } from './storage-errors.js';
13
13
  import { commitDeleteFloor, serveGateRefusal } from './storage.js';
14
14
  import { serveNotReadyError } from './readiness.js';
15
- import { assertPhysicalColumns, assertStoredLayouts, dropTableDdl, layoutsOf, migratePayload, parseLayouts, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowsForRewriteSql, } from './relational-rows.js';
15
+ import { assertPhysicalColumns, assertStoredLayouts, assertStoredScopePatterns, dropTableDdl, layoutsOf, migratePayload, parseLayouts, retiredTableNames, rewritePlan, rewriteRowSql, rewriteValues, SCHEMA_META_DDL_SQLITE, schemaDdl, serverIndexDdl, SQLITE_TABLE_INDEXES_SQL, sqliteIndexSignatures, selectRowsForRewriteSql, } from './relational-rows.js';
16
16
  import { SQLITE_DDL, SQLITE_UPGRADE_PROBE, SqliteFamilyStorage, SqliteFamilyTransaction, sqliteUpgradeDdl, } from './sqlite-dialect.js';
17
17
  import { SqliteAdapterRequiredError, } from './sqlite-driver.js';
18
18
  function toStoredCheckpoint(record) {
@@ -330,6 +330,13 @@ END`);
330
330
  }
331
331
  if (marker !== null && marker.schema_version === schema.version) {
332
332
  assertStoredLayouts(schema, marker.layouts);
333
+ if (!assertStoredScopePatterns(schema, marker.layouts)) {
334
+ // A marker from before 0.63 records no scope patterns: record the
335
+ // configured ones, which every later start compares against.
336
+ this.db
337
+ .query('UPDATE sync_schema_meta SET layouts=? WHERE id=1 AND schema_version=?')
338
+ .run(layoutsOf(schema), schema.version);
339
+ }
333
340
  for (const table of schema.tables.values()) {
334
341
  const escapedTableName = table.name.replaceAll('"', '""');
335
342
  const columns = this.db
@@ -406,6 +413,7 @@ END`);
406
413
  this.schemaVersion = schema.version;
407
414
  return;
408
415
  }
416
+ assertStoredScopePatterns(schema, locked?.layouts);
409
417
  const layouts = parseLayouts(locked?.layouts);
410
418
  const retiredTables = retiredTableNames(schema, layouts);
411
419
  for (const tableName of retiredTables) {
@@ -9,7 +9,7 @@ export declare class StorageConstraintError extends Error {
9
9
  constructor(cause: unknown, opIndex?: number);
10
10
  }
11
11
  /** Stable, privacy-safe failures for trusted server storage queries. */
12
- export type StorageQueryErrorCode = 'sync.storage.schema_migration_pending' | 'sync.storage.schema_migration_conflict' | 'sync.storage.schema_changed' | 'sync.storage.invalid_migration_budget' | 'sync.storage.scan_requires_scope' | 'sync.storage.index_not_found' | 'sync.storage.index_not_materialized' | 'sync.storage.index_value_count_mismatch' | 'sync.storage.invalid_limit' | 'sync.storage.prune_epoch_mismatch' | 'sync.storage.partition_unregistered' | 'sync.storage.invalid_prune_cursor' | 'sync.storage.checkpoint_not_declared' | 'sync.storage.checkpoint_unsupported' | 'sync.storage.checkpoint_fence_missing' | 'sync.storage.checkpoint_incomplete' | 'sync.storage.stored_layout_mismatch' | 'sync.storage.physical_layout_mismatch' | 'sync.storage.transaction_query_unsupported' | 'sync.storage.partition_lock_unsupported' | 'sync.storage.query_over_staged_writes' | 'sync.storage.erase_unsupported' | 'sync.storage.invalid_erase_query';
12
+ export type StorageQueryErrorCode = 'sync.storage.schema_migration_pending' | 'sync.storage.schema_migration_conflict' | 'sync.storage.schema_changed' | 'sync.storage.invalid_migration_budget' | 'sync.storage.scan_requires_scope' | 'sync.storage.index_not_found' | 'sync.storage.index_not_materialized' | 'sync.storage.index_value_count_mismatch' | 'sync.storage.invalid_limit' | 'sync.storage.prune_epoch_mismatch' | 'sync.storage.partition_unregistered' | 'sync.storage.invalid_prune_cursor' | 'sync.storage.checkpoint_not_declared' | 'sync.storage.checkpoint_unsupported' | 'sync.storage.checkpoint_fence_missing' | 'sync.storage.checkpoint_incomplete' | 'sync.storage.stored_layout_mismatch' | 'sync.storage.scope_patterns_changed' | 'sync.storage.physical_layout_mismatch' | 'sync.storage.transaction_query_unsupported' | 'sync.storage.partition_lock_unsupported' | 'sync.storage.query_over_staged_writes' | 'sync.storage.erase_unsupported' | 'sync.storage.invalid_erase_query';
13
13
  /**
14
14
  * Host-only query error. Messages never include identifiers, values, SQL,
15
15
  * paths, or row data; callers branch on `code`, never message text. The
@@ -24,6 +24,7 @@ const STORAGE_QUERY_MESSAGES = {
24
24
  'sync.storage.checkpoint_fence_missing': 'checkpoint activation requires the writer fence raised at declaration',
25
25
  'sync.storage.checkpoint_incomplete': 'a declared backfill checkpoint is not activated and this caller did not declare it',
26
26
  'sync.storage.stored_layout_mismatch': 'stored schema layouts disagree with the configured schema at the same version',
27
+ 'sync.storage.scope_patterns_changed': 'a table declares scope patterns other than the ones its stored rows are indexed under',
27
28
  'sync.storage.physical_layout_mismatch': 'a synced table does not match the storage layout the running code reads and writes',
28
29
  'sync.storage.transaction_query_unsupported': 'this storage transaction cannot run registered queries',
29
30
  'sync.storage.partition_lock_unsupported': 'this storage transaction cannot serialize partition authority',
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@syncular/server",
3
- "version": "0.62.0",
3
+ "version": "0.63.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.62.0"
71
+ "@syncular/core": "0.63.0"
72
72
  },
73
73
  "devDependencies": {
74
74
  "@electric-sql/pglite": "^0.5.4"
package/src/d1-storage.ts CHANGED
@@ -44,6 +44,7 @@ import {
44
44
  assertAppendOnlyMigration,
45
45
  assertPhysicalColumns,
46
46
  assertStoredLayouts,
47
+ assertStoredScopePatterns,
47
48
  dropTableDdl,
48
49
  indexRowPageStatement,
49
50
  layoutsOf,
@@ -1052,6 +1053,17 @@ export class D1ServerStorage
1052
1053
  );
1053
1054
  if (claim === null && marker?.schema_version === schema.version) {
1054
1055
  assertStoredLayouts(schema, marker.layouts);
1056
+ if (!assertStoredScopePatterns(schema, marker.layouts)) {
1057
+ // A marker from before 0.63 records no scope patterns: record the
1058
+ // configured ones, which every later start compares against.
1059
+ statementsExecuted++;
1060
+ await this.#db
1061
+ .prepare(
1062
+ 'UPDATE sync_schema_meta SET layouts=? WHERE id=1 AND schema_version=?',
1063
+ )
1064
+ .bind(layoutsOf(schema), schema.version)
1065
+ .run();
1066
+ }
1055
1067
  statementsExecuted++;
1056
1068
  const present = await this.#db
1057
1069
  .prepare(
@@ -1118,6 +1130,7 @@ export class D1ServerStorage
1118
1130
  throw new StorageQueryError('sync.storage.schema_changed');
1119
1131
  }
1120
1132
  if (claim === null) {
1133
+ assertStoredScopePatterns(schema, marker?.layouts);
1121
1134
  const layouts = parseLayouts(marker?.layouts);
1122
1135
  for (const table of tables) {
1123
1136
  const oldLayout = layouts[table.name];
@@ -64,6 +64,7 @@ import {
64
64
  import {
65
65
  assertPhysicalColumns,
66
66
  assertStoredLayouts,
67
+ assertStoredScopePatterns,
67
68
  deleteRowSql,
68
69
  dropTableDdl,
69
70
  indexRowPageStatement,
@@ -2361,10 +2362,17 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
2361
2362
  );
2362
2363
  }
2363
2364
  if (stored !== undefined && stored === schema.version) {
2364
- assertStoredLayouts(
2365
- schema,
2366
- typeof marker.layouts === 'string' ? marker.layouts : undefined,
2367
- );
2365
+ const storedLayouts =
2366
+ typeof marker.layouts === 'string' ? marker.layouts : undefined;
2367
+ assertStoredLayouts(schema, storedLayouts);
2368
+ if (!assertStoredScopePatterns(schema, storedLayouts)) {
2369
+ // A marker from before 0.63 records no scope patterns: record the
2370
+ // configured ones, which every later start compares against.
2371
+ await this.#exec.query(
2372
+ 'UPDATE sync_schema_meta SET layouts=$1 WHERE id=1 AND schema_version=$2',
2373
+ [layoutsOf(schema), schema.version],
2374
+ );
2375
+ }
2368
2376
  const columns = JSON.parse(marker.columns) as {
2369
2377
  tbl: string;
2370
2378
  name: string;
@@ -2444,11 +2452,12 @@ FOR EACH ROW EXECUTE FUNCTION syncular_writer_fence()`,
2444
2452
  );
2445
2453
  }
2446
2454
  if (lockedVersion === schema.version) return;
2447
- const layouts = parseLayouts(
2455
+ const lockedLayouts =
2448
2456
  typeof lockedMarker[0]?.layouts === 'string'
2449
2457
  ? lockedMarker[0].layouts
2450
- : undefined,
2451
- );
2458
+ : undefined;
2459
+ assertStoredScopePatterns(schema, lockedLayouts);
2460
+ const layouts = parseLayouts(lockedLayouts);
2452
2461
  const retiredTables = retiredTableNames(schema, layouts);
2453
2462
  const existing = new Map<string, ReadonlySet<string>>();
2454
2463
  const existingIndexes = new Map<string, ReadonlyMap<string, string>>();
@@ -872,20 +872,66 @@ export type StoredLayouts = Record<string, readonly StoredColumnLayout[]>;
872
872
 
873
873
  /** The layouts JSON persisted alongside the schema version marker. */
874
874
  export function layoutsOf(schema: CompiledSchema): string {
875
- const out: Record<string, StoredColumnLayout[]> = {};
875
+ const out: Record<string, unknown> = {};
876
+ const scopes: Record<string, string[]> = {};
876
877
  for (const table of schema.tables.values()) {
877
878
  out[table.name] = table.columns.map((column) => ({
878
879
  name: column.name,
879
880
  type: column.type,
880
881
  nullable: column.nullable,
881
882
  }));
883
+ scopes[table.name] = scopePatternsOf(table);
882
884
  }
885
+ // `sync_` is a reserved table-name prefix, so this key names no table.
886
+ out.sync_scopes = scopes;
883
887
  return JSON.stringify(out);
884
888
  }
885
889
 
890
+ function scopePatternsOf(table: CompiledTable): string[] {
891
+ return table.scopePatterns
892
+ .map(({ prefix, variable, column }) => `${prefix}:{${variable}}@${column}`)
893
+ .sort();
894
+ }
895
+
886
896
  export function parseLayouts(json: string | null | undefined): StoredLayouts {
887
897
  if (json === null || json === undefined || json.length === 0) return {};
888
- return JSON.parse(json) as StoredLayouts;
898
+ const { sync_scopes: _scopes, ...layouts } = JSON.parse(json) as Record<
899
+ string,
900
+ unknown
901
+ >;
902
+ return layouts as StoredLayouts;
903
+ }
904
+
905
+ /**
906
+ * §3.1: stored scopes are keyed by the scope patterns a row was written
907
+ * under, and no schema change re-keys them. Refuse a configured table whose
908
+ * patterns differ from the stored ones. Returns false when the marker
909
+ * predates recorded patterns (nothing to compare), so the caller records them.
910
+ */
911
+ export function assertStoredScopePatterns(
912
+ schema: CompiledSchema,
913
+ storedJson: string | null | undefined,
914
+ ): boolean {
915
+ if (storedJson === null || storedJson === undefined || storedJson === '')
916
+ return true;
917
+ const stored = (
918
+ JSON.parse(storedJson) as {
919
+ sync_scopes?: Record<string, readonly string[]>;
920
+ }
921
+ ).sync_scopes;
922
+ if (stored === undefined) return false;
923
+ for (const table of schema.tables.values()) {
924
+ const before = stored[table.name];
925
+ const after = scopePatternsOf(table);
926
+ if (before !== undefined && before.join('\n') !== after.join('\n')) {
927
+ throw new StorageQueryError('sync.storage.scope_patterns_changed', {
928
+ table: table.name,
929
+ stored: before.join(' '),
930
+ configured: after.join(' '),
931
+ });
932
+ }
933
+ }
934
+ return true;
889
935
  }
890
936
 
891
937
  /**
@@ -21,6 +21,7 @@ import { serveNotReadyError } from './readiness';
21
21
  import {
22
22
  assertPhysicalColumns,
23
23
  assertStoredLayouts,
24
+ assertStoredScopePatterns,
24
25
  dropTableDdl,
25
26
  layoutsOf,
26
27
  migratePayload,
@@ -507,6 +508,15 @@ END`);
507
508
  }
508
509
  if (marker !== null && marker.schema_version === schema.version) {
509
510
  assertStoredLayouts(schema, marker.layouts);
511
+ if (!assertStoredScopePatterns(schema, marker.layouts)) {
512
+ // A marker from before 0.63 records no scope patterns: record the
513
+ // configured ones, which every later start compares against.
514
+ this.db
515
+ .query(
516
+ 'UPDATE sync_schema_meta SET layouts=? WHERE id=1 AND schema_version=?',
517
+ )
518
+ .run(layoutsOf(schema), schema.version);
519
+ }
510
520
  for (const table of schema.tables.values()) {
511
521
  const escapedTableName = table.name.replaceAll('"', '""');
512
522
  const columns = this.db
@@ -618,6 +628,7 @@ END`);
618
628
  this.schemaVersion = schema.version;
619
629
  return;
620
630
  }
631
+ assertStoredScopePatterns(schema, locked?.layouts);
621
632
  const layouts = parseLayouts(locked?.layouts);
622
633
  const retiredTables = retiredTableNames(schema, layouts);
623
634
  for (const tableName of retiredTables) {
@@ -32,6 +32,7 @@ export type StorageQueryErrorCode =
32
32
  | 'sync.storage.checkpoint_fence_missing'
33
33
  | 'sync.storage.checkpoint_incomplete'
34
34
  | 'sync.storage.stored_layout_mismatch'
35
+ | 'sync.storage.scope_patterns_changed'
35
36
  | 'sync.storage.physical_layout_mismatch'
36
37
  | 'sync.storage.transaction_query_unsupported'
37
38
  | 'sync.storage.partition_lock_unsupported'
@@ -65,6 +66,8 @@ const STORAGE_QUERY_MESSAGES: Readonly<Record<StorageQueryErrorCode, string>> =
65
66
  'a declared backfill checkpoint is not activated and this caller did not declare it',
66
67
  'sync.storage.stored_layout_mismatch':
67
68
  'stored schema layouts disagree with the configured schema at the same version',
69
+ 'sync.storage.scope_patterns_changed':
70
+ 'a table declares scope patterns other than the ones its stored rows are indexed under',
68
71
  'sync.storage.physical_layout_mismatch':
69
72
  'a synced table does not match the storage layout the running code reads and writes',
70
73
  'sync.storage.transaction_query_unsupported':