package eu.friedcutlet.yokan.database.sync import eu.friedcutlet.yokan.database.YokanDatabase import eu.friedcutlet.yokan.database.YokanDatabaseSnapshot /** * One-time rewrite of local auto-increment ids to [SyncIds] so this device can merge with others. * Runs once per install (tracked in `DbSettings.syncIdentityVersion`); every foreign key and every * keyed settings list follows the rewritten ids. Duplicates that collapse onto one stable id keep * the most recently updated row. */ object SyncIdentityMigration { const val VERSION = 1 fun ensure(db: YokanDatabase) { val current = db.snapshot() if (current.settings.syncIdentityVersion >= VERSION) return val remapped = remap(current) db.replaceAll( remapped.copy(settings = remapped.settings.copy(syncIdentityVersion = VERSION)), expectedSnapshot = current, ) } fun remap(s: YokanDatabaseSnapshot): YokanDatabaseSnapshot { val mangaIds = HashMap() val mangas = s.mangas .map { m -> m.copy(id = SyncIds.forManga(m) ?: m.id).also { mangaIds[m.id] = it.id } } .dedupe { it.id to it.updatedAt } val categoryIds = HashMap() val categories = s.categories.map { c -> c.copy(id = SyncIds.random()).also { categoryIds[c.id] = it.id } } val mangasWithCategories = mangas.map { m -> m.copy(categories = m.categories?.map { categoryIds[it] ?: it }?.distinct()) } val chapterIds = HashMap() val chapters = s.chapters .map { c -> val moved = c.copy(mangaId = c.mangaId?.let { mangaIds[it] ?: it }) moved.copy(id = SyncIds.forChapter(moved) ?: c.id).also { chapterIds[c.id] = it.id } } .dedupe { it.id to it.updatedAt } fun manga(id: Long?) = id?.let { mangaIds[it] ?: it } fun chapter(id: Long?) = id?.let { chapterIds[it] ?: it } val histories = s.histories .map { h -> val moved = h.copy(mangaId = manga(h.mangaId), chapterId = chapter(h.chapterId)) moved.copy(id = SyncIds.forHistory(moved) ?: h.id) } .dedupe { it.id to it.updatedAt } val updates = s.updates .map { u -> val moved = u.copy(mangaId = manga(u.mangaId), chapterId = chapter(u.chapterId)) moved.copy(id = SyncIds.forUpdate(moved) ?: u.id) } .dedupe { it.id to it.updatedAt } val tracks = s.tracks .map { t -> val moved = t.copy(mangaId = manga(t.mangaId)) moved.copy(id = SyncIds.forTrack(moved) ?: t.id) } .dedupe { it.id to it.updatedAt } val downloads = s.downloads.map { d -> d.copy(chapterId = chapter(d.chapterId)) } val pageCache = s.pageCache.map { p -> p.copy(chapterId = chapter(p.chapterId)) } val st = s.settings val settings = st.copy( chapterReleasePreferences = st.chapterReleasePreferences.map { it.copy(mangaId = manga(it.mangaId)) }, automaticReaderLayouts = st.automaticReaderLayouts.map { it.copy(mangaId = manga(it.mangaId) ?: it.mangaId) }, sortChapterList = st.sortChapterList?.map { it.copy(mangaId = manga(it.mangaId)) }, chapterFilterDownloadedList = st.chapterFilterDownloadedList?.map { it.copy(mangaId = manga(it.mangaId)) }, chapterFilterUnreadList = st.chapterFilterUnreadList?.map { it.copy(mangaId = manga(it.mangaId)) }, chapterFilterBookmarkedList = st.chapterFilterBookmarkedList?.map { it.copy(mangaId = manga(it.mangaId)) }, chapterPageIndexList = st.chapterPageIndexList?.map { it.copy(chapterId = chapter(it.chapterId)) }, personalReaderModeList = st.personalReaderModeList?.map { it.copy(mangaId = manga(it.mangaId)) }, filterScanlatorList = st.filterScanlatorList?.map { it.copy(mangaId = manga(it.mangaId)) }, personalPageModeList = st.personalPageModeList?.map { it.copy(mangaId = manga(it.mangaId)) }, autoScrollPages = st.autoScrollPages?.map { it.copy(mangaId = manga(it.mangaId)) }, titleMetadata = st.titleMetadata?.map { it.copy(mangaId = manga(it.mangaId) ?: it.mangaId) }, ) return s.copy( settings = settings, mangas = mangasWithCategories, chapters = chapters, categories = categories, histories = histories, updates = updates, tracks = tracks, downloads = downloads, pageCache = pageCache, changedParts = emptyList(), outbox = emptyList(), ) } /** Keep one row per id: the one with the greatest secondary key (updatedAt). */ private inline fun List.dedupe(key: (T) -> Pair): List { val best = LinkedHashMap() for (item in this) { val (id, stamp) = key(item) val current = best[id] if (current == null || key(current).second <= stamp) best[id] = item } return best.values.toList() } }