Skip to content

Commit

Permalink
Merge pull request #459 from powersync-ja/fix-write-checkpoint-handling
Browse files Browse the repository at this point in the history
Avoid deleting the $local bucket on connect()
  • Loading branch information
rkistner authored Jan 6, 2025
2 parents 3f8f748 + 7a47778 commit 9ca3ea7
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/moody-shoes-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@powersync/common': patch
---

Fix issue where local changes could be reverted when a replication delay is present.
4 changes: 2 additions & 2 deletions packages/common/src/client/AbstractPowerSyncDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -582,12 +582,12 @@ export abstract class AbstractPowerSyncDatabase extends BaseObserver<PowerSyncDB
if (writeCheckpoint) {
const check = await tx.execute(`SELECT 1 FROM ${PSInternalTable.CRUD} LIMIT 1`);
if (!check.rows?.length) {
await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = ? WHERE name='$local'`, [
await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
writeCheckpoint
]);
}
} else {
await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = ? WHERE name='$local'`, [
await tx.execute(`UPDATE ${PSInternalTable.BUCKETS} SET target_op = CAST(? as INTEGER) WHERE name='$local'`, [
this.bucketStorageAdapter.getMaxOpId()
]);
}
Expand Down
9 changes: 5 additions & 4 deletions packages/common/src/client/sync/bucket/SqliteBucketStorage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export class SqliteBucketStorage extends BaseObserver<BucketStorageListener> imp

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

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

0 comments on commit 9ca3ea7

Please sign in to comment.