@syncular/client 0.78.2 → 0.79.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/dist/apply.js +1 -1
- package/dist/blob.js +2 -3
- package/dist/previous-version.js +2 -3
- package/dist/schema.d.ts +1 -0
- package/dist/schema.js +35 -20
- package/package.json +3 -3
- package/src/apply.ts +1 -1
- package/src/blob.ts +2 -5
- package/src/previous-version.ts +2 -5
- package/src/schema.ts +43 -32
package/dist/apply.js
CHANGED
|
@@ -400,7 +400,7 @@ export async function applySqliteSegment(db, schema, table, bytes, descriptor, o
|
|
|
400
400
|
const skipped = new Set();
|
|
401
401
|
const landing = rows.filter((row) => lands(db, table, String(row[table.primaryKey]), Number(row._syncular_version), kept, skipped));
|
|
402
402
|
for (;;) {
|
|
403
|
-
db.exec(`INSERT INTO ${quoteIdent(table.name)}(${insertNames.join(', ')})SELECT ${[...names, quoteIdent('_syncular_version')].join(', ')} FROM ${source} WHERE ${where}
|
|
403
|
+
db.exec(`INSERT INTO ${quoteIdent(table.name)}(${insertNames.join(', ')})SELECT ${[...names, quoteIdent('_syncular_version')].join(', ')} FROM ${source} AS image WHERE ${where} AND NOT EXISTS(SELECT 1 FROM ${quoteIdent(table.name)} AS held WHERE held.${primaryKey}=image.${primaryKey} AND held.${version}>=image._syncular_version) ON CONFLICT(${primaryKey})DO UPDATE SET ${updates}`, params);
|
|
404
404
|
const copied = Number(db.query(`SELECT count(*)AS n FROM ${source} WHERE ${where}`, params)[0]?.n);
|
|
405
405
|
applied += copied;
|
|
406
406
|
chunk += copied;
|
package/dist/blob.js
CHANGED
|
@@ -8,7 +8,7 @@ export const BLOB_COMMIT_REFS = '_syncular_blob_commit_refs';
|
|
|
8
8
|
*/
|
|
9
9
|
import { parseBlobRef, serializeBlobRef } from '@syncular/core';
|
|
10
10
|
import { ClientSyncError } from './errors.js';
|
|
11
|
-
import { quoteIdent } from './schema.js';
|
|
11
|
+
import { quoteIdent, tableExists } from './schema.js';
|
|
12
12
|
/** `"sha256:" + hex` of the bytes — the content address (§5.9.1). */
|
|
13
13
|
export async function computeBlobId(bytes) {
|
|
14
14
|
const input = bytes.buffer instanceof ArrayBuffer
|
|
@@ -21,8 +21,7 @@ export async function computeBlobId(bytes) {
|
|
|
21
21
|
return `sha256:${hex}`;
|
|
22
22
|
}
|
|
23
23
|
export function ensureBlobSchema(db) {
|
|
24
|
-
|
|
25
|
-
if (existing.length > 0) {
|
|
24
|
+
if (tableExists(db, '_syncular_blobs')) {
|
|
26
25
|
const columns = db
|
|
27
26
|
.query('PRAGMA table_info(_syncular_blobs)')
|
|
28
27
|
.map((row) => row.name)
|
package/dist/previous-version.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ClientSyncError } from './errors.js';
|
|
2
2
|
import { INVALID_HOST_RESPONSE_CODE } from './local-rebootstrap.js';
|
|
3
|
-
import { fromSqlValue, jsonToRowValue, localColumnType, quoteIdent, rowValueToJson, } from './schema.js';
|
|
3
|
+
import { fromSqlValue, jsonToRowValue, localColumnType, quoteIdent, rowValueToJson, tableExists, } from './schema.js';
|
|
4
4
|
import { getMeta, setMeta, META_TABLE } from './state.js';
|
|
5
5
|
/** `_syncular_meta` key holding the persisted {@link LocalSchemaDescriptor}. */
|
|
6
6
|
const LOCAL_SCHEMA_DESCRIPTOR_KEY = 'localSchemaDescriptor';
|
|
@@ -228,8 +228,7 @@ export function withPreviousVersionContainer(db, fn, removeFile = false) {
|
|
|
228
228
|
}
|
|
229
229
|
/** Read the container's own metadata record; `undefined` when absent/corrupt. */
|
|
230
230
|
export function readPreviousVersionContainer(db) {
|
|
231
|
-
|
|
232
|
-
if (meta.length === 0)
|
|
231
|
+
if (!tableExists(db, CONTAINER_META_TABLE))
|
|
233
232
|
return undefined;
|
|
234
233
|
const raw = db.query(`SELECT record FROM ${quoteIdent(CONTAINER_META_TABLE)} WHERE id=1`)[0]?.record;
|
|
235
234
|
if (raw === undefined)
|
package/dist/schema.d.ts
CHANGED
|
@@ -110,6 +110,7 @@ export declare function matchesLocalScopes(table: CompiledClientTable, scopes: R
|
|
|
110
110
|
*/
|
|
111
111
|
export declare function pkSqlValue(table: CompiledClientTable, rowId: string): SqlValue;
|
|
112
112
|
export declare function quoteIdent(name: string): string;
|
|
113
|
+
export declare function tableExists(db: ClientDatabase, name: string): boolean;
|
|
113
114
|
/**
|
|
114
115
|
* §5.11: the app-side type of a column for local (plaintext) storage. For an
|
|
115
116
|
* encrypted column this is `declaredType` — the local mirror stays plaintext,
|
package/dist/schema.js
CHANGED
|
@@ -216,6 +216,11 @@ export function pkSqlValue(table, rowId) {
|
|
|
216
216
|
export function quoteIdent(name) {
|
|
217
217
|
return `"${name.replaceAll('"', '""')}"`;
|
|
218
218
|
}
|
|
219
|
+
export function tableExists(db, name) {
|
|
220
|
+
return (db.query("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?", [
|
|
221
|
+
name,
|
|
222
|
+
]).length > 0);
|
|
223
|
+
}
|
|
219
224
|
/**
|
|
220
225
|
* §5.11: the app-side type of a column for local (plaintext) storage. For an
|
|
221
226
|
* encrypted column this is `declaredType` — the local mirror stays plaintext,
|
|
@@ -288,21 +293,22 @@ const FTS_SOURCE_ID_COLUMN = '_syncular_source_id';
|
|
|
288
293
|
* Limitation: because a BEFORE trigger runs before SQLite resolves the
|
|
289
294
|
* conflict, an `OR IGNORE` write whose row is dropped still executes these
|
|
290
295
|
* displacement DELETEs, transiently removing the surviving row's projection
|
|
291
|
-
* entry.
|
|
292
|
-
*
|
|
296
|
+
* entry. The same holds for an upsert whose `DO UPDATE ... WHERE` skips the
|
|
297
|
+
* row, so Syncular's mirror writes never offer a row that will not replace
|
|
298
|
+
* (§5.3); see the guard-block comment below for the full rationale. An open
|
|
299
|
+
* rebuilds a projection whose mapping holds a different row count than its
|
|
300
|
+
* source table.
|
|
293
301
|
*/
|
|
294
302
|
function createFtsProjection(db, table, index) {
|
|
295
303
|
const projection = quoteIdent(index.name);
|
|
296
304
|
const sourceTable = quoteIdent(table.name);
|
|
297
305
|
const primaryKey = quoteIdent(table.primaryKey);
|
|
298
|
-
const existed = db
|
|
306
|
+
const existed = tableExists(db, index.name);
|
|
299
307
|
const indexedColumns = index.columns.map(quoteIdent);
|
|
300
308
|
const tokenizer = index.tokenize.replaceAll("'", "''");
|
|
301
309
|
db.exec(`CREATE VIRTUAL TABLE IF NOT EXISTS ${projection} USING fts5(${quoteIdent(FTS_SOURCE_ID_COLUMN)} UNINDEXED,${indexedColumns.join(', ')},tokenize='${tokenizer}')`);
|
|
302
310
|
const mapping = quoteIdent(`_syncular_fts_${index.name}`);
|
|
303
|
-
const mapped = db.
|
|
304
|
-
`_syncular_fts_${index.name}`,
|
|
305
|
-
]).length > 0;
|
|
311
|
+
const mapped = tableExists(db, `_syncular_fts_${index.name}`);
|
|
306
312
|
db.exec(`CREATE TABLE IF NOT EXISTS ${mapping}(id INTEGER PRIMARY KEY,source_id TEXT NOT NULL UNIQUE)`);
|
|
307
313
|
const sourceId = `CAST(${primaryKey} AS TEXT)`;
|
|
308
314
|
const newSourceId = `CAST(new.${primaryKey} AS TEXT)`;
|
|
@@ -332,16 +338,19 @@ function createFtsProjection(db, table, index) {
|
|
|
332
338
|
// `UPDATE OR REPLACE` needs no BEFORE guard: the new pk equals the displaced
|
|
333
339
|
// row's pk, so the AFTER UPDATE delete of the new source id already clears it.
|
|
334
340
|
//
|
|
335
|
-
// Known limitation
|
|
336
|
-
//
|
|
337
|
-
//
|
|
338
|
-
//
|
|
339
|
-
// SURVIVING row's projection entry (a
|
|
340
|
-
// rewritten). Moving cleanup to AFTER
|
|
341
|
-
// reintroduce the REPLACE-doesn't-fire-
|
|
342
|
-
// per write, so the guards stay
|
|
343
|
-
//
|
|
344
|
-
//
|
|
341
|
+
// Known limitation: a BEFORE trigger fires before SQLite resolves the
|
|
342
|
+
// conflict, and SQLite exposes no signal for the eventual resolution. So an
|
|
343
|
+
// `INSERT OR IGNORE` / `UPDATE OR IGNORE` whose row is dropped on conflict,
|
|
344
|
+
// and an upsert whose `DO UPDATE ... WHERE` skips the row, still run these
|
|
345
|
+
// displacement DELETEs, removing the SURVIVING row's projection entry (a
|
|
346
|
+
// false negative until that row is next rewritten). Moving cleanup to AFTER
|
|
347
|
+
// triggers would dodge the no-op but reintroduce the REPLACE-doesn't-fire-
|
|
348
|
+
// DELETE gap and a full-projection scan per write, so the guards stay
|
|
349
|
+
// BEFORE. Syncular's own mirror writes use an unconditional
|
|
350
|
+
// `INSERT … ON CONFLICT (pk) DO UPDATE` (apply.ts); the image copy filters
|
|
351
|
+
// held rows at the same or a newer version out of its SELECT instead of a
|
|
352
|
+
// `DO UPDATE ... WHERE`. Only hand-written `OR IGNORE` against a mirror
|
|
353
|
+
// table is exposed.
|
|
345
354
|
for (const suffix of ['bi', 'ai', 'ad', 'au', 'bu']) {
|
|
346
355
|
db.exec(`DROP TRIGGER IF EXISTS ${quoteIdent(`${index.name}_${suffix}`)}`);
|
|
347
356
|
}
|
|
@@ -380,10 +389,16 @@ function createFtsProjection(db, table, index) {
|
|
|
380
389
|
const updateGuardBody = displacedByUnique.map(deleteDisplaced).join('; ');
|
|
381
390
|
createTrigger('bu', 'BEFORE UPDATE', updateGuardBody, updateGuardCondition);
|
|
382
391
|
}
|
|
383
|
-
|
|
392
|
+
// Every source row has exactly one mapping row, so a count mismatch marks a
|
|
393
|
+
// projection an earlier release damaged; the open rebuilds it.
|
|
394
|
+
const stale = !existed ||
|
|
395
|
+
(mapped &&
|
|
396
|
+
Number(db.query(`SELECT (SELECT count(*) FROM ${mapping})=(SELECT count(*) FROM ${sourceTable}) AS whole`)[0]?.whole) !== 1);
|
|
397
|
+
if (stale) {
|
|
398
|
+
db.exec(`DELETE FROM ${projection}`);
|
|
384
399
|
db.exec(`INSERT INTO ${projection}(${projectionColumns})SELECT ${sourceId},${indexedColumns.join(', ')} FROM ${sourceTable}`);
|
|
385
400
|
}
|
|
386
|
-
if (!mapped ||
|
|
401
|
+
if (!mapped || stale) {
|
|
387
402
|
db.exec(`DELETE FROM ${mapping}`);
|
|
388
403
|
db.exec(`INSERT INTO ${mapping}(id,source_id)SELECT rowid,${quoteIdent(FTS_SOURCE_ID_COLUMN)} FROM ${projection}`);
|
|
389
404
|
}
|
|
@@ -438,7 +453,7 @@ export function ensureLocalBookkeepingSchema(db) {
|
|
|
438
453
|
createTable(BLOB_COMMIT_REFS, 'commit_id TEXT NOT NULL,blob_id TEXT NOT NULL,PRIMARY KEY(commit_id,blob_id)');
|
|
439
454
|
db.exec(`CREATE INDEX IF NOT EXISTS ${BLOB_COMMIT_REFS}_body ON ${BLOB_COMMIT_REFS}(blob_id)`);
|
|
440
455
|
db.exec(`INSERT OR IGNORE INTO ${META_TABLE}(key,value)VALUES('localRevision','0')`);
|
|
441
|
-
const outboxExisted = db
|
|
456
|
+
const outboxExisted = tableExists(db, OUTBOX_TABLE);
|
|
442
457
|
createTable(OUTBOX_TABLE, 'seq INTEGER PRIMARY KEY AUTOINCREMENT,client_commit_id TEXT NOT NULL UNIQUE,created_at_ms INTEGER NOT NULL,operations TEXT NOT NULL');
|
|
443
458
|
// §7.1: `outboxId` names the outbox sequence. A new outbox table starts
|
|
444
459
|
// a new sequence, so it replaces the id in the same transaction.
|
|
@@ -481,7 +496,7 @@ export function ensureLocalBookkeepingSchema(db) {
|
|
|
481
496
|
// rows and pending evictions and registers its units fresh. Its
|
|
482
497
|
// subscriptions, cursors, echoes, resume tokens and window entries go.
|
|
483
498
|
const legacyEvictions = '_syncular_window_pending_evict';
|
|
484
|
-
if (db
|
|
499
|
+
if (tableExists(db, legacyEvictions)) {
|
|
485
500
|
if (db.query(`SELECT 1 FROM ${legacyEvictions} WHERE NOT json_valid(effective_scopes) OR json_type(effective_scopes)<>'object' OR EXISTS(SELECT 1 FROM json_each(effective_scopes) WHERE json_type(value)<>'array' OR EXISTS(SELECT 1 FROM json_each(value) v WHERE v.type<>'text'))`).length > 0)
|
|
486
501
|
throw new ClientSyncError('sync.local_corrupt', 'a version 3 pending eviction cannot be upgraded');
|
|
487
502
|
db.exec(`INSERT OR REPLACE INTO ${PENDING_EVICTIONS} SELECT sub_id,tbl,effective_scopes FROM ${legacyEvictions}`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@syncular/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.79.0",
|
|
4
4
|
"description": "Syncular TypeScript client core — offline-first sync over SQLite (WASM/OPFS, Bun, Node)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"author": "Benjamin Kniffler",
|
|
@@ -121,11 +121,11 @@
|
|
|
121
121
|
},
|
|
122
122
|
"dependencies": {
|
|
123
123
|
"@sqlite.org/sqlite-wasm": "^3.53.0-build1",
|
|
124
|
-
"@syncular/core": "0.
|
|
124
|
+
"@syncular/core": "0.79.0"
|
|
125
125
|
},
|
|
126
126
|
"devDependencies": {
|
|
127
127
|
"@electric-sql/pglite": "^0.5.4",
|
|
128
|
-
"@syncular/server": "0.
|
|
128
|
+
"@syncular/server": "0.79.0",
|
|
129
129
|
"playwright": "1.63.0",
|
|
130
130
|
"vite": "8.3.4"
|
|
131
131
|
}
|
package/src/apply.ts
CHANGED
|
@@ -680,7 +680,7 @@ export async function applySqliteSegment(
|
|
|
680
680
|
);
|
|
681
681
|
for (;;) {
|
|
682
682
|
db.exec(
|
|
683
|
-
`INSERT INTO ${quoteIdent(table.name)}(${insertNames.join(', ')})SELECT ${[...names, quoteIdent('_syncular_version')].join(', ')} FROM ${source} WHERE ${where}
|
|
683
|
+
`INSERT INTO ${quoteIdent(table.name)}(${insertNames.join(', ')})SELECT ${[...names, quoteIdent('_syncular_version')].join(', ')} FROM ${source} AS image WHERE ${where} AND NOT EXISTS(SELECT 1 FROM ${quoteIdent(table.name)} AS held WHERE held.${primaryKey}=image.${primaryKey} AND held.${version}>=image._syncular_version) ON CONFLICT(${primaryKey})DO UPDATE SET ${updates}`,
|
|
684
684
|
params,
|
|
685
685
|
);
|
|
686
686
|
const copied = Number(
|
package/src/blob.ts
CHANGED
|
@@ -10,7 +10,7 @@ import { type BlobRef, parseBlobRef, serializeBlobRef } from '@syncular/core';
|
|
|
10
10
|
import type { ClientDatabase } from './database';
|
|
11
11
|
import { ClientSyncError } from './errors';
|
|
12
12
|
import type { CompiledClientSchema } from './schema';
|
|
13
|
-
import { quoteIdent } from './schema';
|
|
13
|
+
import { quoteIdent, tableExists } from './schema';
|
|
14
14
|
|
|
15
15
|
/**
|
|
16
16
|
* A blob download result (§5.9.5). The authorized endpoint either serves the
|
|
@@ -121,10 +121,7 @@ export async function computeBlobId(bytes: Uint8Array): Promise<string> {
|
|
|
121
121
|
}
|
|
122
122
|
|
|
123
123
|
export function ensureBlobSchema(db: ClientDatabase): void {
|
|
124
|
-
|
|
125
|
-
"SELECT 1 FROM sqlite_master WHERE type='table' AND name='_syncular_blobs'",
|
|
126
|
-
);
|
|
127
|
-
if (existing.length > 0) {
|
|
124
|
+
if (tableExists(db, '_syncular_blobs')) {
|
|
128
125
|
const columns = db
|
|
129
126
|
.query('PRAGMA table_info(_syncular_blobs)')
|
|
130
127
|
.map((row) => row.name)
|
package/src/previous-version.ts
CHANGED
|
@@ -41,6 +41,7 @@ import {
|
|
|
41
41
|
localColumnType,
|
|
42
42
|
quoteIdent,
|
|
43
43
|
rowValueToJson,
|
|
44
|
+
tableExists,
|
|
44
45
|
} from './schema';
|
|
45
46
|
import { getMeta, setMeta, META_TABLE } from './state';
|
|
46
47
|
|
|
@@ -449,11 +450,7 @@ export function withPreviousVersionContainer<T>(
|
|
|
449
450
|
export function readPreviousVersionContainer(
|
|
450
451
|
db: ClientDatabase,
|
|
451
452
|
): PreviousVersionRecord | undefined {
|
|
452
|
-
|
|
453
|
-
"SELECT 1 AS present FROM sqlite_master WHERE type='table' AND name=?",
|
|
454
|
-
[CONTAINER_META_TABLE],
|
|
455
|
-
);
|
|
456
|
-
if (meta.length === 0) return undefined;
|
|
453
|
+
if (!tableExists(db, CONTAINER_META_TABLE)) return undefined;
|
|
457
454
|
const raw = db.query(
|
|
458
455
|
`SELECT record FROM ${quoteIdent(CONTAINER_META_TABLE)} WHERE id=1`,
|
|
459
456
|
)[0]?.record;
|
package/src/schema.ts
CHANGED
|
@@ -356,6 +356,14 @@ export function quoteIdent(name: string): string {
|
|
|
356
356
|
return `"${name.replaceAll('"', '""')}"`;
|
|
357
357
|
}
|
|
358
358
|
|
|
359
|
+
export function tableExists(db: ClientDatabase, name: string): boolean {
|
|
360
|
+
return (
|
|
361
|
+
db.query("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?", [
|
|
362
|
+
name,
|
|
363
|
+
]).length > 0
|
|
364
|
+
);
|
|
365
|
+
}
|
|
366
|
+
|
|
359
367
|
/**
|
|
360
368
|
* §5.11: the app-side type of a column for local (plaintext) storage. For an
|
|
361
369
|
* encrypted column this is `declaredType` — the local mirror stays plaintext,
|
|
@@ -445,8 +453,11 @@ const FTS_SOURCE_ID_COLUMN = '_syncular_source_id';
|
|
|
445
453
|
* Limitation: because a BEFORE trigger runs before SQLite resolves the
|
|
446
454
|
* conflict, an `OR IGNORE` write whose row is dropped still executes these
|
|
447
455
|
* displacement DELETEs, transiently removing the surviving row's projection
|
|
448
|
-
* entry.
|
|
449
|
-
*
|
|
456
|
+
* entry. The same holds for an upsert whose `DO UPDATE ... WHERE` skips the
|
|
457
|
+
* row, so Syncular's mirror writes never offer a row that will not replace
|
|
458
|
+
* (§5.3); see the guard-block comment below for the full rationale. An open
|
|
459
|
+
* rebuilds a projection whose mapping holds a different row count than its
|
|
460
|
+
* source table.
|
|
450
461
|
*/
|
|
451
462
|
function createFtsProjection(
|
|
452
463
|
db: ClientDatabase,
|
|
@@ -456,11 +467,7 @@ function createFtsProjection(
|
|
|
456
467
|
const projection = quoteIdent(index.name);
|
|
457
468
|
const sourceTable = quoteIdent(table.name);
|
|
458
469
|
const primaryKey = quoteIdent(table.primaryKey);
|
|
459
|
-
const existed =
|
|
460
|
-
db.query(
|
|
461
|
-
"SELECT 1 AS present FROM sqlite_master WHERE type='table' AND name=?",
|
|
462
|
-
[index.name],
|
|
463
|
-
).length > 0;
|
|
470
|
+
const existed = tableExists(db, index.name);
|
|
464
471
|
const indexedColumns = index.columns.map(quoteIdent);
|
|
465
472
|
const tokenizer = index.tokenize.replaceAll("'", "''");
|
|
466
473
|
db.exec(
|
|
@@ -468,10 +475,7 @@ function createFtsProjection(
|
|
|
468
475
|
);
|
|
469
476
|
|
|
470
477
|
const mapping = quoteIdent(`_syncular_fts_${index.name}`);
|
|
471
|
-
const mapped =
|
|
472
|
-
db.query("SELECT 1 FROM sqlite_master WHERE type='table' AND name=?", [
|
|
473
|
-
`_syncular_fts_${index.name}`,
|
|
474
|
-
]).length > 0;
|
|
478
|
+
const mapped = tableExists(db, `_syncular_fts_${index.name}`);
|
|
475
479
|
db.exec(
|
|
476
480
|
`CREATE TABLE IF NOT EXISTS ${mapping}(id INTEGER PRIMARY KEY,source_id TEXT NOT NULL UNIQUE)`,
|
|
477
481
|
);
|
|
@@ -506,16 +510,19 @@ function createFtsProjection(
|
|
|
506
510
|
// `UPDATE OR REPLACE` needs no BEFORE guard: the new pk equals the displaced
|
|
507
511
|
// row's pk, so the AFTER UPDATE delete of the new source id already clears it.
|
|
508
512
|
//
|
|
509
|
-
// Known limitation
|
|
510
|
-
//
|
|
511
|
-
//
|
|
512
|
-
//
|
|
513
|
-
// SURVIVING row's projection entry (a
|
|
514
|
-
// rewritten). Moving cleanup to AFTER
|
|
515
|
-
// reintroduce the REPLACE-doesn't-fire-
|
|
516
|
-
// per write, so the guards stay
|
|
517
|
-
//
|
|
518
|
-
//
|
|
513
|
+
// Known limitation: a BEFORE trigger fires before SQLite resolves the
|
|
514
|
+
// conflict, and SQLite exposes no signal for the eventual resolution. So an
|
|
515
|
+
// `INSERT OR IGNORE` / `UPDATE OR IGNORE` whose row is dropped on conflict,
|
|
516
|
+
// and an upsert whose `DO UPDATE ... WHERE` skips the row, still run these
|
|
517
|
+
// displacement DELETEs, removing the SURVIVING row's projection entry (a
|
|
518
|
+
// false negative until that row is next rewritten). Moving cleanup to AFTER
|
|
519
|
+
// triggers would dodge the no-op but reintroduce the REPLACE-doesn't-fire-
|
|
520
|
+
// DELETE gap and a full-projection scan per write, so the guards stay
|
|
521
|
+
// BEFORE. Syncular's own mirror writes use an unconditional
|
|
522
|
+
// `INSERT … ON CONFLICT (pk) DO UPDATE` (apply.ts); the image copy filters
|
|
523
|
+
// held rows at the same or a newer version out of its SELECT instead of a
|
|
524
|
+
// `DO UPDATE ... WHERE`. Only hand-written `OR IGNORE` against a mirror
|
|
525
|
+
// table is exposed.
|
|
519
526
|
for (const suffix of ['bi', 'ai', 'ad', 'au', 'bu']) {
|
|
520
527
|
db.exec(`DROP TRIGGER IF EXISTS ${quoteIdent(`${index.name}_${suffix}`)}`);
|
|
521
528
|
}
|
|
@@ -568,12 +575,23 @@ function createFtsProjection(
|
|
|
568
575
|
createTrigger('bu', 'BEFORE UPDATE', updateGuardBody, updateGuardCondition);
|
|
569
576
|
}
|
|
570
577
|
|
|
571
|
-
|
|
578
|
+
// Every source row has exactly one mapping row, so a count mismatch marks a
|
|
579
|
+
// projection an earlier release damaged; the open rebuilds it.
|
|
580
|
+
const stale =
|
|
581
|
+
!existed ||
|
|
582
|
+
(mapped &&
|
|
583
|
+
Number(
|
|
584
|
+
db.query(
|
|
585
|
+
`SELECT (SELECT count(*) FROM ${mapping})=(SELECT count(*) FROM ${sourceTable}) AS whole`,
|
|
586
|
+
)[0]?.whole,
|
|
587
|
+
) !== 1);
|
|
588
|
+
if (stale) {
|
|
589
|
+
db.exec(`DELETE FROM ${projection}`);
|
|
572
590
|
db.exec(
|
|
573
591
|
`INSERT INTO ${projection}(${projectionColumns})SELECT ${sourceId},${indexedColumns.join(', ')} FROM ${sourceTable}`,
|
|
574
592
|
);
|
|
575
593
|
}
|
|
576
|
-
if (!mapped ||
|
|
594
|
+
if (!mapped || stale) {
|
|
577
595
|
db.exec(`DELETE FROM ${mapping}`);
|
|
578
596
|
db.exec(
|
|
579
597
|
`INSERT INTO ${mapping}(id,source_id)SELECT rowid,${quoteIdent(FTS_SOURCE_ID_COLUMN)} FROM ${projection}`,
|
|
@@ -649,10 +667,7 @@ export function ensureLocalBookkeepingSchema(db: ClientDatabase): void {
|
|
|
649
667
|
db.exec(
|
|
650
668
|
`INSERT OR IGNORE INTO ${META_TABLE}(key,value)VALUES('localRevision','0')`,
|
|
651
669
|
);
|
|
652
|
-
const outboxExisted =
|
|
653
|
-
db.query(
|
|
654
|
-
`SELECT 1 FROM sqlite_master WHERE type='table' AND name='${OUTBOX_TABLE}'`,
|
|
655
|
-
).length > 0;
|
|
670
|
+
const outboxExisted = tableExists(db, OUTBOX_TABLE);
|
|
656
671
|
createTable(
|
|
657
672
|
OUTBOX_TABLE,
|
|
658
673
|
'seq INTEGER PRIMARY KEY AUTOINCREMENT,client_commit_id TEXT NOT NULL UNIQUE,created_at_ms INTEGER NOT NULL,operations TEXT NOT NULL',
|
|
@@ -722,11 +737,7 @@ export function ensureLocalBookkeepingSchema(db: ClientDatabase): void {
|
|
|
722
737
|
// rows and pending evictions and registers its units fresh. Its
|
|
723
738
|
// subscriptions, cursors, echoes, resume tokens and window entries go.
|
|
724
739
|
const legacyEvictions = '_syncular_window_pending_evict';
|
|
725
|
-
if (
|
|
726
|
-
db.query(
|
|
727
|
-
`SELECT 1 FROM sqlite_master WHERE type='table' AND name='${legacyEvictions}'`,
|
|
728
|
-
).length > 0
|
|
729
|
-
) {
|
|
740
|
+
if (tableExists(db, legacyEvictions)) {
|
|
730
741
|
if (
|
|
731
742
|
db.query(
|
|
732
743
|
`SELECT 1 FROM ${legacyEvictions} WHERE NOT json_valid(effective_scopes) OR json_type(effective_scopes)<>'object' OR EXISTS(SELECT 1 FROM json_each(effective_scopes) WHERE json_type(value)<>'array' OR EXISTS(SELECT 1 FROM json_each(value) v WHERE v.type<>'text'))`,
|