Skip to content

Commit 9ca3ea7

Browse files
authored
Merge pull request #459 from powersync-ja/fix-write-checkpoint-handling
Avoid deleting the $local bucket on connect()
2 parents 3f8f748 + 7a47778 commit 9ca3ea7

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

.changeset/moody-shoes-remain.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/common': patch
3+
---
4+
5+
Fix issue where local changes could be reverted when a replication delay is present.

packages/common/src/client/AbstractPowerSyncDatabase.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,12 +582,12 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
582582
if (writeCheckpoint) {
583583
const check = await tx.execute(`SELECT 1 FROM ${PSInternalTable.CRUD} LIMIT 1`);
584584
if (!check.rows?.length) {
585-
await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = ? WHERE name='$local'`, [
585+
await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
586586
writeCheckpoint
587587
]);
588588
}
589589
} else {
590-
await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = ? WHERE name='$local'`, [
590+
await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
591591
this.bucketStorageAdapter.getMaxOpId()
592592
]);
593593
}

packages/common/src/client/sync/bucket/SqliteBucketStorage.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class SqliteBucketStorage extends BaseObserver<BucketStorageListener> imp
8585

8686
async getBucketStates(): Promise<BucketState[]> {
8787
const result = await this.db.getAll<BucketState>(
88-
'SELECT name as bucket, cast(last_op as TEXT) as op_id FROM ps_buckets WHERE pending_delete = 0'
88+
"SELECT name as bucket, cast(last_op as TEXT) as op_id FROM ps_buckets WHERE pending_delete = 0 AND name != '$local'"
8989
);
9090
return result;
9191
}
@@ -249,9 +249,10 @@ export class SqliteBucketStorage extends BaseObserver<BucketStorageListener> imp
249249
}
250250

251251
async updateLocalTarget(cb: () => Promise<string>): Promise<boolean> {
252-
const rs1 = await this.db.getAll("SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = ?", [
253-
MAX_OP_ID
254-
]);
252+
const rs1 = await this.db.getAll(
253+
"SELECT target_op FROM ps_buckets WHERE name = '$local' AND target_op = CAST(? as INTEGER)",
254+
[MAX_OP_ID]
255+
);
255256
if (!rs1.length) {
256257
// Nothing to update
257258
return false;

0 commit comments

Comments
 (0)