From 4ead569fb9d93ab3aa6250098ddb1f769c40313b Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Mon, 13 Jan 2025 11:36:54 +0200 Subject: [PATCH 1/4] feat: expose close method --- .../commonMain/kotlin/com/powersync/PowerSyncDatabase.kt | 8 ++++++++ .../kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt | 5 +++++ .../kotlin/com/powersync/db/internal/InternalDatabase.kt | 2 ++ .../com/powersync/db/internal/InternalDatabaseImpl.kt | 4 ++++ 4 files changed, 19 insertions(+) diff --git a/core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt b/core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt index 27c6adf4..888751e2 100644 --- a/core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt +++ b/core/src/commonMain/kotlin/com/powersync/PowerSyncDatabase.kt @@ -120,4 +120,12 @@ public interface PowerSyncDatabase : Queries { * To preserve data in local-only tables, set clearLocal to false. */ public suspend fun disconnectAndClear(clearLocal: Boolean = true) + + /** + * Close the database, releasing resources. + * Also disconnects any active connection. + * + * Once close is called, this database cannot be used again - a new one must be constructed. + */ + public suspend fun close() } diff --git a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt index edabc3bb..9db82c21 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/PowerSyncDatabaseImpl.kt @@ -314,6 +314,11 @@ internal class PowerSyncDatabaseImpl( } } + override suspend fun close() { + disconnect() + internalDb.close() + } + /** * Check that a supported version of the powersync extension is loaded. */ diff --git a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabase.kt b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabase.kt index d894e241..b8f549e0 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabase.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabase.kt @@ -14,4 +14,6 @@ internal interface InternalDatabase : Queries { fun getExistingTableNames(tableGlob: String): List fun updatesOnTable(tableName: String): Flow + + fun close() } diff --git a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt index 9c5fb369..956dd092 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt @@ -261,6 +261,10 @@ internal class InternalDatabaseImpl( return existingTableNames } + override fun close() { + this.driver.close() + } + internal data class ExplainQueryResult( val addr: String, val opcode: String, From 9a12cd4faba8b1ab75c43092e97c530708288c8d Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Mon, 13 Jan 2025 11:48:18 +0200 Subject: [PATCH 2/4] chore: use Closeable instead of close function --- .../kotlin/com/powersync/db/internal/InternalDatabase.kt | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabase.kt b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabase.kt index b8f549e0..672dda4a 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabase.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabase.kt @@ -1,12 +1,13 @@ package com.powersync.db.internal +import app.cash.sqldelight.db.Closeable import com.persistence.PowersyncQueries import com.powersync.PsSqlDriver import com.powersync.db.Queries import com.powersync.persistence.PsDatabase import kotlinx.coroutines.flow.Flow -internal interface InternalDatabase : Queries { +internal interface InternalDatabase : Queries, Closeable { val driver: PsSqlDriver val transactor: PsDatabase val queries: PowersyncQueries @@ -14,6 +15,4 @@ internal interface InternalDatabase : Queries { fun getExistingTableNames(tableGlob: String): List fun updatesOnTable(tableName: String): Flow - - fun close() } From ba8c9d07dd862c3d6ee41c9c2fb991e6636d7f30 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Mon, 13 Jan 2025 11:59:13 +0200 Subject: [PATCH 3/4] chore: update version --- CHANGELOG.md | 4 ++++ .../db/internal/InternalDatabaseImpl.kt | 5 ++++- .../iosApp/iosApp.xcodeproj/project.pbxproj | 18 ++++++++++++++++++ gradle.properties | 2 +- 4 files changed, 27 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 79b5d4fd..d7e7c24d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.0.0-BETA16 + +* Add `close` method to database methods + ## 1.0.0-BETA15 * Update powersync-sqlite-core to 0.3.8 diff --git a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt index 956dd092..79544b9b 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt @@ -9,6 +9,7 @@ import app.cash.sqldelight.coroutines.mapToList import app.cash.sqldelight.db.QueryResult import app.cash.sqldelight.db.SqlCursor import app.cash.sqldelight.db.SqlPreparedStatement +import co.touchlab.kermit.Logger import com.persistence.PowersyncQueries import com.powersync.PsSqlDriver import com.powersync.persistence.PsDatabase @@ -189,7 +190,9 @@ internal class InternalDatabaseImpl( override suspend fun writeTransaction(callback: suspend (PowerSyncTransaction) -> R): R = transactor.transactionWithResult(noEnclosing = true) { - callback(transaction) + val res = callback(transaction) + Logger.i("writeTransaction completed") + res } // Register callback for table updates diff --git a/demos/hello-powersync/iosApp/iosApp.xcodeproj/project.pbxproj b/demos/hello-powersync/iosApp/iosApp.xcodeproj/project.pbxproj index 9481617e..1625063e 100644 --- a/demos/hello-powersync/iosApp/iosApp.xcodeproj/project.pbxproj +++ b/demos/hello-powersync/iosApp/iosApp.xcodeproj/project.pbxproj @@ -106,6 +106,7 @@ 7555FF79242A565900829871 /* Resources */, EF8A45596C10845FBC80C40F /* Frameworks */, BDEF1F2128374088F98D10FB /* [CP] Embed Pods Frameworks */, + 2A09661A10CA625857E1B999 /* [CP] Copy Pods Resources */, ); buildRules = ( ); @@ -162,6 +163,23 @@ /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ + 2A09661A10CA625857E1B999 /* [CP] Copy Pods Resources */ = { + isa = PBXShellScriptBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + inputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-resources-${CONFIGURATION}-input-files.xcfilelist", + ); + name = "[CP] Copy Pods Resources"; + outputFileListPaths = ( + "${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-resources-${CONFIGURATION}-output-files.xcfilelist", + ); + runOnlyForDeploymentPostprocessing = 0; + shellPath = /bin/sh; + shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-iosApp/Pods-iosApp-resources.sh\"\n"; + showEnvVarsInLog = 0; + }; BDEF1F2128374088F98D10FB /* [CP] Embed Pods Frameworks */ = { isa = PBXShellScriptBuildPhase; buildActionMask = 2147483647; diff --git a/gradle.properties b/gradle.properties index 65452f48..3fd4fbb0 100644 --- a/gradle.properties +++ b/gradle.properties @@ -17,7 +17,7 @@ development=true RELEASE_SIGNING_ENABLED=true # Library config GROUP=com.powersync -LIBRARY_VERSION=1.0.0-BETA15 +LIBRARY_VERSION=1.0.0-BETA16 GITHUB_REPO=https://github.com/powersync-ja/powersync-kotlin.git # POM POM_URL=https://github.com/powersync-ja/powersync-kotlin/ From 7454955e59bc66f3dca02325f3caeb0cb9ecd213 Mon Sep 17 00:00:00 2001 From: DominicGBauer Date: Tue, 14 Jan 2025 11:11:28 +0200 Subject: [PATCH 4/4] fix: remove log --- .../kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt index 79544b9b..956dd092 100644 --- a/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt +++ b/core/src/commonMain/kotlin/com/powersync/db/internal/InternalDatabaseImpl.kt @@ -9,7 +9,6 @@ import app.cash.sqldelight.coroutines.mapToList import app.cash.sqldelight.db.QueryResult import app.cash.sqldelight.db.SqlCursor import app.cash.sqldelight.db.SqlPreparedStatement -import co.touchlab.kermit.Logger import com.persistence.PowersyncQueries import com.powersync.PsSqlDriver import com.powersync.persistence.PsDatabase @@ -190,9 +189,7 @@ internal class InternalDatabaseImpl( override suspend fun writeTransaction(callback: suspend (PowerSyncTransaction) -> R): R = transactor.transactionWithResult(noEnclosing = true) { - val res = callback(transaction) - Logger.i("writeTransaction completed") - res + callback(transaction) } // Register callback for table updates