Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to SQDelight 2.0 #20

Merged
merged 1 commit into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ viewModel-compose = "2.6.1"
lifecycle-runtime-compose = "2.6.1"
kotlinx-serialization = "1.5.1"
org-robolectric = "4.9"
sqlDelight = "1.5.5"
sql-delight = "2.0.0"
rss-parser = "6.0.0"
kotlinx-date-time = "0.4.0"
accompanist = "0.28.0"
Expand Down Expand Up @@ -82,11 +82,12 @@ touchlab-kermit-simple = { module = "co.touchlab:kermit-simple", version.ref = "
touchlab-kermit-crashlytics = { module = "co.touchlab:kermit-crashlytics", version.ref = "kermit" }
touchlab-kermit-test = { module = "co.touchlab:kermit-test", version.ref = "kermit" }
org-robolectric = { module = "org.robolectric:robolectric", version.ref = "org-robolectric" }
squareup-sqldelight-android-driver = { module = "com.squareup.sqldelight:android-driver", version.ref = "sqlDelight" }
squareup-sqldelight-coroutine-extensions = { module = "com.squareup.sqldelight:coroutines-extensions", version.ref = "sqlDelight" }
squareup-sqldelight-native-driver = { module = "com.squareup.sqldelight:native-driver", version.ref = "sqlDelight" }
squareup-sqldelight-runtime = { module = "com.squareup.sqldelight:runtime", version.ref = "sqlDelight" }
squareup-sqldelight-sqlite-driver = { module = "com.squareup.sqldelight:sqlite-driver", version.ref = "sqlDelight" }
sqldelight-android-driver = { module = "app.cash.sqldelight:android-driver", version.ref = "sql-delight" }
sqldelight-coroutine-extensions = { module = "app.cash.sqldelight:coroutines-extensions", version.ref = "sql-delight" }
sqldelight-native-driver = { module = "app.cash.sqldelight:native-driver", version.ref = "sql-delight" }
sqldelight-runtime = { module = "app.cash.sqldelight:runtime", version.ref = "sql-delight" }
sqldelight-sqlite-driver = { module = "app.cash.sqldelight:sqlite-driver", version.ref = "sql-delight" }
sqldelight-primitive-adapter = { module = "app.cash.sqldelight:primitive-adapters", version.ref = "sql-delight" }
com-prof18-rss-parser = { module = "com.prof18.rssparser:rssparser", version.ref = "rss-parser" }
kotlinx-datetime = { module = "org.jetbrains.kotlinx:kotlinx-datetime", version.ref = "kotlinx-date-time" }
accompanist-systemuicontroller = { group = "com.google.accompanist", name = "accompanist-systemuicontroller", version.ref = "accompanist" }
Expand Down Expand Up @@ -121,7 +122,7 @@ kotlin-serialization = { id = "org.jetbrains.kotlin.plugin.serialization", versi
kotlin-multiplatform = { id = "org.jetbrains.kotlin.multiplatform", version.ref = "kotlin" }
versionsBenManes = { id = "com.github.ben-manes.versions", version.ref = "versions-ben-manes" }
native-coroutines = { id = "com.rickclephas.kmp.nativecoroutines", version.ref = "native-coroutines" }
sqldelight = { id = "com.squareup.sqldelight", version.ref = "sqlDelight" }
sqldelight = { id = "app.cash.sqldelight", version.ref = "sql-delight" }
triplet-play = { id = "com.github.triplet.play", version.ref = "triplet-play" }
compose-multiplatform = { id = "org.jetbrains.compose", version.ref = "compose-multiplatform" }
ksp = { id = "com.google.devtools.ksp", version.ref = "ksp" }
Expand Down
25 changes: 14 additions & 11 deletions shared/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,9 @@ kotlin {

val commonMain by getting {
dependencies {
implementation(libs.squareup.sqldelight.runtime)
implementation(libs.squareup.sqldelight.coroutine.extensions)
implementation(libs.sqldelight.runtime)
implementation(libs.sqldelight.coroutine.extensions)
implementation(libs.sqldelight.primitive.adapter)
implementation(libs.koin.core)
implementation(libs.kotlinx.coroutines.core)
implementation(libs.com.prof18.rss.parser)
Expand Down Expand Up @@ -100,7 +101,7 @@ kotlin {
dependsOn(commonMobileMain)

dependencies {
implementation(libs.squareup.sqldelight.android.driver)
implementation(libs.sqldelight.android.driver)
implementation(libs.androidx.lifecycle.viewModel.ktx)
implementation(libs.koin.android)
implementation(libs.crashk.ios)
Expand All @@ -113,7 +114,7 @@ kotlin {
dependencies {
implementation(libs.junit)
implementation(libs.org.robolectric)
implementation(libs.squareup.sqldelight.sqlite.driver)
implementation(libs.sqldelight.sqlite.driver)
implementation(libs.androidx.test.core.ktx)
}
}
Expand All @@ -128,7 +129,7 @@ kotlin {
iosSimulatorArm64Main.dependsOn(this)

dependencies {
implementation(libs.squareup.sqldelight.native.driver)
implementation(libs.sqldelight.native.driver)

api(libs.touchlab.kermit.simple)
}
Expand All @@ -143,15 +144,15 @@ kotlin {
iosSimulatorArm64Test.dependsOn(this)

dependencies {
implementation(libs.squareup.sqldelight.native.driver)
implementation(libs.sqldelight.native.driver)
}
}

val desktopMain by getting {
dependsOn(commonJvmAndroidMain)

dependencies {
implementation(libs.squareup.sqldelight.sqlite.driver)
implementation(libs.sqldelight.sqlite.driver)
implementation(libs.kotlinx.coroutines.swing)
api(libs.sentry)
}
Expand All @@ -164,11 +165,13 @@ kotlin {
}

sqldelight {
database("FeedFlowDB") {
packageName = "com.prof18.feedflow.db"
schemaOutputDirectory = file("src/commonMain/sqldelight/com/prof18/feedflow/schema")
databases {
create("FeedFlowDB") {
packageName.set("com.prof18.feedflow.db")
schemaOutputDirectory.set(file("src/commonMain/sqldelight/com/prof18/feedflow/schema"))

verifyMigrations = true
verifyMigrations.set(true)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.prof18.feedflow.di

import android.content.Context
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.android.AndroidSqliteDriver
import com.prof18.feedflow.data.DatabaseHelper
import com.prof18.feedflow.db.FeedFlowDB
import com.prof18.feedflow.domain.DateFormatter
Expand All @@ -12,8 +14,6 @@ import com.prof18.feedflow.presentation.BaseViewModel
import com.prof18.feedflow.utils.DispatcherProvider
import com.russhwolf.settings.Settings
import com.russhwolf.settings.SharedPreferencesSettings
import com.squareup.sqldelight.android.AndroidSqliteDriver
import com.squareup.sqldelight.db.SqlDriver
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import org.koin.core.definition.Definition
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
package com.prof18.feedflow.data

import app.cash.sqldelight.Transacter
import app.cash.sqldelight.TransactionWithoutReturn
import app.cash.sqldelight.adapter.primitive.IntColumnAdapter
import app.cash.sqldelight.coroutines.asFlow
import app.cash.sqldelight.coroutines.mapToList
import app.cash.sqldelight.db.SqlDriver
import co.touchlab.kermit.Logger
import com.prof18.feedflow.core.model.FeedItem
import com.prof18.feedflow.core.model.FeedSource
import com.prof18.feedflow.core.model.ParsedFeedSource
import com.prof18.feedflow.db.FeedFlowDB
import com.prof18.feedflow.db.Feed_item
import com.prof18.feedflow.db.Feed_source
import com.prof18.feedflow.db.SelectFeeds
import com.prof18.feedflow.domain.model.FeedItemId
import com.squareup.sqldelight.Transacter
import com.squareup.sqldelight.TransactionWithoutReturn
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.runtime.coroutines.asFlow
import com.squareup.sqldelight.runtime.coroutines.mapToList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
Expand All @@ -27,7 +30,16 @@ internal class DatabaseHelper(
private val backgroundDispatcher: CoroutineDispatcher,
private val logger: Logger,
) {
private val dbRef: FeedFlowDB = FeedFlowDB(sqlDriver)
private val dbRef: FeedFlowDB = FeedFlowDB(
sqlDriver,
feed_itemAdapter = Feed_item.Adapter(
url_hashAdapter = IntColumnAdapter,
feed_source_idAdapter = IntColumnAdapter,
),
feed_sourceAdapter = Feed_source.Adapter(
url_hashAdapter = IntColumnAdapter,
),
)

suspend fun getFeedSources(): List<FeedSource> = withContext(backgroundDispatcher) {
dbRef.feedSourceQueries
Expand All @@ -50,7 +62,7 @@ internal class DatabaseHelper(
.catch {
logger.e(it) { "Something wrong while getting data from Database" }
}
.mapToList()
.mapToList(backgroundDispatcher)
.map { feedSources ->
feedSources.map { feedSource ->
FeedSource(
Expand All @@ -68,7 +80,7 @@ internal class DatabaseHelper(
dbRef.feedItemQueries
.selectFeeds()
.asFlow()
.mapToList()
.mapToList(backgroundDispatcher)
.retry(3) { exception ->
exception is NullPointerException
}
Expand Down
3 changes: 2 additions & 1 deletion shared/src/commonMain/kotlin/com/prof18/feedflow/di/Koin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import com.prof18.feedflow.presentation.ImportExportViewModel
import com.prof18.feedflow.utils.AppEnvironment
import com.prof18.rssparser.RssParser
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
import org.koin.core.KoinApplication
import org.koin.core.context.startKoin
import org.koin.core.definition.Definition
Expand Down Expand Up @@ -63,7 +64,7 @@ private val coreModule = module {
single {
DatabaseHelper(
sqlDriver = get(),
backgroundDispatcher = Dispatchers.Default,
backgroundDispatcher = Dispatchers.IO,
logger = getWith("DatabaseHelper"),
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import kotlin.Boolean;
import kotlin.Int;

CREATE TABLE feed_item (
url_hash INTEGER AS Int NOT NULL PRIMARY KEY,
url TEXT NOT NULL,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import kotlin.Int;

CREATE TABLE feed_source (
url_hash INTEGER AS Int NOT NULL PRIMARY KEY,
url TEXT NOT NULL,
Expand Down
23 changes: 16 additions & 7 deletions shared/src/desktopMain/kotlin/com/prof18/feedflow/DbInitializer.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package com.prof18.feedflow

import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.jdbc.sqlite.JdbcSqliteDriver
import co.touchlab.kermit.Logger
import com.prof18.feedflow.data.DatabaseHelper
import com.prof18.feedflow.db.FeedFlowDB
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.sqlite.driver.JdbcSqliteDriver
import java.io.File
import java.util.Properties

Expand All @@ -15,15 +16,23 @@ internal fun initDatabase(logger: Logger): SqlDriver {

val driver = JdbcSqliteDriver(JdbcSqliteDriver.IN_MEMORY + databasePath.absolutePath, Properties())

val sqlCursor = driver.executeQuery(null, "PRAGMA user_version;", 0, null)
val currentVer: Int = sqlCursor.use { sqlCursor.getLong(0)?.toInt() ?: 0 }
val sqlCursor = driver.executeQuery(
null,
"PRAGMA user_version;",
{
QueryResult.Value(it.getLong(0))
},
0,
null,
)
val currentVer: Long = sqlCursor.value ?: -1L

if (currentVer == 0) {
if (currentVer == 0L) {
FeedFlowDB.Schema.create(driver)
setVersion(driver, 1)
logger.d("init: created tables, setVersion to 1")
} else {
val schemaVer: Int = FeedFlowDB.Schema.version
val schemaVer = FeedFlowDB.Schema.version
if (schemaVer > currentVer) {
FeedFlowDB.Schema.migrate(driver, currentVer, schemaVer)
setVersion(driver, schemaVer)
Expand All @@ -35,6 +44,6 @@ internal fun initDatabase(logger: Logger): SqlDriver {
return driver
}

fun setVersion(driver: SqlDriver, version: Int) {
fun setVersion(driver: SqlDriver, version: Long) {
driver.execute(null, "PRAGMA user_version = $version;", 0, null)
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prof18.feedflow.di

import app.cash.sqldelight.db.SqlDriver
import com.prof18.feedflow.domain.DateFormatter
import com.prof18.feedflow.domain.HtmlParser
import com.prof18.feedflow.domain.JvmAndroidDateFormatter
Expand All @@ -11,7 +12,6 @@ import com.prof18.feedflow.utils.AppEnvironment
import com.prof18.feedflow.utils.DispatcherProvider
import com.russhwolf.settings.PreferencesSettings
import com.russhwolf.settings.Settings
import com.squareup.sqldelight.db.SqlDriver
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import org.koin.core.KoinApplication
Expand Down
4 changes: 2 additions & 2 deletions shared/src/iosMain/kotlin/com/prof18/feedflow/di/KoinIOS.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.prof18.feedflow.di

import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.driver.native.NativeSqliteDriver
import co.touchlab.kermit.Logger
import com.prof18.feedflow.data.DatabaseHelper
import com.prof18.feedflow.db.FeedFlowDB
Expand All @@ -18,8 +20,6 @@ import com.prof18.feedflow.utils.DispatcherProvider
import com.russhwolf.settings.ExperimentalSettingsImplementation
import com.russhwolf.settings.KeychainSettings
import com.russhwolf.settings.Settings
import com.squareup.sqldelight.db.SqlDriver
import com.squareup.sqldelight.drivers.native.NativeSqliteDriver
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.IO
Expand Down