From 0ff89d039b348405d83e7cb7c3e5b7904f7b7239 Mon Sep 17 00:00:00 2001 From: Aria Moradi Date: Tue, 3 Jan 2023 13:19:44 +0330 Subject: [PATCH] fix CategoryMetaTable reference to CategoryTable (#473) --- .../migration/M0023_CategoryMetaRefFix.kt | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0023_CategoryMetaRefFix.kt diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0023_CategoryMetaRefFix.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0023_CategoryMetaRefFix.kt new file mode 100644 index 00000000..fcdc2b91 --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0023_CategoryMetaRefFix.kt @@ -0,0 +1,35 @@ +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 +import org.jetbrains.exposed.sql.transactions.TransactionManager + +@Suppress("ClassName", "unused") +class M0023_CategoryMetaRefFix : SQLMigration() { + fun String.toSqlName(): String = + TransactionManager.defaultDatabase!!.identifierManager.let { + it.quoteIfNecessary( + it.inProperCase(this) + ) + } + + private val CategoryMetaTable by lazy { "CategoryMeta".toSqlName() } + private val CategoryRefColumn by lazy { "category_ref".toSqlName() } + private val CategoryTable by lazy { "Category".toSqlName() } + + override val sql by lazy { + // Incorrectly referenced in M0021 + """ + ALTER TABLE $CategoryMetaTable DROP COLUMN $CategoryRefColumn; + ALTER TABLE $CategoryMetaTable ADD COLUMN $CategoryRefColumn INT DEFAULT 0; + ALTER TABLE $CategoryMetaTable ADD FOREIGN KEY ($CategoryRefColumn) + REFERENCES $CategoryTable(ID) ON DELETE CASCADE; + """ + } +}