From 9e9ae46dda605ec0769693991421a42ac7448fa4 Mon Sep 17 00:00:00 2001 From: stevensJourney Date: Tue, 13 May 2025 09:52:08 +0200 Subject: [PATCH 1/4] Add default argument for writeCheckpoint --- CHANGELOG.md | 11 ++++++++++ Sources/PowerSync/Protocol/db/CrudBatch.swift | 11 ++++++++++ Tests/PowerSyncTests/CrudTests.swift | 20 +++++++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 68b5b87..a03e887 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,16 @@ # Changelog +## 1.1.1 (unreleased) + +* Added default `nil` value for `writeCheckpoint` parameter when calling `CrudBatch.complete()`. Developers no longer need to specify `nil` as an argument +``` diff +guard let finalBatch = try await powersync.getCrudBatch(limit: 100) else { + return nil +} +- try await batch.complete(writeCheckpoint: nil) ++ try await batch.complete() +``` + ## 1.1.0 * Add sync progress information through `SyncStatusData.downloadProgress`. diff --git a/Sources/PowerSync/Protocol/db/CrudBatch.swift b/Sources/PowerSync/Protocol/db/CrudBatch.swift index 6fb7fe8..cfcea74 100644 --- a/Sources/PowerSync/Protocol/db/CrudBatch.swift +++ b/Sources/PowerSync/Protocol/db/CrudBatch.swift @@ -13,3 +13,14 @@ public protocol CrudBatch { /// `writeCheckpoint` is optional. func complete(writeCheckpoint: String?) async throws } + +public extension CrudBatch { + /// Call to remove the changes from the local queue, once successfully uploaded. + /// + /// `writeCheckpoint` is optional. + func complete(writeCheckpoint: String? = nil) async throws { + try await self.complete( + writeCheckpoint: writeCheckpoint + ) + } +} diff --git a/Tests/PowerSyncTests/CrudTests.swift b/Tests/PowerSyncTests/CrudTests.swift index c76d0c8..5b303d6 100644 --- a/Tests/PowerSyncTests/CrudTests.swift +++ b/Tests/PowerSyncTests/CrudTests.swift @@ -178,5 +178,25 @@ final class CrudTests: XCTestCase { } XCTAssertNil(afterCompleteBatch) + + try await database.writeTransaction { tx in + for i in 0 ..< 100 { + try tx.execute( + sql: "INSERT INTO users (id, name, email, favorite_number) VALUES (uuid(), 'a', 'a@example.com', ?)", + parameters: [i] + ) + } + } + + guard let finalBatch = try await database.getCrudBatch(limit: 100) else { + return XCTFail("Failed to get crud batch") + } + XCTAssert(finalBatch.crud.count == 100) + XCTAssert(finalBatch.hasMore == false) + // Calling complete without a writeCheckpoint param should be possible + try await finalBatch.complete() + + let finalValidationBatch = try await database.getCrudBatch(limit: 100) + XCTAssertNil(finalValidationBatch) } } From 880c6b4122576a66b1e421777c28235a45a869ab Mon Sep 17 00:00:00 2001 From: stevensJourney Date: Tue, 13 May 2025 10:02:47 +0200 Subject: [PATCH 2/4] cleanup Crud extensions --- Sources/PowerSync/Protocol/db/CrudBatch.swift | 6 ++---- Sources/PowerSync/Protocol/db/CrudTransaction.swift | 6 ++---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/Sources/PowerSync/Protocol/db/CrudBatch.swift b/Sources/PowerSync/Protocol/db/CrudBatch.swift index cfcea74..c57b917 100644 --- a/Sources/PowerSync/Protocol/db/CrudBatch.swift +++ b/Sources/PowerSync/Protocol/db/CrudBatch.swift @@ -16,11 +16,9 @@ public protocol CrudBatch { public extension CrudBatch { /// Call to remove the changes from the local queue, once successfully uploaded. - /// - /// `writeCheckpoint` is optional. - func complete(writeCheckpoint: String? = nil) async throws { + func complete() async throws { try await self.complete( - writeCheckpoint: writeCheckpoint + writeCheckpoint: nil ) } } diff --git a/Sources/PowerSync/Protocol/db/CrudTransaction.swift b/Sources/PowerSync/Protocol/db/CrudTransaction.swift index 3ce8147..eadc954 100644 --- a/Sources/PowerSync/Protocol/db/CrudTransaction.swift +++ b/Sources/PowerSync/Protocol/db/CrudTransaction.swift @@ -18,11 +18,9 @@ public protocol CrudTransaction { public extension CrudTransaction { /// Call to remove the changes from the local queue, once successfully uploaded. - /// - /// `writeCheckpoint` is optional. - func complete(writeCheckpoint: String? = nil) async throws { + func complete() async throws { try await self.complete( - writeCheckpoint: writeCheckpoint + writeCheckpoint: nil ) } } From 059deec504c4f6be3ccd283485c8b3fcd4330b04 Mon Sep 17 00:00:00 2001 From: stevensJourney Date: Tue, 13 May 2025 10:08:46 +0200 Subject: [PATCH 3/4] cleanup changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a03e887..e6ca3d7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.1.1 (unreleased) -* Added default `nil` value for `writeCheckpoint` parameter when calling `CrudBatch.complete()`. Developers no longer need to specify `nil` as an argument +* Improved `CrudBatch` and `CrudTransaction` `complete` function extensions. Developers no longer need to specify `nil` as an argument for `writeCheckpoint` when calling `CrudBatch.complete`. The base `complete` functions still accepts an optional `writeCheckpoint` argument if developers use custom write checkpoints. ``` diff guard let finalBatch = try await powersync.getCrudBatch(limit: 100) else { return nil From 623704cf833c690eee231126d2fafba0e8c2a637 Mon Sep 17 00:00:00 2001 From: stevensJourney Date: Tue, 13 May 2025 10:09:19 +0200 Subject: [PATCH 4/4] fix typo --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e6ca3d7..c13e378 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.1.1 (unreleased) -* Improved `CrudBatch` and `CrudTransaction` `complete` function extensions. Developers no longer need to specify `nil` as an argument for `writeCheckpoint` when calling `CrudBatch.complete`. The base `complete` functions still accepts an optional `writeCheckpoint` argument if developers use custom write checkpoints. +* Improved `CrudBatch` and `CrudTransaction` `complete` function extensions. Developers no longer need to specify `nil` as an argument for `writeCheckpoint` when calling `CrudBatch.complete`. The base `complete` functions still accept an optional `writeCheckpoint` argument if developers use custom write checkpoints. ``` diff guard let finalBatch = try await powersync.getCrudBatch(limit: 100) else { return nil