one more update to versions

This commit is contained in:
InsanusMokrassar 2021-04-20 20:37:23 +06:00
parent b5072486b4
commit 7f813a519b
2 changed files with 21 additions and 13 deletions

View File

@ -18,19 +18,16 @@ class StandardVersionsRepo<T>(
onCreate: suspend T.() -> Unit, onCreate: suspend T.() -> Unit,
onUpdate: suspend T.(from: Int, to: Int) -> Unit onUpdate: suspend T.(from: Int, to: Int) -> Unit
) { ) {
var savedVersion = proxy.getTableVersion(tableName) var currentVersion = proxy.getTableVersion(tableName)
if (savedVersion == null) { if (currentVersion == null) {
proxy.database.onCreate() proxy.database.onCreate()
proxy.updateTableVersion(tableName, version)
} else {
while (savedVersion != null && savedVersion < version) {
val newVersion = savedVersion + 1
proxy.database.onUpdate(savedVersion, newVersion)
proxy.updateTableVersion(tableName, newVersion)
savedVersion = newVersion
} }
while (currentVersion == null || currentVersion < version) {
val oldVersion = currentVersion ?: 0
currentVersion = oldVersion + 1
proxy.database.onUpdate(oldVersion, currentVersion)
proxy.updateTableVersion(tableName, currentVersion)
} }
} }
} }

View File

@ -5,6 +5,7 @@ import android.database.DatabaseErrorHandler
import android.database.sqlite.SQLiteDatabase import android.database.sqlite.SQLiteDatabase
import android.database.sqlite.SQLiteOpenHelper import android.database.sqlite.SQLiteOpenHelper
import dev.inmo.micro_utils.coroutines.safely import dev.inmo.micro_utils.coroutines.safely
import dev.inmo.micro_utils.repos.keyvalue.keyValueStore
import dev.inmo.micro_utils.repos.versions.* import dev.inmo.micro_utils.repos.versions.*
import kotlin.coroutines.Continuation import kotlin.coroutines.Continuation
import kotlin.coroutines.resume import kotlin.coroutines.resume
@ -15,7 +16,8 @@ class StandardSQLHelper(
name: String, name: String,
factory: SQLiteDatabase.CursorFactory? = null, factory: SQLiteDatabase.CursorFactory? = null,
version: Int = 1, version: Int = 1,
errorHandler: DatabaseErrorHandler? = null errorHandler: DatabaseErrorHandler? = null,
useSharedPreferencesForVersions: Boolean = false
) { ) {
val sqlOpenHelper = object : SQLiteOpenHelper(context, name, factory, version, errorHandler) { val sqlOpenHelper = object : SQLiteOpenHelper(context, name, factory, version, errorHandler) {
override fun onCreate(db: SQLiteDatabase?) {} override fun onCreate(db: SQLiteDatabase?) {}
@ -23,7 +25,16 @@ class StandardSQLHelper(
override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {} override fun onUpgrade(db: SQLiteDatabase?, oldVersion: Int, newVersion: Int) {}
} }
val versionsRepo: VersionsRepo<SQLiteOpenHelper> by lazy { val versionsRepo: VersionsRepo<SQLiteOpenHelper> by lazy {
StandardVersionsRepo(AndroidSQLStandardVersionsRepoProxy(sqlOpenHelper)) StandardVersionsRepo(
if (useSharedPreferencesForVersions) {
KeyValueBasedVersionsRepoProxy(
context.keyValueStore("AndroidSPStandardVersionsRepo"),
sqlOpenHelper
)
} else {
AndroidSQLStandardVersionsRepoProxy(sqlOpenHelper)
}
)
} }
suspend fun <T> writableTransaction(block: suspend SQLiteDatabase.() -> T): T = sqlOpenHelper.writableTransaction(block) suspend fun <T> writableTransaction(block: suspend SQLiteDatabase.() -> T): T = sqlOpenHelper.writableTransaction(block)