Skip to content

Commit

Permalink
fix: nested transaction error on iOS
Browse files Browse the repository at this point in the history
Close #16
  • Loading branch information
tamimattafi authored Apr 19, 2024
1 parent c29e7f8 commit 408732c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import org.gradle.api.publish.maven.MavenPomScm

class PublishConventions : Plugin<Project> {

private val version = "0.1.0-alpha06"
private val version = "0.1.0-alpha07"
private val group = "com.attafitamim.kabin"

override fun apply(project: Project) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ abstract class KabinBaseDatabase(
) : KabinDatabase {

private val queries = KabinSuspendingQueries(driver)

abstract suspend fun clearTables()

final override suspend fun clear() {
queries.safeGlobalTransaction(configuration) {
clearTables()
}.await()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.attafitamim.kabin.core.database.configuration.KabinDatabaseConfigurat
import com.attafitamim.kabin.core.exceptions.SqlMigrationMissing
import com.attafitamim.kabin.core.migration.KabinMigrationStrategy
import com.attafitamim.kabin.core.migration.Migration
import com.attafitamim.kabin.core.utils.safeGlobalTransaction
import com.attafitamim.kabin.core.utils.safeGlobalQuery

abstract class KabinSqlSchema(
val migrations: List<Migration>,
Expand All @@ -21,7 +21,7 @@ abstract class KabinSqlSchema(
abstract suspend fun createTables(driver: SqlDriver)

override fun create(driver: SqlDriver): QueryResult.AsyncValue<Unit> =
driver.safeGlobalTransaction(configuration) {
driver.safeGlobalQuery(configuration) {
createTables(driver)
}

Expand All @@ -30,7 +30,7 @@ abstract class KabinSqlSchema(
oldVersion: Long,
newVersion: Long,
vararg callbacks: AfterVersion
): QueryResult.AsyncValue<Unit> = driver.safeGlobalTransaction(configuration) {
): QueryResult.AsyncValue<Unit> = driver.safeGlobalQuery(configuration) {
when {
oldVersion == newVersion -> callbacks.notifyAll(driver)
oldVersion > newVersion -> handleMissingMigration(oldVersion, newVersion, driver, callbacks)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,30 @@ suspend inline fun <T> KabinSuspendingQueries.safeTransactionWithResult(
body()
}

inline fun SqlDriver.safeGlobalTransaction(
inline fun SqlDriver.safeGlobalQuery(
configuration: KabinDatabaseConfiguration,
noinline body: suspend TransactionCallbacks.() -> Unit
): QueryResult.AsyncValue<Unit> = KabinSuspendingQueries(this)
.safeGlobalTransaction(configuration, body)
noinline body: suspend () -> Unit
): QueryResult.AsyncValue<Unit> = with(KabinSuspendingQueries(this)) {
QueryResult.AsyncValue {
try {
tryToggleForeignKeys(configuration, enabled = false)
tryDifferForeignKeys(configuration, enabled = true)
body()
} finally {
tryToggleForeignKeys(configuration, enabled = true)
tryDifferForeignKeys(configuration, enabled = false)
}
}
}

inline fun KabinSuspendingQueries.safeGlobalTransaction(
suspend inline fun KabinSuspendingQueries.safeGlobalTransaction(
configuration: KabinDatabaseConfiguration,
noinline body: suspend TransactionCallbacks.() -> Unit
) = QueryResult.AsyncValue {
try {
tryToggleForeignKeys(configuration, enabled = false)
safeTransaction(configuration, body = body)
} finally {
tryToggleForeignKeys(configuration, enabled = true)
}
) = try {
tryToggleForeignKeys(configuration, enabled = false)
safeTransaction(configuration, body = body)
} finally {
tryToggleForeignKeys(configuration, enabled = true)
}

suspend fun KabinSuspendingQueries.tryToggleForeignKeys(
Expand All @@ -52,9 +60,10 @@ suspend fun KabinSuspendingQueries.tryToggleForeignKeys(
}

suspend fun KabinSuspendingQueries.tryDifferForeignKeys(
configuration: KabinDatabaseConfiguration
configuration: KabinDatabaseConfiguration,
enabled: Boolean = true
) = with(configuration.extendedConfig) {
if (foreignKeyConstraintsEnabled && deferForeignKeysInsideTransaction) {
deferForeignKeys()
deferForeignKeys(enabled)
}
}

0 comments on commit 408732c

Please sign in to comment.