Skip to content

Commit 657eda7

Browse files
Merge remote-tracking branch 'origin/main' into watches
2 parents c591299 + 48e7456 commit 657eda7

File tree

5 files changed

+40
-4
lines changed

5 files changed

+40
-4
lines changed

.changeset/chilled-birds-cheer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/web': patch
3+
---
4+
5+
Use `BEGIN EXCLUSIVE` to open write transactions

.changeset/tall-dolphins-sleep.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
'@powersync/common': patch
3+
'@powersync/node': patch
4+
'@powersync/web': patch
5+
'@powersync/react-native': patch
6+
---
7+
8+
Rust sync client: Fix reported `lastSyncedAt` values in sync status.

packages/common/src/client/sync/stream/AbstractStreamingSyncImplementation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -941,7 +941,7 @@ The next upload iteration will be delayed.`);
941941
return {
942942
priority: status.priority,
943943
hasSynced: status.has_synced ?? undefined,
944-
lastSyncedAt: status?.last_synced_at != null ? new Date(status!.last_synced_at!) : undefined
944+
lastSyncedAt: status?.last_synced_at != null ? new Date(status!.last_synced_at! * 1000) : undefined
945945
};
946946
}
947947

packages/node/tests/sync.test.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,29 @@ function defineSyncTests(impl: SyncClientImplementation) {
112112
connectionMethod: SyncStreamConnectionMethod.HTTP
113113
};
114114

115+
mockSyncServiceTest('sets last sync time', async ({ syncService }) => {
116+
const db = await syncService.createDatabase();
117+
db.connect(new TestConnector(), options);
118+
await vi.waitFor(() => expect(syncService.connectedListeners).toHaveLength(1));
119+
120+
syncService.pushLine({
121+
checkpoint: {
122+
last_op_id: '0',
123+
buckets: []
124+
}
125+
});
126+
syncService.pushLine({ checkpoint_complete: { last_op_id: '0' } });
127+
const now = Date.now();
128+
129+
await db.waitForFirstSync();
130+
const status = db.currentStatus;
131+
const lastSyncedAt = status.lastSyncedAt!.getTime();
132+
133+
// The reported time of the last sync should be close to the current time (5s is very generous already, but we've
134+
// had an issue where dates weren't parsed correctly and we were off by decades).
135+
expect(Math.abs(lastSyncedAt - now)).toBeLessThan(5000);
136+
});
137+
115138
describe('reports progress', () => {
116139
let lastOpId = 0;
117140

packages/web/src/db/adapters/LockedAsyncDatabaseAdapter.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export class LockedAsyncDatabaseAdapter
235235
}
236236

237237
writeTransaction<T>(fn: (tx: Transaction) => Promise<T>, options?: DBLockOptions | undefined): Promise<T> {
238-
return this.writeLock(this.wrapTransaction(fn));
238+
return this.writeLock(this.wrapTransaction(fn, true));
239239
}
240240

241241
private generateDBHelpers<
@@ -279,9 +279,9 @@ export class LockedAsyncDatabaseAdapter
279279
/**
280280
* Wraps a lock context into a transaction context
281281
*/
282-
private wrapTransaction<T>(cb: (tx: Transaction) => Promise<T>) {
282+
private wrapTransaction<T>(cb: (tx: Transaction) => Promise<T>, write = false) {
283283
return async (tx: LockContext): Promise<T> => {
284-
await this._execute('BEGIN TRANSACTION');
284+
await this._execute(write ? 'BEGIN EXCLUSIVE' : 'BEGIN');
285285
let finalized = false;
286286
const commit = async (): Promise<QueryResult> => {
287287
if (finalized) {

0 commit comments

Comments
 (0)