mirror of
https://github.com/mihonapp/mihon.git
synced 2025-08-25 15:41:32 +02:00
Compare commits
4 Commits
f9473c1ca2
...
d2c6de8ea4
Author | SHA1 | Date | |
---|---|---|---|
|
d2c6de8ea4 | ||
|
56782cb309 | ||
|
b57699f98d | ||
|
45fb16b088 |
@@ -77,8 +77,6 @@ import eu.kanade.tachiyomi.util.system.dpToPx
|
||||
import eu.kanade.tachiyomi.util.system.isNavigationBarNeedsScrim
|
||||
import eu.kanade.tachiyomi.util.system.openInBrowser
|
||||
import eu.kanade.tachiyomi.util.view.setComposeContent
|
||||
import kotlinx.coroutines.Deferred
|
||||
import kotlinx.coroutines.awaitAll
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
import kotlinx.coroutines.flow.collectLatest
|
||||
|
@@ -3,7 +3,7 @@ package mihon.core.migration
|
||||
interface Migration {
|
||||
val version: Float
|
||||
|
||||
suspend fun action(migrationContext: MigrationContext): Boolean
|
||||
suspend operator fun invoke(migrationContext: MigrationContext): Boolean
|
||||
|
||||
companion object {
|
||||
const val ALWAYS = -1f
|
||||
@@ -11,7 +11,7 @@ interface Migration {
|
||||
fun of(version: Float, action: suspend (MigrationContext) -> Boolean): Migration = object : Migration {
|
||||
override val version: Float = version
|
||||
|
||||
override suspend fun action(migrationContext: MigrationContext): Boolean {
|
||||
override suspend operator fun invoke(migrationContext: MigrationContext): Boolean {
|
||||
return action(migrationContext)
|
||||
}
|
||||
}
|
||||
|
@@ -1,7 +1,5 @@
|
||||
package mihon.core.migration
|
||||
|
||||
import kotlinx.coroutines.CoroutineScope
|
||||
import kotlinx.coroutines.Dispatchers
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import tachiyomi.core.common.util.system.logcat
|
||||
|
||||
@@ -16,51 +14,40 @@ object Migrator {
|
||||
onMigrationComplete: () -> Unit
|
||||
): Boolean {
|
||||
val migrationContext = MigrationContext()
|
||||
val coroutineScope = CoroutineScope(Dispatchers.IO)
|
||||
|
||||
val migrationsByVersion = migrations.groupBy { it.version.toInt() }
|
||||
val always = listOf(Migration.ALWAYS.toInt())
|
||||
|
||||
if (old == 0) {
|
||||
onMigrationComplete()
|
||||
return with(coroutineScope) {
|
||||
migrationContext.migrate(always, migrationsByVersion, dryrun)
|
||||
}
|
||||
return migrationContext.migrate(
|
||||
migrations = migrations.filter { it.isAlways() },
|
||||
dryrun = dryrun
|
||||
)
|
||||
.also { onMigrationComplete() }
|
||||
}
|
||||
|
||||
if (old >= new) {
|
||||
return false
|
||||
}
|
||||
|
||||
onMigrationComplete()
|
||||
val versions = migrationsByVersion.keys.filter { version -> version in (old + 1)..new }
|
||||
return with(coroutineScope) {
|
||||
migrationContext.migrate(always + versions, migrationsByVersion, dryrun)
|
||||
}
|
||||
return migrationContext.migrate(
|
||||
migrations = migrations.filter { it.isAlways() || it.version.toInt() in (old + 1)..new },
|
||||
dryrun = dryrun
|
||||
)
|
||||
.also { onMigrationComplete() }
|
||||
}
|
||||
|
||||
context (CoroutineScope)
|
||||
private fun Migration.isAlways() = version == Migration.ALWAYS
|
||||
|
||||
@SuppressWarnings("MaxLineLength")
|
||||
private fun MigrationContext.migrate(
|
||||
versions: List<Int>,
|
||||
migrationsByVersion: Map<Int, List<Migration>>,
|
||||
dryrun: Boolean
|
||||
): Boolean {
|
||||
var aBoolean = false
|
||||
for (version in versions.sorted()) {
|
||||
val migrations = migrationsByVersion.getOrDefault(version, emptyList()).sortedBy(Migration::version)
|
||||
for (migration in migrations) {
|
||||
val success = runBlocking {
|
||||
if (!dryrun) {
|
||||
logcat { "Running migration: { name = ${migration::class.simpleName}, version = ${migration.version} }" }
|
||||
} else {
|
||||
logcat { "(Dry-run) Running migration: { name = ${migration::class.simpleName}, version = ${migration.version} }" }
|
||||
}
|
||||
migration.action(this@migrate)
|
||||
private fun MigrationContext.migrate(migrations: List<Migration>, dryrun: Boolean): Boolean {
|
||||
return migrations.sortedBy { it.version }
|
||||
.map { migration ->
|
||||
if (!dryrun) {
|
||||
logcat { "Running migration: { name = ${migration::class.simpleName}, version = ${migration.version} }" }
|
||||
runBlocking { migration(this@migrate) }
|
||||
} else {
|
||||
logcat { "(Dry-run) Running migration: { name = ${migration::class.simpleName}, version = ${migration.version} }" }
|
||||
true
|
||||
}
|
||||
aBoolean = success || aBoolean
|
||||
}
|
||||
}
|
||||
return aBoolean
|
||||
.reduce { acc, b -> acc || b }
|
||||
}
|
||||
}
|
||||
|
@@ -8,7 +8,7 @@ import mihon.core.migration.MigrationContext
|
||||
class SetupBackupCreateMigration : Migration {
|
||||
override val version: Float = Migration.ALWAYS
|
||||
|
||||
override suspend fun action(migrationContext: MigrationContext): Boolean {
|
||||
override suspend fun invoke(migrationContext: MigrationContext): Boolean {
|
||||
val context = migrationContext.get<App>() ?: return false
|
||||
BackupCreateJob.setupTask(context)
|
||||
return true
|
||||
|
@@ -8,7 +8,7 @@ import mihon.core.migration.MigrationContext
|
||||
class SetupLibraryUpdateMigration : Migration {
|
||||
override val version: Float = Migration.ALWAYS
|
||||
|
||||
override suspend fun action(migrationContext: MigrationContext): Boolean {
|
||||
override suspend fun invoke(migrationContext: MigrationContext): Boolean {
|
||||
val context = migrationContext.get<App>() ?: return false
|
||||
LibraryUpdateJob.setupTask(context)
|
||||
return true
|
||||
|
@@ -12,7 +12,7 @@ import tachiyomi.core.common.util.system.logcat
|
||||
class TrustExtensionRepositoryMigration : Migration {
|
||||
override val version: Float = 7f
|
||||
|
||||
override suspend fun action(migrationContext: MigrationContext): Boolean = withIOContext {
|
||||
override suspend fun invoke(migrationContext: MigrationContext): Boolean = withIOContext {
|
||||
val sourcePreferences = migrationContext.get<SourcePreferences>() ?: return@withIOContext false
|
||||
val extensionRepositoryRepository =
|
||||
migrationContext.get<ExtensionRepoRepository>() ?: return@withIOContext false
|
||||
|
Reference in New Issue
Block a user