From 1f555314365b85fdf207d07b60bc1184e4bad4b2 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Sun, 3 Aug 2025 15:41:30 +0200 Subject: [PATCH] Fix handling of disabled manga migration steps In case e.g., the "tracking" should not get migrated, the "migration action" was still created. While creating the action, an error was thrown because the "tracking" information was missing, as expected, since it's not required. To prevent this, the actions should only get created in case they are actually required for the migration. --- src/modules/manga/services/Mangas.ts | 61 +++++++++++++++++----------- 1 file changed, 37 insertions(+), 24 deletions(-) diff --git a/src/modules/manga/services/Mangas.ts b/src/modules/manga/services/Mangas.ts index aeae9921..2b006a41 100644 --- a/src/modules/manga/services/Mangas.ts +++ b/src/modules/manga/services/Mangas.ts @@ -98,7 +98,8 @@ type PerformActionOptions = Action extends 'mark_as_ ? DownloadActionOption : DefaultActionOption; -type MigrateFuncReturn = { copy: () => Promise[]; cleanup: () => Promise[] }; +type MigrateAction = { copy: () => Promise[]; cleanup: () => Promise[] }; +type MigrateActionCreator = () => MigrateAction; const ARTIST_AUTHOR_SEPARATOR_REGEX = /\s*[,|、]\s*/; @@ -313,7 +314,7 @@ export class Mangas { mangaToMigrate: GetMangaToMigrateQuery['manga'], mangaToMigrateToInfo: GetMangaToMigrateToFetchMutation, deleteChapters: boolean, - ): MigrateFuncReturn { + ): MigrateAction { if (!mangaToMigrate.chapters || !mangaToMigrateToInfo.fetchChapters?.chapters) { throw new Error('Chapters are missing'); } @@ -362,7 +363,7 @@ export class Mangas { mode: MigrateMode, mangaToMigrate: MangaToMigrate, mangaToMigrateTo: MangaToMigrateTo, - ): MigrateFuncReturn { + ): MigrateAction { if (!mangaToMigrate.trackRecords) { throw new Error('TrackRecords of manga to migrate are missing'); } @@ -403,7 +404,7 @@ export class Mangas { mangaToMigrateTo: MangaToMigrateTo, migrateCategories: boolean, removeMangaFromCategories: boolean, - ): MigrateFuncReturn { + ): MigrateAction { if (!mangaToMigrateFrom?.categories) { throw new Error('Categories are missing'); } @@ -477,18 +478,18 @@ export class Mangas { } const performMigrationAction = async ( - migrateAction: keyof MigrateFuncReturn, - ...actions: [boolean | undefined, MigrateFuncReturn][] - ) => - Promise.all( - actions - .filter(([performAction]) => performAction) - .map(([, action]) => action[migrateAction]()) - .flat(), - ); + migrateAction: keyof MigrateAction, + ...actions: MigrateAction[] + ) => Promise.all(actions.map((action) => action[migrateAction]()).flat()); - const performMigrationActions = async (...actions: [boolean | undefined, MigrateFuncReturn][]) => { - const migrationActions: TupleUnion = ['copy', 'cleanup']; + const performMigrationActions = async ( + ...actionCreators: [boolean | undefined, MigrateActionCreator][] + ) => { + const migrationActions: TupleUnion = ['copy', 'cleanup']; + + const actions = actionCreators + .filter(([performAction]) => performAction) + .map(([, actionCreator]) => actionCreator()); for (const migrationAction of migrationActions) { // the migration actions (copy, cleanup) are supposed to be run sequentially to ensure that the cleanup @@ -501,21 +502,33 @@ export class Mangas { await performMigrationActions( [ migrateChapters, - Mangas.migrateChapters(mode, mangaToMigrateData.manga, mangaToMigrateToData, !!deleteChapters), + () => + Mangas.migrateChapters( + mode, + mangaToMigrateData.manga, + mangaToMigrateToData, + !!deleteChapters, + ), ], [ migrateTracking, - Mangas.migrateTracking(mode, mangaToMigrateData.manga, mangaToMigrateToData.fetchManga.manga), + () => + Mangas.migrateTracking( + mode, + mangaToMigrateData.manga, + mangaToMigrateToData.fetchManga!.manga, + ), ], [ true, - Mangas.migrateManga( - mode, - mangaToMigrateData.manga, - mangaToMigrateToData.fetchManga.manga, - !!migrateCategories, - removeMangaFromCategories, - ), + () => + Mangas.migrateManga( + mode, + mangaToMigrateData.manga, + mangaToMigrateToData.fetchManga!.manga, + !!migrateCategories, + removeMangaFromCategories, + ), ], ); },