-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Seperate out migrations to allow run-once migrations (#882)
* Seperate out migrations to allow run-once migrations * Previous * Move migrations to a new file
- Loading branch information
Showing
2 changed files
with
87 additions
and
61 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
server/src/main/kotlin/suwayomi/tachidesk/server/Migration.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package suwayomi.tachidesk.server | ||
|
||
import android.app.Application | ||
import android.content.Context | ||
import mu.KotlinLogging | ||
import uy.kohesive.injekt.Injekt | ||
import uy.kohesive.injekt.api.get | ||
import java.io.File | ||
import java.util.prefs.Preferences | ||
|
||
private fun migratePreferences( | ||
parent: String?, | ||
rootNode: Preferences, | ||
) { | ||
val subNodes = rootNode.childrenNames() | ||
|
||
for (subNodeName in subNodes) { | ||
val subNode = rootNode.node(subNodeName) | ||
val key = | ||
if (parent != null) { | ||
"$parent/$subNodeName" | ||
} else { | ||
subNodeName | ||
} | ||
val preferences = Injekt.get<Application>().getSharedPreferences(key, Context.MODE_PRIVATE) | ||
|
||
val items: Map<String, String?> = | ||
subNode.keys().associateWith { | ||
subNode[it, null]?.ifBlank { null } | ||
} | ||
|
||
preferences.edit().apply { | ||
items.forEach { (key, value) -> | ||
if (value != null) { | ||
putString(key, value) | ||
} | ||
} | ||
}.apply() | ||
|
||
migratePreferences(key, subNode) // Recursively migrate sub-level nodes | ||
} | ||
} | ||
|
||
const val MIGRATION_VERSION = 1 | ||
|
||
fun runMigrations(applicationDirs: ApplicationDirs) { | ||
val migrationPreferences = | ||
Injekt.get<Application>() | ||
.getSharedPreferences( | ||
"migrations", | ||
Context.MODE_PRIVATE, | ||
) | ||
val version = migrationPreferences.getInt("version", 0) | ||
val logger = KotlinLogging.logger("Migration") | ||
logger.info { "Running migrations, previous version $version, target version $MIGRATION_VERSION" } | ||
|
||
if (version < 1) { | ||
logger.info { "Running migration for version: 1" } | ||
val oldMangaDownloadDir = File(applicationDirs.downloadsRoot) | ||
val newMangaDownloadDir = File(applicationDirs.mangaDownloadsRoot) | ||
val downloadDirs = oldMangaDownloadDir.listFiles().orEmpty() | ||
|
||
val moveDownloadsToNewFolder = !newMangaDownloadDir.exists() && downloadDirs.isNotEmpty() | ||
if (moveDownloadsToNewFolder) { | ||
newMangaDownloadDir.mkdirs() | ||
|
||
for (downloadDir in downloadDirs) { | ||
if (downloadDir == File(applicationDirs.thumbnailDownloadsRoot)) { | ||
continue | ||
} | ||
|
||
downloadDir.renameTo(File(newMangaDownloadDir, downloadDir.name)) | ||
} | ||
} | ||
|
||
// Migrate from old preferences api | ||
val prefRootNode = "suwayomi/tachidesk" | ||
val isMigrationRequired = Preferences.userRoot().nodeExists(prefRootNode) | ||
if (isMigrationRequired) { | ||
val preferences = Preferences.userRoot().node(prefRootNode) | ||
migratePreferences(null, preferences) | ||
preferences.removeNode() | ||
} | ||
} | ||
migrationPreferences.edit().putInt("version", MIGRATION_VERSION).apply() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters