Skip to content

Commit 8ac9f45

Browse files
committed
Test partial sync with data
1 parent 9df1e92 commit 8ac9f45

File tree

2 files changed

+37
-6
lines changed

2 files changed

+37
-6
lines changed

packages/powersync_core/lib/src/bucket_storage.dart

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,16 @@ class BucketStorage {
111111
}
112112
return r;
113113
}
114-
final bucketNames = [for (final c in checkpoint.checksums) c.bucket];
114+
final bucketNames = [
115+
for (final c in checkpoint.checksums)
116+
if (forPriority == null || c.priority <= forPriority) c.bucket
117+
];
115118

116119
await writeTransaction((tx) async {
117120
await tx.execute(
118121
"UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))",
119122
[checkpoint.lastOpId, jsonEncode(bucketNames)]);
120-
if (checkpoint.writeCheckpoint != null) {
123+
if (forPriority == null && checkpoint.writeCheckpoint != null) {
121124
await tx.execute(
122125
"UPDATE ps_buckets SET last_op = ? WHERE name = '\$local'",
123126
[checkpoint.writeCheckpoint]);

packages/powersync_core/test/in_memory_sync_test.dart

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,25 +168,46 @@ void main() {
168168

169169
final checksums = [
170170
for (var prio = 0; prio <= 3; prio++)
171-
BucketChecksum(bucket: 'prio$prio', priority: prio, checksum: 0)
171+
BucketChecksum(
172+
bucket: 'prio$prio', priority: prio, checksum: 10 + prio)
172173
];
173174
syncService.addLine({
174175
'checkpoint': Checkpoint(
175-
lastOpId: '0',
176+
lastOpId: '4',
176177
writeCheckpoint: null,
177178
checksums: checksums,
178179
)
179180
});
181+
var operationId = 1;
182+
183+
void addRow(int priority) {
184+
syncService.addLine({
185+
'data': {
186+
'bucket': 'prio$priority',
187+
'data': [
188+
{
189+
'checksum': priority + 10,
190+
'data': {'name': 'test', 'email': 'email'},
191+
'op': 'PUT',
192+
'op_id': '${operationId++}',
193+
'object_id': 'prio$priority',
194+
'object_type': 'customers'
195+
}
196+
]
197+
}
198+
});
199+
}
180200

181201
// Receiving the checkpoint sets the state to downloading
182202
await expectLater(
183203
status, emits(isSyncStatus(downloading: true, hasSynced: false)));
184204

185205
// Emit partial sync complete for each priority but the last.
186206
for (var prio = 0; prio < 3; prio++) {
207+
addRow(prio);
187208
syncService.addLine({
188209
'partial_checkpoint_complete': {
189-
'last_op_id': '0',
210+
'last_op_id': operationId.toString(),
190211
'priority': prio,
191212
}
192213
});
@@ -199,15 +220,22 @@ void main() {
199220
isTrue,
200221
)),
201222
);
223+
224+
await database.waitForFirstSync(priority: BucketPriority(prio));
225+
expect(await database.getAll('SELECT * FROM customers'),
226+
hasLength(prio + 1));
202227
}
203228

204229
// Complete the sync
230+
addRow(3);
205231
syncService.addLine({
206-
'checkpoint_complete': {'last_op_id': '0'}
232+
'checkpoint_complete': {'last_op_id': operationId.toString()}
207233
});
208234

209235
await expectLater(
210236
status, emits(isSyncStatus(downloading: false, hasSynced: true)));
237+
await database.waitForFirstSync();
238+
expect(await database.getAll('SELECT * FROM customers'), hasLength(4));
211239
});
212240

213241
test('remembers last partial sync state', () async {

0 commit comments

Comments
 (0)