Skip to content

Commit 16684fc

Browse files
committed
Cleanup logic for dropping the destination table, ordering by operation_id. Using createDestination table helper.
1 parent 0ba0cea commit 16684fc

3 files changed

Lines changed: 24 additions & 17 deletions

File tree

‎packages/powersync-db-collection/package.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
"@powersync/common": "^1.41.0"
6363
},
6464
"devDependencies": {
65-
"@powersync/common": "0.0.0-dev-20260305124002",
66-
"@powersync/node": "0.0.0-dev-20260305124002",
65+
"@powersync/common": "0.0.0-dev-20260306125455",
66+
"@powersync/node": "0.0.0-dev-20260306125455",
6767
"@types/debug": "^4.1.12",
6868
"@vitest/coverage-istanbul": "^3.2.4"
6969
}

‎packages/powersync-db-collection/src/powersync.ts‎

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ export function powerSyncCollectionOptions<
370370
.writeTransaction(async (context) => {
371371
begin()
372372
const operations = await context.getAll<TriggerDiffRecord>(
373-
`SELECT * FROM ${trackedTableName} ORDER BY timestamp ASC`,
373+
`SELECT * FROM ${trackedTableName} ORDER BY operation_id ASC`,
374374
)
375375
const pendingOperations: Array<PendingOperation> = []
376376

@@ -440,8 +440,22 @@ export function powerSyncCollectionOptions<
440440
} else {
441441
abortController.signal.addEventListener(
442442
`abort`,
443-
() => {
444-
disposeTracking?.()
443+
async () => {
444+
await disposeTracking?.()
445+
446+
// In on-demand mode, we need to manually drop the destination table because we opt-out of internal management of the destination table.
447+
if (syncMode === 'on-demand') {
448+
try {
449+
await database.execute(
450+
`DROP TABLE IF EXISTS ${trackedTableName};`,
451+
)
452+
} catch (error) {
453+
database.logger.error(
454+
`Could not drop tracked table ${trackedTableName}`,
455+
error,
456+
)
457+
}
458+
}
445459
},
446460
{ once: true },
447461
)
@@ -513,17 +527,9 @@ export function powerSyncCollectionOptions<
513527
return
514528
}
515529

516-
await database.writeLock(async (context) => {
517-
await context.execute(`
518-
CREATE TEMP TABLE IF NOT EXISTS ${trackedTableName} (
519-
operation_id INTEGER PRIMARY KEY AUTOINCREMENT,
520-
id TEXT,
521-
operation TEXT,
522-
timestamp TEXT,
523-
value TEXT,
524-
previous_value TEXT
525-
)
526-
`)
530+
await database.triggers.createDiffDestinationTable(trackedTableName, {
531+
temporary: true,
532+
onlyIfNotExists: true,
527533
})
528534

529535
const combinedWhere =

‎packages/powersync-db-collection/tests/on-demand-sync.test.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,11 @@ describe(`On-Demand Sync Mode`, () => {
3333
schema: APP_SCHEMA,
3434
})
3535
onTestFinished(async () => {
36+
await db.disconnectAndClear()
37+
3638
// Wait a moment for any pending cleanup operations to complete
3739
// before closing the database to prevent "operation on closed remote" errors
3840
await new Promise((resolve) => setTimeout(resolve, 100))
39-
await db.disconnectAndClear()
4041
await db.close()
4142
})
4243
await db.disconnectAndClear()

0 commit comments

Comments
 (0)