diff --git a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/versions/StandardVersionsRepo.kt b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/versions/StandardVersionsRepo.kt index b02432c2759..46a52acb02d 100644 --- a/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/versions/StandardVersionsRepo.kt +++ b/repos/common/src/commonMain/kotlin/dev/inmo/micro_utils/repos/versions/StandardVersionsRepo.kt @@ -18,19 +18,16 @@ class StandardVersionsRepo( onCreate: suspend T.() -> Unit, onUpdate: suspend T.(from: Int, to: Int) -> Unit ) { - var savedVersion = proxy.getTableVersion(tableName) - if (savedVersion == null) { + var currentVersion = proxy.getTableVersion(tableName) + if (currentVersion == null) { proxy.database.onCreate() - proxy.updateTableVersion(tableName, version) - } else { - while (savedVersion != null && savedVersion < version) { - val newVersion = savedVersion + 1 + } + while (currentVersion == null || currentVersion < version) { + val oldVersion = currentVersion ?: 0 + currentVersion = oldVersion + 1 + proxy.database.onUpdate(oldVersion, currentVersion) - proxy.database.onUpdate(savedVersion, newVersion) - - proxy.updateTableVersion(tableName, newVersion) - savedVersion = newVersion - } + proxy.updateTableVersion(tableName, currentVersion) } } } \ No newline at end of file diff --git a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/StandardSQLHelper.kt b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/StandardSQLHelper.kt index 8979680cefe..7126eb9f2ca 100644 --- a/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/StandardSQLHelper.kt +++ b/repos/common/src/main/kotlin/dev/inmo/micro_utils/repos/StandardSQLHelper.kt @@ -5,6 +5,7 @@ import android.database.DatabaseErrorHandler import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteOpenHelper import dev.inmo.micro_utils.coroutines.safely +import dev.inmo.micro_utils.repos.keyvalue.keyValueStore import dev.inmo.micro_utils.repos.versions.* import kotlin.coroutines.Continuation import kotlin.coroutines.resume @@ -15,7 +16,8 @@ class StandardSQLHelper( name: String, factory: SQLiteDatabase.CursorFactory? = null, version: Int = 1, - errorHandler: DatabaseErrorHandler? = null + errorHandler: DatabaseErrorHandler? = null, + useSharedPreferencesForVersions: Boolean = false ) { val sqlOpenHelper = object : SQLiteOpenHelper(context, name, factory, version, errorHandler) { override fun onCreate(db: SQLiteDatabase?) {} @@ -23,7 +25,16 @@ class StandardSQLHelper( override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {} } val versionsRepo: VersionsRepo by lazy { - StandardVersionsRepo(AndroidSQLStandardVersionsRepoProxy(sqlOpenHelper)) + StandardVersionsRepo( + if (useSharedPreferencesForVersions) { + KeyValueBasedVersionsRepoProxy( + context.keyValueStore("AndroidSPStandardVersionsRepo"), + sqlOpenHelper + ) + } else { + AndroidSQLStandardVersionsRepoProxy(sqlOpenHelper) + } + ) } suspend fun writableTransaction(block: suspend SQLiteDatabase.() -> T): T = sqlOpenHelper.writableTransaction(block)