Skip to content

Commit

Permalink
chore: bump sqldelight to 2.0.0 (#2045)
Browse files Browse the repository at this point in the history
* chore: bump sqldelight to 2.0.0

* replace sourceFolders with srcDirs

* fix sqldelight breaking changes

* remove unwanted changes

* detekt

* fix: ios driver changes

* detekt
  • Loading branch information
MohamadJaara authored Sep 13, 2023
1 parent 3b87e90 commit f847369
Show file tree
Hide file tree
Showing 9 changed files with 24 additions and 16 deletions.
4 changes: 2 additions & 2 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ kover = "0.7.1"
multiplatform-settings = "1.0.0"
# if you update sqlDelight check if https://github.com/cashapp/sqldelight/issues/4154 is fixed
# and delete the work around in the dev.mk file
sqldelight = "2.0.0-alpha05"
sqldelight = "2.0.0"
sqlcipher-android = "4.5.5"
pbandk = "0.14.2"
turbine = "1.0.0"
Expand Down Expand Up @@ -157,7 +157,7 @@ sqldelight-androidDriver = { module = "app.cash.sqldelight:android-driver", vers
sqldelight-androidxPaging = { module = "app.cash.sqldelight:androidx-paging3-extensions", version.ref = "sqldelight" }
sqldelight-nativeDriver = { module = "app.cash.sqldelight:native-driver", version.ref = "sqldelight" }
sqldelight-jvmDriver = { module = "app.cash.sqldelight:sqlite-driver", version.ref = "sqldelight" }
sqldelight-jsDriver = { module = "app.cash.sqldelight:sqljs-driver", version.ref = "sqldelight" }
sqldelight-jsDriver = { module = "app.cash.sqldelight:web-worker-driver", version.ref = "sqldelight" }
sqldelight-primitiveAdapters = { module = "app.cash.sqldelight:primitive-adapters", version.ref = "sqldelight" }
sqldelight-dialect = { module = "app.cash.sqldelight:sqlite-3-33-dialect", version.ref = "sqldelight" }

Expand Down
4 changes: 2 additions & 2 deletions persistence/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ sqldelight {
dialect(libs.sqldelight.dialect.get().toString())
packageName.set("com.wire.kalium.persistence")
val sourceFolderName = "db_user"
sourceFolders.set(listOf(sourceFolderName))
srcDirs.setFrom(listOf("src/commonMain/$sourceFolderName"))
schemaOutputDirectory.set(file("src/commonMain/$sourceFolderName/schemas"))
}

create("GlobalDatabase") {
dialect(libs.sqldelight.dialect.get().toString())
packageName.set("com.wire.kalium.persistence")
val sourceFolderName = "db_global"
sourceFolders.set(listOf(sourceFolderName))
srcDirs.setFrom(listOf("src/commonMain/$sourceFolderName"))
schemaOutputDirectory.set(file("src/commonMain/$sourceFolderName/schemas"))
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,14 @@ package com.wire.kalium.persistence.db.support

import androidx.sqlite.db.SupportSQLiteDatabase
import androidx.sqlite.db.SupportSQLiteOpenHelper
import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlSchema
import app.cash.sqldelight.driver.android.AndroidSqliteDriver

internal class SqliteCallback(schema: SqlSchema, private val enableWAL: Boolean) : SupportSQLiteOpenHelper.Callback(schema.version) {
internal class SqliteCallback(
schema: SqlSchema<QueryResult.Value<Unit>>,
private val enableWAL: Boolean
) : SupportSQLiteOpenHelper.Callback(schema.version.toInt()) {
private val baseCallback = AndroidSqliteDriver.Callback(schema)
override fun onCreate(db: SupportSQLiteDatabase) = baseCallback.onCreate(db)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,16 @@ actual class GlobalDatabaseProvider(

init {
NSFileManager.defaultManager.createDirectoryAtPath(storePath, true, null, null)

val schema = GlobalDatabase.Schema
val driver = NativeSqliteDriver(
DatabaseConfiguration(
name = dbName,
version = schema.version,
version = schema.version.toInt(),
create = { connection ->
wrapConnection(connection) { schema.create(it) }
},
upgrade = { connection, oldVersion, newVersion ->
wrapConnection(connection) { schema.migrate(it, oldVersion, newVersion) }
wrapConnection(connection) { schema.migrate(it, oldVersion.toLong(), newVersion.toLong()) }
},
extendedConfig = DatabaseConfiguration.Extended(
basePath = storePath
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ actual fun userDatabaseBuilder(
val driver = NativeSqliteDriver(
DatabaseConfiguration(
name = FileNameUtil.userDBName(userId),
version = schema.version,
version = schema.version.toInt(),
journalMode = if (enableWAL) JournalMode.WAL else JournalMode.DELETE,
create = { connection ->
wrapConnection(connection) { schema.create(it) }
},
upgrade = { connection, oldVersion, newVersion ->
wrapConnection(connection) { schema.migrate(it, oldVersion, newVersion) }
wrapConnection(connection) { schema.migrate(it, oldVersion.toLong(), newVersion.toLong()) }
},
extendedConfig = DatabaseConfiguration.Extended(
basePath = platformDatabaseData.storePath
Expand Down Expand Up @@ -96,13 +96,13 @@ fun inMemoryDatabase(
val driver = NativeSqliteDriver(
DatabaseConfiguration(
name = FileNameUtil.userDBName(userId),
version = schema.version,
version = schema.version.toInt(),
inMemory = true,
create = { connection ->
wrapConnection(connection) { schema.create(it) }
},
upgrade = { connection, oldVersion, newVersion ->
wrapConnection(connection) { schema.migrate(it, oldVersion, newVersion) }
wrapConnection(connection) { schema.migrate(it, oldVersion.toLong(), newVersion.toLong()) }
}
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import com.wire.kalium.persistence.dao.QualifiedIDEntity;
import com.wire.kalium.persistence.dao.client.ClientTypeEntity;
import com.wire.kalium.persistence.dao.client.DeviceTypeEntity;
import kotlin.Boolean;
import kotlin.String;
import kotlin.collections.Map;
import kotlinx.datetime.Instant;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.wire.kalium.persistence.dao.message.MessageEntity;
import com.wire.kalium.persistence.dao.message.RecipientFailureTypeEntity;
import kotlin.Boolean;
import kotlin.Int;
import kotlin.String;
import kotlin.collections.List;
import kotlinx.datetime.Instant;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import com.wire.kalium.persistence.dao.BotIdEntity;
import com.wire.kalium.persistence.dao.QualifiedIDEntity;
import kotlin.Boolean;
import kotlin.String;
import kotlin.collections.List;

CREATE TABLE Service (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

package com.wire.kalium.persistence.db

import app.cash.sqldelight.db.QueryResult
import app.cash.sqldelight.db.SqlDriver
import app.cash.sqldelight.db.SqlSchema
import com.wire.kalium.persistence.UserDatabase
Expand Down Expand Up @@ -283,11 +284,11 @@ internal expect fun getDatabaseAbsoluteFileLocation(
): String?

@Suppress("TooGenericExceptionCaught")
fun SqlDriver.migrate(sqlSchema: SqlSchema): Boolean {
fun SqlDriver.migrate(sqlSchema: SqlSchema<QueryResult.Value<Unit>>): Boolean {
val oldVersion = this.executeQuery(null, "PRAGMA user_version;", {
it.next()
it.getLong(0)
}, 0).value?.toInt() ?: return false
it.getLong(0).let { QueryResult.Value<Long?>(it) }
}, 0).value ?: return false

val newVersion = sqlSchema.version
return try {
Expand All @@ -309,9 +310,10 @@ fun SqlDriver.checkFKViolations(): Boolean {
// foreign_key_check returns the rows with the fk violations
// if the cursor has a next, it means there are violations
// and the backup is corrupted
if (it.next()) {
if (it.next().value) {
result = true
}
QueryResult.Unit
}, 0, null)

return result
Expand Down

0 comments on commit f847369

Please sign in to comment.