Prevent negative lastPageRead values (#1267)

Co-authored-by: Mitchell Syer <Syer10@users.noreply.github.com>
This commit is contained in:
schroda
2025-02-15 01:06:54 +01:00
committed by GitHub
parent 733ba16af2
commit c8bd39b4bf
4 changed files with 28 additions and 5 deletions

View File

@@ -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
}
}

View File

@@ -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]
}
}

View File

@@ -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)
}
}
}

View File

@@ -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()
}