Streamline deprecated settings config value migration logic (#1633)

* Streamline deprecated settings config value migration logic

* Add "autoDownloadAheadLimit" config migration

For consistency

* Replace "exitCode" with "shutdownApp"

* Enhance shutdown logging to include exit reason
This commit is contained in:
schroda
2025-09-13 18:22:09 +02:00
committed by GitHub
parent 904d3980d6
commit 904157a91a
5 changed files with 81 additions and 56 deletions
@@ -37,7 +37,6 @@ import org.koin.core.context.startKoin
import org.koin.core.module.Module
import org.koin.dsl.module
import suwayomi.tachidesk.global.impl.KcefWebView.Companion.toCefCookie
import suwayomi.tachidesk.graphql.types.AuthMode
import suwayomi.tachidesk.graphql.types.DatabaseType
import suwayomi.tachidesk.i18n.LocalizationHelper
import suwayomi.tachidesk.manga.impl.backup.proto.ProtoBackupExport
@@ -47,9 +46,12 @@ import suwayomi.tachidesk.manga.impl.update.Updater
import suwayomi.tachidesk.manga.impl.util.lang.renameTo
import suwayomi.tachidesk.server.database.databaseUp
import suwayomi.tachidesk.server.generated.BuildConfig
import suwayomi.tachidesk.server.settings.SettingsRegistry
import suwayomi.tachidesk.server.util.AppMutex.handleAppMutex
import suwayomi.tachidesk.server.util.ConfigTypeRegistration
import suwayomi.tachidesk.server.util.ExitCode
import suwayomi.tachidesk.server.util.SystemTray
import suwayomi.tachidesk.server.util.shutdownApp
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import xyz.nulldev.androidcompat.AndroidCompat
@@ -142,17 +144,18 @@ fun setupLogLevelUpdating(
)
}
fun <T : Any> migrateConfig(
fun migrateConfig(
configDocument: ConfigDocument,
config: Config,
configKey: String,
toConfigKey: String,
toType: (ConfigValue) -> T?,
toType: (ConfigValue) -> Any?,
): ConfigDocument {
try {
val configValue = config.getValue(configKey)
val typedValue = toType(configValue)
if (typedValue != null) {
logger.debug { "Migrating config value: $configKey -> $toConfigKey" }
return configDocument.withValue(
toConfigKey,
typedValue.toConfig("internal").getValue("internal"),
@@ -309,59 +312,31 @@ fun applicationSetup() {
// make sure the user config file is up-to-date
GlobalConfigManager.updateUserConfig { config ->
var updatedConfig = this
updatedConfig =
migrateConfig(
updatedConfig,
config,
"server.basicAuthEnabled",
"server.authMode",
toType = {
if (it.unwrapped() as? Boolean == true) {
AuthMode.BASIC_AUTH.name
} else {
null
}
},
)
updatedConfig =
migrateConfig(
updatedConfig,
config,
"server.basicAuthUsername",
"server.authUsername",
toType = { it.unwrapped() as? String },
)
updatedConfig =
migrateConfig(
updatedConfig,
config,
"server.basicAuthPassword",
"server.authPassword",
toType = { it.unwrapped() as? String },
)
// Migrate KOReader sync strategy from single to forward/backward strategies
try {
val oldStrategy = config.getString("server.koreaderSyncStrategy")
val (forward, backward) =
when (oldStrategy.uppercase()) {
"PROMPT" -> "PROMPT" to "PROMPT"
"SILENT" -> "KEEP_REMOTE" to "KEEP_LOCAL"
"SEND" -> "KEEP_LOCAL" to "KEEP_LOCAL"
"RECEIVE" -> "KEEP_REMOTE" to "KEEP_REMOTE"
"DISABLED" -> "DISABLED" to "DISABLED"
else -> null to null
}
val settingsRequiringMigration = SettingsRegistry.getAll().filterValues { it.deprecated?.replaceWith != null }
settingsRequiringMigration.forEach { (name, data) ->
val configKey = "server.$name"
val toConfigKey = "server.${data.deprecated!!.replaceWith}"
if (forward != null && backward != null) {
updatedConfig =
updatedConfig
.withValue("server.koreaderSyncStrategyForward", forward.toConfig("internal").getValue("internal"))
.withValue("server.koreaderSyncStrategyBackward", backward.toConfig("internal").getValue("internal"))
.withoutPath("server.koreaderSyncStrategy")
if (data.deprecated!!.migrateConfig != null) {
logger.debug { "Migrating config value: $configKey -> $toConfigKey" }
updatedConfig = data.deprecated!!.migrateConfig!!(config.getValue(configKey), updatedConfig)
return@forEach
}
} catch (_: ConfigException.Missing) {
// Key doesn't exist, no migration needed
if (data.deprecated!!.migrateConfigValue != null) {
updatedConfig =
migrateConfig(
updatedConfig,
config,
configKey,
toConfigKey,
data.deprecated!!.migrateConfigValue!!,
)
return@forEach
}
shutdownApp(ExitCode.ConfigMigrationMisconfiguredFailure)
}
updatedConfig
@@ -21,10 +21,11 @@ import suwayomi.tachidesk.graphql.types.DatabaseType
import suwayomi.tachidesk.server.ApplicationDirs
import suwayomi.tachidesk.server.ServerConfig
import suwayomi.tachidesk.server.serverConfig
import suwayomi.tachidesk.server.util.ExitCode
import suwayomi.tachidesk.server.util.shutdownApp
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import java.sql.SQLException
import kotlin.system.exitProcess
object DBManager {
var db: Database? = null
@@ -90,7 +91,7 @@ fun databaseUp() {
} catch (e: SQLException) {
logger.error(e) { "Error up-to-database migration" }
if (System.getProperty("crashOnFailedMigration").toBoolean()) {
exitProcess(101)
shutdownApp(ExitCode.DbMigrationFailure)
}
}
}
@@ -19,10 +19,12 @@ enum class ExitCode(
MutexCheckFailedTachideskRunning(1),
MutexCheckFailedAnotherAppRunning(2),
WebUISetupFailure(3),
ConfigMigrationMisconfiguredFailure(4),
DbMigrationFailure(5),
}
fun shutdownApp(exitCode: ExitCode) {
logger.info { "Shutting Down Suwayomi-Server. Goodbye!" }
logger.info { "Shutting Down Suwayomi-Server. Goodbye! (reason= ${exitCode.code} (${exitCode.name}))" }
exitProcess(exitCode.code)
}