All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonMain.dev.inmo.micro_utils.repos.versions.StandardVersionsRepo.kt Maven / Gradle / Ivy

There is a newer version: 0.22.2
Show newest version
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)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy