From 8ac9f45c9fcf351b3fca1f142bcee14f1e97418b Mon Sep 17 00:00:00 2001 From: Simon Binder Date: Wed, 19 Feb 2025 12:31:41 +0100 Subject: [PATCH] Test partial sync with data --- .../lib/src/bucket_storage.dart | 7 ++-- .../test/in_memory_sync_test.dart | 36 ++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) diff --git a/packages/powersync_core/lib/src/bucket_storage.dart b/packages/powersync_core/lib/src/bucket_storage.dart index 344c92ee..d79e4c05 100644 --- a/packages/powersync_core/lib/src/bucket_storage.dart +++ b/packages/powersync_core/lib/src/bucket_storage.dart @@ -111,13 +111,16 @@ class BucketStorage { } return r; } - final bucketNames = [for (final c in checkpoint.checksums) c.bucket]; + final bucketNames = [ + for (final c in checkpoint.checksums) + if (forPriority == null || c.priority <= forPriority) c.bucket + ]; await writeTransaction((tx) async { await tx.execute( "UPDATE ps_buckets SET last_op = ? WHERE name IN (SELECT json_each.value FROM json_each(?))", [checkpoint.lastOpId, jsonEncode(bucketNames)]); - if (checkpoint.writeCheckpoint != null) { + if (forPriority == null && checkpoint.writeCheckpoint != null) { await tx.execute( "UPDATE ps_buckets SET last_op = ? WHERE name = '\$local'", [checkpoint.writeCheckpoint]); diff --git a/packages/powersync_core/test/in_memory_sync_test.dart b/packages/powersync_core/test/in_memory_sync_test.dart index c7636de3..3cb0e232 100644 --- a/packages/powersync_core/test/in_memory_sync_test.dart +++ b/packages/powersync_core/test/in_memory_sync_test.dart @@ -168,15 +168,35 @@ void main() { final checksums = [ for (var prio = 0; prio <= 3; prio++) - BucketChecksum(bucket: 'prio$prio', priority: prio, checksum: 0) + BucketChecksum( + bucket: 'prio$prio', priority: prio, checksum: 10 + prio) ]; syncService.addLine({ 'checkpoint': Checkpoint( - lastOpId: '0', + lastOpId: '4', writeCheckpoint: null, checksums: checksums, ) }); + var operationId = 1; + + void addRow(int priority) { + syncService.addLine({ + 'data': { + 'bucket': 'prio$priority', + 'data': [ + { + 'checksum': priority + 10, + 'data': {'name': 'test', 'email': 'email'}, + 'op': 'PUT', + 'op_id': '${operationId++}', + 'object_id': 'prio$priority', + 'object_type': 'customers' + } + ] + } + }); + } // Receiving the checkpoint sets the state to downloading await expectLater( @@ -184,9 +204,10 @@ void main() { // Emit partial sync complete for each priority but the last. for (var prio = 0; prio < 3; prio++) { + addRow(prio); syncService.addLine({ 'partial_checkpoint_complete': { - 'last_op_id': '0', + 'last_op_id': operationId.toString(), 'priority': prio, } }); @@ -199,15 +220,22 @@ void main() { isTrue, )), ); + + await database.waitForFirstSync(priority: BucketPriority(prio)); + expect(await database.getAll('SELECT * FROM customers'), + hasLength(prio + 1)); } // Complete the sync + addRow(3); syncService.addLine({ - 'checkpoint_complete': {'last_op_id': '0'} + 'checkpoint_complete': {'last_op_id': operationId.toString()} }); await expectLater( status, emits(isSyncStatus(downloading: false, hasSynced: true))); + await database.waitForFirstSync(); + expect(await database.getAll('SELECT * FROM customers'), hasLength(4)); }); test('remembers last partial sync state', () async {