Fix New Databases (#2016)
* Standardize toSqlName * Rename Meta Key db field since KEY is now a reserved name in H2 * Changelog entry * Use toSqlName * Forgot this key * Catch any exception
This commit is contained in:
@@ -13,6 +13,6 @@ import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
||||
* Metadata storage for clients, server/global level.
|
||||
*/
|
||||
object GlobalMetaTable : IntIdTable() {
|
||||
val key = varchar("key", 256)
|
||||
val key = varchar("meta_key", 256)
|
||||
val value = varchar("value", 4096)
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@ import suwayomi.tachidesk.manga.model.table.CategoryMetaTable.ref
|
||||
* Metadata storage for clients, about Category with id == [ref].
|
||||
*/
|
||||
object CategoryMetaTable : IntIdTable() {
|
||||
val key = varchar("key", 256)
|
||||
val key = varchar("meta_key", 256)
|
||||
val value = varchar("value", 4096)
|
||||
val ref = reference("category_ref", CategoryTable, ReferenceOption.CASCADE)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import suwayomi.tachidesk.manga.model.table.ChapterMetaTable.ref
|
||||
* }
|
||||
*/
|
||||
object ChapterMetaTable : IntIdTable() {
|
||||
val key = varchar("key", 256)
|
||||
val key = varchar("meta_key", 256)
|
||||
val value = varchar("value", 4096)
|
||||
val ref = reference("chapter_ref", ChapterTable, ReferenceOption.CASCADE)
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import suwayomi.tachidesk.manga.model.table.MangaMetaTable.ref
|
||||
* }
|
||||
*/
|
||||
object MangaMetaTable : IntIdTable() {
|
||||
val key = varchar("key", 256)
|
||||
val key = varchar("meta_key", 256)
|
||||
val value = varchar("value", 4096)
|
||||
val ref = reference("manga_ref", MangaTable, ReferenceOption.CASCADE)
|
||||
}
|
||||
|
||||
@@ -14,7 +14,7 @@ import suwayomi.tachidesk.manga.model.table.SourceMetaTable.ref
|
||||
* Metadata storage for clients, about Source with id == [ref].
|
||||
*/
|
||||
object SourceMetaTable : IntIdTable() {
|
||||
val key = varchar("key", 256)
|
||||
val key = varchar("meta_key", 256)
|
||||
val value = varchar("value", 4096)
|
||||
val ref = long("source_ref")
|
||||
}
|
||||
|
||||
@@ -27,7 +27,6 @@ import suwayomi.tachidesk.server.util.ExitCode
|
||||
import suwayomi.tachidesk.server.util.shutdownApp
|
||||
import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
import java.sql.SQLException
|
||||
import kotlin.time.Duration.Companion.minutes
|
||||
import kotlin.time.Duration.Companion.seconds
|
||||
|
||||
@@ -184,7 +183,7 @@ fun databaseUp() {
|
||||
}
|
||||
val migrations = loadMigrationsFrom("suwayomi.tachidesk.server.database.migration", ServerConfig::class.java)
|
||||
runMigrations(migrations)
|
||||
} catch (e: SQLException) {
|
||||
} catch (e: Exception) {
|
||||
logger.error(e) { "Error up-to-database migration" }
|
||||
if (System.getProperty("crashOnFailedMigration").toBoolean()) {
|
||||
shutdownApp(ExitCode.DbMigrationFailure)
|
||||
|
||||
+1
-8
@@ -10,17 +10,10 @@ package suwayomi.tachidesk.server.database.migration
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
import de.neonew.exposed.migrations.helpers.SQLMigration
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.TransactionManager
|
||||
import suwayomi.tachidesk.server.database.migration.helpers.toSqlName
|
||||
|
||||
@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() }
|
||||
|
||||
+4
-2
@@ -8,6 +8,7 @@ package suwayomi.tachidesk.server.database.migration
|
||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||
|
||||
import de.neonew.exposed.migrations.helpers.SQLMigration
|
||||
import suwayomi.tachidesk.server.database.migration.helpers.toSqlName
|
||||
|
||||
@Suppress("ClassName", "unused")
|
||||
class M0049_FixDuplicatedMetas : SQLMigration() {
|
||||
@@ -15,7 +16,7 @@ class M0049_FixDuplicatedMetas : SQLMigration() {
|
||||
table: String,
|
||||
refColumn: String? = null,
|
||||
): String {
|
||||
val groupBy = listOfNotNull(refColumn, "KEY").joinToString(", ")
|
||||
val groupBy = listOfNotNull(refColumn, "KEY".toSqlName()).joinToString(", ")
|
||||
|
||||
return """
|
||||
DELETE FROM $table
|
||||
@@ -30,10 +31,11 @@ class M0049_FixDuplicatedMetas : SQLMigration() {
|
||||
""".trimIndent()
|
||||
}
|
||||
|
||||
override val sql: String =
|
||||
override val sql: String by lazy {
|
||||
createMigrationForTable("CATEGORYMETA", "CATEGORY_REF") +
|
||||
createMigrationForTable("CHAPTERMETA", "CHAPTER_REF") +
|
||||
createMigrationForTable("GLOBALMETA") +
|
||||
createMigrationForTable("MANGAMETA", "MANGA_REF") +
|
||||
createMigrationForTable("SOURCEMETA", "SOURCE_REF")
|
||||
}
|
||||
}
|
||||
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
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 suwayomi.tachidesk.graphql.types.DatabaseType
|
||||
import suwayomi.tachidesk.server.database.migration.helpers.toSqlName
|
||||
import suwayomi.tachidesk.server.serverConfig
|
||||
|
||||
@Suppress("ClassName", "unused")
|
||||
class M0055_RenameMetaKeys : SQLMigration() {
|
||||
fun postgresRename(table: String): String =
|
||||
"ALTER TABLE $table " +
|
||||
"RENAME COLUMN " + "KEY".toSqlName() + " TO META_KEY;"
|
||||
|
||||
fun h2Rename(table: String): String =
|
||||
"ALTER TABLE $table " +
|
||||
"ALTER COLUMN " + "KEY".toSqlName() + " RENAME TO META_KEY;"
|
||||
|
||||
fun createRenameMigration(table: String): String =
|
||||
when (serverConfig.databaseType.value) {
|
||||
DatabaseType.H2 -> h2Rename(table.toSqlName())
|
||||
DatabaseType.POSTGRESQL -> postgresRename(table.toSqlName())
|
||||
}
|
||||
|
||||
override val sql: String by lazy {
|
||||
createRenameMigration("CATEGORYMETA") +
|
||||
createRenameMigration("CHAPTERMETA") +
|
||||
createRenameMigration("GLOBALMETA") +
|
||||
createRenameMigration("MANGAMETA") +
|
||||
createRenameMigration("SOURCEMETA")
|
||||
}
|
||||
}
|
||||
-8
@@ -1,17 +1,9 @@
|
||||
package suwayomi.tachidesk.server.database.migration.helpers
|
||||
|
||||
import de.neonew.exposed.migrations.helpers.SQLMigration
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.TransactionManager
|
||||
import suwayomi.tachidesk.graphql.types.DatabaseType
|
||||
import suwayomi.tachidesk.server.serverConfig
|
||||
|
||||
fun String.toSqlName(): String =
|
||||
TransactionManager.current().db.identifierManager.let {
|
||||
it.quoteIfNecessary(
|
||||
it.inProperCase(this),
|
||||
)
|
||||
}
|
||||
|
||||
abstract class RenameFieldMigration(
|
||||
tableName: String,
|
||||
originalName: String,
|
||||
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package suwayomi.tachidesk.server.database.migration.helpers
|
||||
|
||||
import org.jetbrains.exposed.v1.jdbc.transactions.TransactionManager
|
||||
|
||||
fun String.toSqlName(): String =
|
||||
TransactionManager.current().db.identifierManager.let {
|
||||
it.quoteIfNecessary(
|
||||
it.inProperCase(this),
|
||||
)
|
||||
}
|
||||
Reference in New Issue
Block a user