commonMain.dev.inmo.micro_utils.repos.versions.StandardVersionsRepo.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micro_utils.repos.common-jvm Show documentation
Show all versions of micro_utils.repos.common-jvm Show documentation
It is set of projects with micro tools for avoiding of routines coding
package dev.inmo.micro_utils.repos.versions
import dev.inmo.micro_utils.repos.Repo
interface StandardVersionsRepoProxy : Repo {
val database: T
suspend fun getTableVersion(tableName: String): Int?
suspend fun updateTableVersion(tableName: String, version: Int)
}
class StandardVersionsRepo(
private val proxy: StandardVersionsRepoProxy
) : VersionsRepo {
override suspend fun setTableVersion(
tableName: String,
version: Int,
onCreate: suspend T.() -> Unit,
onUpdate: suspend T.(from: Int, to: Int) -> Unit
) {
var currentVersion = proxy.getTableVersion(tableName)
if (currentVersion == null) {
proxy.database.onCreate()
}
while (currentVersion == null || currentVersion < version) {
val oldVersion = currentVersion ?: 0
currentVersion = oldVersion + 1
proxy.database.onUpdate(oldVersion, currentVersion)
proxy.updateTableVersion(tableName, currentVersion)
}
}
}