From def4ba984ad1bf9cbe128ac30e4bd0b6fc596d90 Mon Sep 17 00:00:00 2001 From: Philip Wedemann <22521688+hfhbd@users.noreply.github.com> Date: Fri, 11 Aug 2023 10:18:32 +0200 Subject: [PATCH] Bump sqldelight to 2.0.0 (#771) * Update libs.versions.toml * Use web-worker driver --------- Co-authored-by: hfhbd --- .../kotlin/app/softwork/composetodo/Container.kt | 5 ++++- clients/build.gradle.kts | 8 +------- .../composetodo/repository/TodoRepository.kt | 3 ++- .../kotlin/app/softwork/composetodo/IosContainer.kt | 5 ++++- .../app/softwork/composetodo/DesktopContainer.kt | 3 ++- gradle/libs.versions.toml | 4 ++-- kotlin-js-store/yarn.lock | 13 +++++++++---- web/build.gradle.kts | 5 ++++- .../main/kotlin/app/softwork/composetodo/Main.kt | 8 ++++++-- 9 files changed, 34 insertions(+), 20 deletions(-) diff --git a/androidApp/src/main/kotlin/app/softwork/composetodo/Container.kt b/androidApp/src/main/kotlin/app/softwork/composetodo/Container.kt index 31582ede..aee47a7f 100644 --- a/androidApp/src/main/kotlin/app/softwork/composetodo/Container.kt +++ b/androidApp/src/main/kotlin/app/softwork/composetodo/Container.kt @@ -1,6 +1,7 @@ package app.softwork.composetodo import android.content.* +import app.cash.sqldelight.async.coroutines.* import app.cash.sqldelight.driver.android.* import app.softwork.composetodo.repository.* import app.softwork.composetodo.repository.TodoRepository.Companion.createDatabase @@ -15,7 +16,9 @@ import io.ktor.serialization.kotlinx.json.* import kotlinx.coroutines.flow.* class Container(applicationContext: Context) : AppContainer { - private val db = createDatabase(AndroidSqliteDriver(ComposeTodoDB.Schema, applicationContext, "composetodo.db")) + private val db = createDatabase( + AndroidSqliteDriver(ComposeTodoDB.Schema.synchronous(), applicationContext, "composetodo.db") + ) override val client = HttpClient(Android) { defaultRequest { diff --git a/clients/build.gradle.kts b/clients/build.gradle.kts index b6a2c933..100d2c3b 100644 --- a/clients/build.gradle.kts +++ b/clients/build.gradle.kts @@ -12,7 +12,7 @@ sqldelight { databases.register("ComposeTodoDB") { packageName.set("app.softwork.composetodo") deriveSchemaFromMigrations.set(true) - verifyMigrations.set(true) + generateAsync.set(true) } } @@ -94,12 +94,6 @@ kotlin { val iosSimulatorArm64Test by getting { dependsOn(iosTest) } - - val jsMain by getting { - dependencies { - api(libs.sqldelight.sqljsDriver) - } - } } } diff --git a/clients/src/commonMain/kotlin/app/softwork/composetodo/repository/TodoRepository.kt b/clients/src/commonMain/kotlin/app/softwork/composetodo/repository/TodoRepository.kt index ad93c27c..80716228 100644 --- a/clients/src/commonMain/kotlin/app/softwork/composetodo/repository/TodoRepository.kt +++ b/clients/src/commonMain/kotlin/app/softwork/composetodo/repository/TodoRepository.kt @@ -1,5 +1,6 @@ package app.softwork.composetodo.repository +import app.cash.sqldelight.async.coroutines.* import app.cash.sqldelight.coroutines.* import app.cash.sqldelight.db.* import app.softwork.composetodo.* @@ -35,7 +36,7 @@ interface TodoRepository { } override suspend fun deleteAll() { - val allIDs = dao.allIDs().executeAsList() + val allIDs = dao.allIDs().awaitAsList() dao.deleteAll() allIDs.forEach { id -> api.deleteTodo(id) diff --git a/clients/src/iosMain/kotlin/app/softwork/composetodo/IosContainer.kt b/clients/src/iosMain/kotlin/app/softwork/composetodo/IosContainer.kt index c9016ea8..fa8ffea2 100644 --- a/clients/src/iosMain/kotlin/app/softwork/composetodo/IosContainer.kt +++ b/clients/src/iosMain/kotlin/app/softwork/composetodo/IosContainer.kt @@ -1,5 +1,6 @@ package app.softwork.composetodo +import app.cash.sqldelight.async.coroutines.* import app.cash.sqldelight.driver.native.* import app.softwork.composetodo.repository.* import app.softwork.composetodo.viewmodels.* @@ -18,7 +19,9 @@ class IosContainer( protocol: URLProtocol, host: String ) : AppContainer { - private val db = TodoRepository.createDatabase(NativeSqliteDriver(ComposeTodoDB.Schema, "composetodo.db")) + private val db = TodoRepository.createDatabase( + NativeSqliteDriver(ComposeTodoDB.Schema.synchronous(), "composetodo.db") + ) constructor() : this(protocol = URLProtocol.HTTPS, host = "api.todo.softwork.app") diff --git a/desktop/src/main/kotlin/app/softwork/composetodo/DesktopContainer.kt b/desktop/src/main/kotlin/app/softwork/composetodo/DesktopContainer.kt index 7b8b06e8..7d34bfdc 100644 --- a/desktop/src/main/kotlin/app/softwork/composetodo/DesktopContainer.kt +++ b/desktop/src/main/kotlin/app/softwork/composetodo/DesktopContainer.kt @@ -1,5 +1,6 @@ package app.softwork.composetodo +import app.cash.sqldelight.async.coroutines.* import app.cash.sqldelight.db.* import app.cash.sqldelight.driver.jdbc.sqlite.* import app.softwork.composetodo.repository.* @@ -18,7 +19,7 @@ class DesktopContainer : AppContainer { private val db: ComposeTodoDB init { val driver: SqlDriver = JdbcSqliteDriver("jdbc:sqlite:composetodo.db") - ComposeTodoDB.Schema.migrate(driver, 0, 1) + ComposeTodoDB.Schema.synchronous().migrate(driver, 0, 1) db = createDatabase(driver) } override fun todoViewModel(api: API.LoggedIn): TodoViewModel = diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 04a691d6..64462c77 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -5,7 +5,7 @@ coroutines = "1.7.3" datetime = "0.4.0" serialization = "1.5.1" ktor = "2.3.3" -sqldelight = "2.0.0-alpha05" +sqldelight = "2.0.0" cloudkit = "0.1.0" [libraries] @@ -19,7 +19,7 @@ serialization-json = { module = "org.jetbrains.kotlinx:kotlinx-serialization-jso sqldelight-sqliteDriver = { module = "app.cash.sqldelight:sqlite-driver", version.ref = "sqldelight" } sqldelight-androidDriver = { module = "app.cash.sqldelight:android-driver", version.ref = "sqldelight" } sqldelight-nativeDriver = { module = "app.cash.sqldelight:native-driver", version.ref = "sqldelight" } -sqldelight-sqljsDriver = { module = "app.cash.sqldelight:sqljs-driver", version.ref = "sqldelight" } +sqldelight-sqljsDriver = { module = "app.cash.sqldelight:web-worker-driver", version.ref = "sqldelight" } sqldelight-coroutinesExtensions = { module = "app.cash.sqldelight:coroutines-extensions", version.ref = "sqldelight" } apacheCompress = { module = "org.apache.commons:commons-compress", version = "1.23.0" } diff --git a/kotlin-js-store/yarn.lock b/kotlin-js-store/yarn.lock index 6347531f..eaea84d1 100644 --- a/kotlin-js-store/yarn.lock +++ b/kotlin-js-store/yarn.lock @@ -2,6 +2,11 @@ # yarn lockfile v1 +"@cashapp/sqldelight-sqljs-worker@2.0.0": + version "2.0.0" + resolved "https://registry.yarnpkg.com/@cashapp/sqldelight-sqljs-worker/-/sqldelight-sqljs-worker-2.0.0.tgz#16c0138a9611905272019481b2c6abfcb55f9ba8" + integrity sha512-mdavcZ0KnY9LvUfs6Z3o9bDXumpVxICvgBSSo570anDhOGwak3ttU0YbkAtDsyqLIhBLGwGEUqyyDLJ1LqPndg== + "@colors/colors@1.5.0": version "1.5.0" resolved "https://registry.yarnpkg.com/@colors/colors/-/colors-1.5.0.tgz#bb504579c1cae923e6576a4f5da43d25f97bdbd9" @@ -2607,10 +2612,10 @@ spdy@^4.0.2: select-hose "^2.0.0" spdy-transport "^3.0.0" -sql.js@1.7.0: - version "1.7.0" - resolved "https://registry.yarnpkg.com/sql.js/-/sql.js-1.7.0.tgz#cadfed9773b643fbba0fc1ed1847e3a647e1fcb9" - integrity sha512-qAfft3xkSgHqmmfNugWTp/59PsqIw8gbeao5TZmpmzQQsAJ49de3iDDKuxVixidYs6dkHNksY8m27v2dZNn2jw== +sql.js@1.8.0: + version "1.8.0" + resolved "https://registry.yarnpkg.com/sql.js/-/sql.js-1.8.0.tgz#cb45d957e17a2239662fe2f614c9b678990867a6" + integrity sha512-3HD8pSkZL+5YvYUI8nlvNILs61ALqq34xgmF+BHpqxe68yZIJ1H+sIVIODvni25+CcxHUxDyrTJUL0lE/m7afw== statuses@2.0.1: version "2.0.1" diff --git a/web/build.gradle.kts b/web/build.gradle.kts index 37eb40e7..a0523f60 100644 --- a/web/build.gradle.kts +++ b/web/build.gradle.kts @@ -29,7 +29,10 @@ dependencies { implementation(libs.bootstrapCompose) implementation(libs.routingCompose) - implementation(npm("sql.js", "1.7.0")) + implementation(libs.sqldelight.sqljsDriver) + implementation(npm("@cashapp/sqldelight-sqljs-worker", "2.0.0")) + implementation(npm("@cashapp/sqldelight-sqljs-worker", "2.0.0")) + implementation(npm("sql.js", "1.8.0")) implementation(devNpm("copy-webpack-plugin", "9.1.0")) testImplementation(kotlin("test")) diff --git a/web/src/main/kotlin/app/softwork/composetodo/Main.kt b/web/src/main/kotlin/app/softwork/composetodo/Main.kt index 8383e297..e5a79bcf 100644 --- a/web/src/main/kotlin/app/softwork/composetodo/Main.kt +++ b/web/src/main/kotlin/app/softwork/composetodo/Main.kt @@ -1,8 +1,9 @@ package app.softwork.composetodo -import app.cash.sqldelight.driver.sqljs.* +import app.cash.sqldelight.driver.worker.* import kotlinx.coroutines.* import org.jetbrains.compose.web.* +import org.w3c.dom.* suspend fun main() { // https://youtrack.jetbrains.com/issue/KTOR-539 @@ -14,7 +15,10 @@ window.fetch = function (resource, init) { }; """ ) - val driver = initSqlDriver(ComposeTodoDB.Schema).await() + val driver = WebWorkerDriver( + Worker(js("""new URL("@cashapp/sqldelight-sqljs-worker/sqljs.worker.js", import.meta.url)""")) + ) + ComposeTodoDB.Schema.migrate(driver, 0, 1).await() renderComposable(rootElementId = "root") { val appContainer = WebContainer(driver) MainApp(appContainer)