diff --git a/data/db-sqldelight/src/commonMain/kotlin/app/tivi/data/DatabaseConfiguration.kt b/data/db-sqldelight/src/commonMain/kotlin/app/tivi/data/DatabaseConfiguration.kt index 7dcd52dd65..a4a2f0473e 100644 --- a/data/db-sqldelight/src/commonMain/kotlin/app/tivi/data/DatabaseConfiguration.kt +++ b/data/db-sqldelight/src/commonMain/kotlin/app/tivi/data/DatabaseConfiguration.kt @@ -3,4 +3,7 @@ package app.tivi.data -data class DatabaseConfiguration(val inMemory: Boolean = false) +data class DatabaseConfiguration( + val name: String = "tivi", + val inMemory: Boolean = false, +) diff --git a/data/db-sqldelight/src/iosMain/kotlin/app/tivi/data/SqlDelightDatabasePlatformComponent.kt b/data/db-sqldelight/src/iosMain/kotlin/app/tivi/data/SqlDelightDatabasePlatformComponent.kt index bedcc2f72f..20e4ace545 100644 --- a/data/db-sqldelight/src/iosMain/kotlin/app/tivi/data/SqlDelightDatabasePlatformComponent.kt +++ b/data/db-sqldelight/src/iosMain/kotlin/app/tivi/data/SqlDelightDatabasePlatformComponent.kt @@ -3,9 +3,12 @@ package app.tivi.data +import app.cash.sqldelight.db.QueryResult import app.cash.sqldelight.db.SqlDriver +import app.cash.sqldelight.db.SqlSchema import app.cash.sqldelight.driver.native.NativeSqliteDriver import app.cash.sqldelight.driver.native.inMemoryDriver +import app.cash.sqldelight.driver.native.wrapConnection import app.tivi.inject.ApplicationScope import me.tatarka.inject.annotations.Provides @@ -14,11 +17,11 @@ actual interface SqlDelightDatabasePlatformComponent { @ApplicationScope fun provideDriverFactory(configuration: DatabaseConfiguration): SqlDriver { return when { - configuration.inMemory -> inMemoryDriver(Database.Schema) + configuration.inMemory -> inMemoryDriver(Database.Schema, configuration.name) else -> { NativeSqliteDriver( schema = Database.Schema, - name = "tivi.db", + name = configuration.name, maxReaderConnections = 4, ) } @@ -27,3 +30,24 @@ actual interface SqlDelightDatabasePlatformComponent { } } } + +private fun inMemoryDriver( + schema: SqlSchema>, + name: String, +): NativeSqliteDriver = NativeSqliteDriver( + co.touchlab.sqliter.DatabaseConfiguration( + name = name, + inMemory = true, + version = if (schema.version > Int.MAX_VALUE) { + error("Schema version is larger than Int.MAX_VALUE: ${schema.version}.") + } else { + schema.version.toInt() + }, + create = { connection -> + wrapConnection(connection) { schema.create(it) } + }, + upgrade = { connection, oldVersion, newVersion -> + wrapConnection(connection) { schema.migrate(it, oldVersion.toLong(), newVersion.toLong()) } + }, + ), +) diff --git a/data/db-sqldelight/src/jvmMain/kotlin/app/tivi/data/SqlDelightDatabasePlatformComponent.kt b/data/db-sqldelight/src/jvmMain/kotlin/app/tivi/data/SqlDelightDatabasePlatformComponent.kt index 57e8eae8db..32fb046499 100644 --- a/data/db-sqldelight/src/jvmMain/kotlin/app/tivi/data/SqlDelightDatabasePlatformComponent.kt +++ b/data/db-sqldelight/src/jvmMain/kotlin/app/tivi/data/SqlDelightDatabasePlatformComponent.kt @@ -17,7 +17,7 @@ actual interface SqlDelightDatabasePlatformComponent { ): SqlDriver = JdbcSqliteDriver( url = when { configuration.inMemory -> JdbcSqliteDriver.IN_MEMORY - else -> "jdbc:sqlite:${getDatabaseFile().absolutePath}" + else -> "jdbc:sqlite:${configuration.file.absolutePath}" }, ).also { db -> Database.Schema.create(db) @@ -25,12 +25,8 @@ actual interface SqlDelightDatabasePlatformComponent { } } -private fun getDatabaseFile(): File { - return File( - appDir.also { if (!it.exists()) it.mkdirs() }, - "tivi.db", - ) -} +private val DatabaseConfiguration.file: File + get() = File(appDir.also { if (!it.exists()) it.mkdirs() }, "$name.db") private val appDir: File get() { diff --git a/data/test/build.gradle.kts b/data/test/build.gradle.kts index 65feef9efc..d4072926b3 100644 --- a/data/test/build.gradle.kts +++ b/data/test/build.gradle.kts @@ -36,6 +36,8 @@ kotlin { implementation(libs.kotlin.coroutines.test) implementation(libs.turbine) + + implementation(libs.uuid) } } } diff --git a/data/test/src/commonTest/kotlin/app/tivi/data/DatabaseTest.kt b/data/test/src/commonTest/kotlin/app/tivi/data/DatabaseTest.kt index bb55c89add..47b5f82f21 100644 --- a/data/test/src/commonTest/kotlin/app/tivi/data/DatabaseTest.kt +++ b/data/test/src/commonTest/kotlin/app/tivi/data/DatabaseTest.kt @@ -16,6 +16,7 @@ import app.tivi.trakt.TraktCommonComponent import app.tivi.util.Logger import app.tivi.utils.SuccessRefreshTokenAction import app.tivi.utils.SuccessTraktLoginAction +import com.benasher44.uuid.uuid4 import kotlin.test.AfterTest import kotlinx.coroutines.DelicateCoroutinesApi import kotlinx.coroutines.GlobalScope @@ -56,9 +57,7 @@ abstract class TestApplicationComponent : @Provides override fun provideDatabaseConfiguration(): DatabaseConfiguration { - return super.provideDatabaseConfiguration().copy( - inMemory = true, - ) + return DatabaseConfiguration(name = uuid4().toString(), inMemory = true) } @Provides