mirror of
https://github.com/Suwayomi/Suwayomi-Server.git
synced 2026-06-30 17:34:39 -05:00
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:
@@ -10,7 +10,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
|
|||||||
- .
|
- .
|
||||||
|
|
||||||
### Changed
|
### Changed
|
||||||
- .
|
- (Database/H2) Use the latest H2 database engine
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
- (CloudFlareInterceptor) Don't send the `cf_clearance` cookie back to Flaresolverr
|
- (CloudFlareInterceptor) Don't send the `cf_clearance` cookie back to Flaresolverr
|
||||||
|
|||||||
@@ -13,6 +13,6 @@ import org.jetbrains.exposed.v1.core.dao.id.IntIdTable
|
|||||||
* Metadata storage for clients, server/global level.
|
* Metadata storage for clients, server/global level.
|
||||||
*/
|
*/
|
||||||
object GlobalMetaTable : IntIdTable() {
|
object GlobalMetaTable : IntIdTable() {
|
||||||
val key = varchar("key", 256)
|
val key = varchar("meta_key", 256)
|
||||||
val value = varchar("value", 4096)
|
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].
|
* Metadata storage for clients, about Category with id == [ref].
|
||||||
*/
|
*/
|
||||||
object CategoryMetaTable : IntIdTable() {
|
object CategoryMetaTable : IntIdTable() {
|
||||||
val key = varchar("key", 256)
|
val key = varchar("meta_key", 256)
|
||||||
val value = varchar("value", 4096)
|
val value = varchar("value", 4096)
|
||||||
val ref = reference("category_ref", CategoryTable, ReferenceOption.CASCADE)
|
val ref = reference("category_ref", CategoryTable, ReferenceOption.CASCADE)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import suwayomi.tachidesk.manga.model.table.ChapterMetaTable.ref
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
object ChapterMetaTable : IntIdTable() {
|
object ChapterMetaTable : IntIdTable() {
|
||||||
val key = varchar("key", 256)
|
val key = varchar("meta_key", 256)
|
||||||
val value = varchar("value", 4096)
|
val value = varchar("value", 4096)
|
||||||
val ref = reference("chapter_ref", ChapterTable, ReferenceOption.CASCADE)
|
val ref = reference("chapter_ref", ChapterTable, ReferenceOption.CASCADE)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ import suwayomi.tachidesk.manga.model.table.MangaMetaTable.ref
|
|||||||
* }
|
* }
|
||||||
*/
|
*/
|
||||||
object MangaMetaTable : IntIdTable() {
|
object MangaMetaTable : IntIdTable() {
|
||||||
val key = varchar("key", 256)
|
val key = varchar("meta_key", 256)
|
||||||
val value = varchar("value", 4096)
|
val value = varchar("value", 4096)
|
||||||
val ref = reference("manga_ref", MangaTable, ReferenceOption.CASCADE)
|
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].
|
* Metadata storage for clients, about Source with id == [ref].
|
||||||
*/
|
*/
|
||||||
object SourceMetaTable : IntIdTable() {
|
object SourceMetaTable : IntIdTable() {
|
||||||
val key = varchar("key", 256)
|
val key = varchar("meta_key", 256)
|
||||||
val value = varchar("value", 4096)
|
val value = varchar("value", 4096)
|
||||||
val ref = long("source_ref")
|
val ref = long("source_ref")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -27,7 +27,6 @@ import suwayomi.tachidesk.server.util.ExitCode
|
|||||||
import suwayomi.tachidesk.server.util.shutdownApp
|
import suwayomi.tachidesk.server.util.shutdownApp
|
||||||
import uy.kohesive.injekt.Injekt
|
import uy.kohesive.injekt.Injekt
|
||||||
import uy.kohesive.injekt.api.get
|
import uy.kohesive.injekt.api.get
|
||||||
import java.sql.SQLException
|
|
||||||
import kotlin.time.Duration.Companion.minutes
|
import kotlin.time.Duration.Companion.minutes
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
@@ -184,7 +183,7 @@ fun databaseUp() {
|
|||||||
}
|
}
|
||||||
val migrations = loadMigrationsFrom("suwayomi.tachidesk.server.database.migration", ServerConfig::class.java)
|
val migrations = loadMigrationsFrom("suwayomi.tachidesk.server.database.migration", ServerConfig::class.java)
|
||||||
runMigrations(migrations)
|
runMigrations(migrations)
|
||||||
} catch (e: SQLException) {
|
} catch (e: Exception) {
|
||||||
logger.error(e) { "Error up-to-database migration" }
|
logger.error(e) { "Error up-to-database migration" }
|
||||||
if (System.getProperty("crashOnFailedMigration").toBoolean()) {
|
if (System.getProperty("crashOnFailedMigration").toBoolean()) {
|
||||||
shutdownApp(ExitCode.DbMigrationFailure)
|
shutdownApp(ExitCode.DbMigrationFailure)
|
||||||
|
|||||||
@@ -10,17 +10,10 @@ package suwayomi.tachidesk.server.database.migration
|
|||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
import de.neonew.exposed.migrations.helpers.SQLMigration
|
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")
|
@Suppress("ClassName", "unused")
|
||||||
class M0023_CategoryMetaRefFix : SQLMigration() {
|
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 CategoryMetaTable by lazy { "CategoryMeta".toSqlName() }
|
||||||
private val CategoryRefColumn by lazy { "category_ref".toSqlName() }
|
private val CategoryRefColumn by lazy { "category_ref".toSqlName() }
|
||||||
private val CategoryTable by lazy { "Category".toSqlName() }
|
private val CategoryTable by lazy { "Category".toSqlName() }
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ package suwayomi.tachidesk.server.database.migration
|
|||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
||||||
|
|
||||||
import de.neonew.exposed.migrations.helpers.SQLMigration
|
import de.neonew.exposed.migrations.helpers.SQLMigration
|
||||||
|
import suwayomi.tachidesk.server.database.migration.helpers.toSqlName
|
||||||
|
|
||||||
@Suppress("ClassName", "unused")
|
@Suppress("ClassName", "unused")
|
||||||
class M0049_FixDuplicatedMetas : SQLMigration() {
|
class M0049_FixDuplicatedMetas : SQLMigration() {
|
||||||
@@ -15,7 +16,7 @@ class M0049_FixDuplicatedMetas : SQLMigration() {
|
|||||||
table: String,
|
table: String,
|
||||||
refColumn: String? = null,
|
refColumn: String? = null,
|
||||||
): String {
|
): String {
|
||||||
val groupBy = listOfNotNull(refColumn, "KEY").joinToString(", ")
|
val groupBy = listOfNotNull(refColumn, "KEY".toSqlName()).joinToString(", ")
|
||||||
|
|
||||||
return """
|
return """
|
||||||
DELETE FROM $table
|
DELETE FROM $table
|
||||||
@@ -30,10 +31,11 @@ class M0049_FixDuplicatedMetas : SQLMigration() {
|
|||||||
""".trimIndent()
|
""".trimIndent()
|
||||||
}
|
}
|
||||||
|
|
||||||
override val sql: String =
|
override val sql: String by lazy {
|
||||||
createMigrationForTable("CATEGORYMETA", "CATEGORY_REF") +
|
createMigrationForTable("CATEGORYMETA", "CATEGORY_REF") +
|
||||||
createMigrationForTable("CHAPTERMETA", "CHAPTER_REF") +
|
createMigrationForTable("CHAPTERMETA", "CHAPTER_REF") +
|
||||||
createMigrationForTable("GLOBALMETA") +
|
createMigrationForTable("GLOBALMETA") +
|
||||||
createMigrationForTable("MANGAMETA", "MANGA_REF") +
|
createMigrationForTable("MANGAMETA", "MANGA_REF") +
|
||||||
createMigrationForTable("SOURCEMETA", "SOURCE_REF")
|
createMigrationForTable("SOURCEMETA", "SOURCE_REF")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,17 +1,9 @@
|
|||||||
package suwayomi.tachidesk.server.database.migration.helpers
|
package suwayomi.tachidesk.server.database.migration.helpers
|
||||||
|
|
||||||
import de.neonew.exposed.migrations.helpers.SQLMigration
|
import de.neonew.exposed.migrations.helpers.SQLMigration
|
||||||
import org.jetbrains.exposed.v1.jdbc.transactions.TransactionManager
|
|
||||||
import suwayomi.tachidesk.graphql.types.DatabaseType
|
import suwayomi.tachidesk.graphql.types.DatabaseType
|
||||||
import suwayomi.tachidesk.server.serverConfig
|
import suwayomi.tachidesk.server.serverConfig
|
||||||
|
|
||||||
fun String.toSqlName(): String =
|
|
||||||
TransactionManager.current().db.identifierManager.let {
|
|
||||||
it.quoteIfNecessary(
|
|
||||||
it.inProperCase(this),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract class RenameFieldMigration(
|
abstract class RenameFieldMigration(
|
||||||
tableName: String,
|
tableName: String,
|
||||||
originalName: String,
|
originalName: String,
|
||||||
|
|||||||
@@ -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