diff --git a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt index 5c809293..b40b132b 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/graphql/mutations/ChapterMutation.kt @@ -85,7 +85,7 @@ class ChapterMutation { this[ChapterTable.isBookmarked] = it } patch.lastPageRead?.also { - this[ChapterTable.lastPageRead] = it.coerceAtMost(chapterIdToPageCount[chapterId] ?: 0).coerceAtLeast(0) + this[ChapterTable.lastPageRead] = it.coerceIn(0, chapterIdToPageCount[chapterId]) this[ChapterTable.lastReadAt] = now } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/backup/proto/ProtoBackupImport.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/backup/proto/ProtoBackupImport.kt index b30c0f8c..ea834d50 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/backup/proto/ProtoBackupImport.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/backup/proto/ProtoBackupImport.kt @@ -385,7 +385,7 @@ object ProtoBackupImport : ProtoBackupBase() { this[ChapterTable.manga] = mangaId this[ChapterTable.isRead] = chapter.read - this[ChapterTable.lastPageRead] = chapter.last_page_read + this[ChapterTable.lastPageRead] = chapter.last_page_read.coerceAtLeast(0) this[ChapterTable.isBookmarked] = chapter.bookmark this[ChapterTable.fetchedAt] = TimeUnit.MILLISECONDS.toSeconds(chapter.date_fetch) @@ -414,13 +414,13 @@ object ProtoBackupImport : ProtoBackupBase() { it[ChapterTable.manga] = mangaId it[isRead] = chapter.read - it[lastPageRead] = chapter.last_page_read + it[lastPageRead] = chapter.last_page_read.coerceAtLeast(0) it[isBookmarked] = chapter.bookmark } } else { ChapterTable.update({ (ChapterTable.url eq dbChapter[ChapterTable.url]) and (ChapterTable.manga eq mangaId) }) { it[isRead] = chapter.read || dbChapter[isRead] - it[lastPageRead] = max(chapter.last_page_read, dbChapter[lastPageRead]) + it[lastPageRead] = max(chapter.last_page_read, dbChapter[lastPageRead]).coerceAtLeast(0) it[isBookmarked] = chapter.bookmark || dbChapter[isBookmarked] } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt index ea7e47e3..36e7c41f 100644 --- a/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt +++ b/server/src/main/kotlin/suwayomi/tachidesk/manga/impl/chapter/ChapterForDownload.kt @@ -159,7 +159,7 @@ private class ChapterForDownload( ChapterTable.update({ ChapterTable.id eq chapterId }) { val pageCount = pageList.size it[ChapterTable.pageCount] = pageCount - it[ChapterTable.lastPageRead] = chapterEntry[ChapterTable.lastPageRead].coerceAtMost(pageCount - 1) + it[ChapterTable.lastPageRead] = chapterEntry[ChapterTable.lastPageRead].coerceIn(0, pageCount - 1) } } } diff --git a/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0043_PreventNegativeLastPageRead.kt b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0043_PreventNegativeLastPageRead.kt new file mode 100644 index 00000000..e342d309 --- /dev/null +++ b/server/src/main/kotlin/suwayomi/tachidesk/server/database/migration/M0043_PreventNegativeLastPageRead.kt @@ -0,0 +1,23 @@ +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 M0043_PreventNegativeLastPageRead : SQLMigration() { + override val sql: String = + """ + UPDATE CHAPTER + SET LAST_PAGE_READ = 0 + WHERE LAST_PAGE_READ < 0; + + ALTER TABLE CHAPTER + ADD CONSTRAINT CHK_LAST_READ_PAGE_POSITIVE CHECK (LAST_PAGE_READ >= 0) + """.trimIndent() +}