From e7e76ed68d79a15b5ad58d4e6203fb2cb122715b Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 14 Jul 2025 23:49:27 +0200 Subject: [PATCH] Prevent duplicated meta entries in database (#1517) fixes #1513 --- .../migration/M0049_FixDuplicatedMetas.kt | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0049_FixDuplicatedMetas.kt diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0049_FixDuplicatedMetas.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0049_FixDuplicatedMetas.kt new file mode 100644 index 00000000..a35ff941 --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0049_FixDuplicatedMetas.kt @@ -0,0 +1,39 @@ +package suwayomi.tachidesk.server.database.migration + +/* + * Copyright (C) Contributors to the Suwayomi project + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ + +import de.neonew.exposed.migrations.helpers.SQLMigration + +@Suppress("ClassName", "unused") +class M0049_FixDuplicatedMetas : SQLMigration() { + private fun createMigrationForTable( + table: String, + refColumn: String? = null, + ): String { + val groupBy = listOfNotNull(refColumn, "KEY").joinToString(", ") + + return """ + DELETE FROM $table + WHERE ID NOT IN ( + SELECT MIN(ID) + FROM $table + GROUP BY $groupBy + ); + + ALTER TABLE $table + ADD CONSTRAINT UC_$table UNIQUE ($groupBy); + """.trimIndent() + } + + override val sql: String = + createMigrationForTable("CATEGORYMETA", "CATEGORY_REF") + + createMigrationForTable("CHAPTERMETA", "CHAPTER_REF") + + createMigrationForTable("GLOBALMETA") + + createMigrationForTable("MANGAMETA", "MANGA_REF") + + createMigrationForTable("SOURCEMETA", "SOURCE_REF") +}