Skip to content

Commit ebacd30

Browse files
committed
Update PowerSync Kotlin and core extension
1 parent 82880a4 commit ebacd30

File tree

7 files changed

+46
-16
lines changed

7 files changed

+46
-16
lines changed

Package.resolved

Lines changed: 4 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ if let kotlinSdkPath = localKotlinSdkOverride {
3838
// Not using a local build, so download from releases
3939
conditionalTargets.append(.binaryTarget(
4040
name: "PowerSyncKotlin",
41-
url: "https://github.com/powersync-ja/powersync-kotlin/releases/download/v1.7.0/PowersyncKotlinRelease.zip",
42-
checksum: "836ac106c26a184c10373c862745d9af195737ad01505bb965f197797aa88535"
41+
url: "https://github.com/powersync-ja/powersync-kotlin/releases/download/v1.8.0/PowersyncKotlinRelease.zip",
42+
checksum: "31ac7c5e11d747e11bceb0b34f30438d37033e700c621b0a468aa308d887587f"
4343
))
4444
}
4545

@@ -51,7 +51,7 @@ if let corePath = localCoreExtension {
5151
// Not using a local build, so download from releases
5252
conditionalDependencies.append(.package(
5353
url: "https://github.com/powersync-ja/powersync-sqlite-core-swift.git",
54-
exact: "0.4.6"
54+
exact: "0.4.8"
5555
))
5656
}
5757

Sources/PowerSync/Kotlin/KotlinAdapter.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ enum KotlinAdapter {
4949
return PowerSyncKotlin.RawTable(
5050
name: table.name,
5151
put: translateStatement(table.put),
52-
delete: translateStatement(table.delete)
52+
delete: translateStatement(table.delete),
53+
clear: table.clear,
5354
);
5455
}
5556

Sources/PowerSync/Kotlin/KotlinPowerSyncDatabaseImpl.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ final class KotlinPowerSyncDatabaseImpl: PowerSyncDatabaseProtocol,
8989

9090
func disconnectAndClear(clearLocal: Bool = true) async throws {
9191
try await kotlinDatabase.disconnectAndClear(
92-
clearLocal: clearLocal
92+
clearLocal: clearLocal,
93+
soft: false
9394
)
9495
}
9596

Sources/PowerSync/Protocol/PowerSyncDatabaseProtocol.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,20 @@ public protocol PowerSyncDatabaseProtocol: Queries, Sendable {
216216
func disconnect() async throws
217217

218218
/// Disconnect and clear the database.
219-
/// Use this when logging out.
220-
/// The database can still be queried after this is called, but the tables
221-
/// would be empty.
222219
///
223-
/// - Parameter clearLocal: Set to false to preserve data in local-only tables. Defaults to `true`.
224-
func disconnectAndClear(clearLocal: Bool) async throws
220+
/// Clearing the database is useful when a user logs out, to ensure another user logging in later would not see
221+
/// previous data.
222+
///
223+
/// The database can still be queried after this is called, but the tables would be empty.
224+
///
225+
/// To perserve data in local-only tables, set `clearLocal` to `false`.
226+
///
227+
/// A `soft` clear deletes publicly visible data, but keeps internal copies of data synced in the database. This
228+
/// usually means that if the same user logs out and back in again, the first sync is very fast because all internal
229+
/// data is still available. When a different user logs in, no old data would be visible at any point.
230+
/// Using soft clears is recommended where it's not a security issue that old data could be reconstructed from
231+
/// the database.
232+
func disconnectAndClear(clearLocal: Bool, soft: Bool) async throws
225233

226234
/// Close the database, releasing resources.
227235
/// Also disconnects any active connection.
@@ -289,8 +297,8 @@ public extension PowerSyncDatabaseProtocol {
289297
)
290298
}
291299

292-
func disconnectAndClear() async throws {
293-
try await disconnectAndClear(clearLocal: true)
300+
func disconnectAndClear(clearLocal: Bool = true, soft: Bool = false) async throws {
301+
try await disconnectAndClear(clearLocal: clearLocal, soft: soft)
294302
}
295303

296304
func getCrudBatch(limit: Int32 = 100) async throws -> CrudBatch? {

Sources/PowerSync/Protocol/Schema/RawTable.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@ public struct RawTable: BaseTableProtocol {
2424

2525
/// The statement to run when the sync client has to delete a row.
2626
public let delete: PendingStatement
27+
28+
/// An optional statement to run when the database is cleared.
29+
public let clear: String?
2730

28-
public init(name: String, put: PendingStatement, delete: PendingStatement) {
31+
public init(name: String, put: PendingStatement, delete: PendingStatement, clear: String? = nil) {
2932
self.name = name
3033
self.put = put
3134
self.delete = delete
35+
self.clear = clear
3236
}
3337
}
3438

Tests/PowerSyncTests/CrudTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,4 +238,19 @@ final class CrudTests: XCTestCase {
238238
let finalTx = try await database.getNextCrudTransaction()
239239
XCTAssertEqual(finalTx!.crud.count, 15)
240240
}
241+
242+
func testSoftClear() async throws {
243+
try await database.execute(sql: "INSERT INTO users (id, name) VALUES (uuid(), ?)", parameters: ["test"]);
244+
try await database.execute(sql: "INSERT INTO ps_buckets (name, last_applied_op) VALUES (?, ?)", parameters: ["bkt", 10])
245+
246+
// Doing a soft-clear should delete data but keep the bucket around.
247+
try await database.disconnectAndClear(soft: true)
248+
let entries = try await database.getAll("SELECT name FROM ps_buckets", mapper: { cursor in try cursor.getString(index: 0) })
249+
XCTAssertEqual(entries.count, 1)
250+
251+
// Doing a default clear also deletes buckets.
252+
try await database.disconnectAndClear();
253+
let newEntries = try await database.getAll("SELECT name FROM ps_buckets", mapper: { cursor in try cursor.getString(index: 0) })
254+
XCTAssertEqual(newEntries.count, 0)
255+
}
241256
}

0 commit comments

Comments
 (0)