Merge manga favorite related fields into favorite_at

Migration 16 replaces favorite, date_added and favorite_modified_at with one nullable favorite_at. A favorite whose date_added is 0 falls back to favorite_modified_at. Manga.favorite and dateAdded become getters derived from it, and restore merges it by taking the earliest non-zero time.
This commit is contained in:
AntsyLich
2026-09-27 22:37:02 +06:00
parent f8fff318ba
commit 986f46c09a
18 changed files with 183 additions and 109 deletions
+1 -1
View File
@@ -36,7 +36,7 @@ android {
defaultConfig { defaultConfig {
applicationId = "app.mihon" applicationId = "app.mihon"
versionCode = 31 versionCode = 32
versionName = "0.20.4" versionName = "0.20.4"
buildConfigField("boolean", "TELEMETRY_INCLUDED", "${Config.includeTelemetry}") buildConfigField("boolean", "TELEMETRY_INCLUDED", "${Config.includeTelemetry}")
@@ -48,15 +48,10 @@ class UpdateManga(
} }
suspend fun awaitUpdateFavorite(mangaId: Long, favorite: Boolean): Boolean { suspend fun awaitUpdateFavorite(mangaId: Long, favorite: Boolean): Boolean {
val dateAdded = when (favorite) { val update = when (favorite) {
true -> Clock.System.now().toEpochMilliseconds() true -> MangaUpdate(mangaId) { favoriteAt = Clock.System.now().toEpochMilliseconds() }
false -> 0 false -> MangaUpdate(mangaId) { favoriteAt = null }
} }
return mangaRepository.update( return mangaRepository.update(update)
MangaUpdate(mangaId) {
this.favorite = favorite
this.dateAdded = dateAdded
},
)
} }
} }
@@ -88,12 +88,11 @@ private fun Manga.toBackupManga() =
thumbnailUrl = this.thumbnailUrl, thumbnailUrl = this.thumbnailUrl,
favorite = this.favorite, favorite = this.favorite,
source = this.source, source = this.source,
dateAdded = this.dateAdded, dateAdded = this.favoriteAt ?: 0L,
viewer = (this.viewerFlags.toInt() and ReadingMode.MASK), viewer = (this.viewerFlags.toInt() and ReadingMode.MASK),
viewer_flags = this.viewerFlags.toInt(), viewer_flags = this.viewerFlags.toInt(),
chapterFlags = this.chapterFlags.toInt(), chapterFlags = this.chapterFlags.toInt(),
updateStrategy = this.updateStrategy, updateStrategy = this.updateStrategy,
favoriteModifiedAt = this.favoriteModifiedAt,
notes = this.notes, notes = this.notes,
initialized = this.initialized, initialized = this.initialized,
memo = this.memo.toByteArray(), memo = this.memo.toByteArray(),
@@ -57,13 +57,19 @@ class BackupManga(
genre = this@BackupManga.genre, genre = this@BackupManga.genre,
status = this@BackupManga.status.toLong(), status = this@BackupManga.status.toLong(),
thumbnailUrl = this@BackupManga.thumbnailUrl, thumbnailUrl = this@BackupManga.thumbnailUrl,
favorite = this@BackupManga.favorite,
source = this@BackupManga.source, source = this@BackupManga.source,
dateAdded = this@BackupManga.dateAdded, // For libraries older than dateAdded, the favorite timestamp is the only record of when
// an entry was added. It was written by a trigger in seconds, unlike dateAdded.
favoriteAt = if (this@BackupManga.favorite) {
this@BackupManga.dateAdded.takeIf { it != 0L }
?: this@BackupManga.favoriteModifiedAt?.times(1000)
?: 0L
} else {
null
},
viewerFlags = (this@BackupManga.viewer_flags ?: this@BackupManga.viewer).toLong(), viewerFlags = (this@BackupManga.viewer_flags ?: this@BackupManga.viewer).toLong(),
chapterFlags = this@BackupManga.chapterFlags.toLong(), chapterFlags = this@BackupManga.chapterFlags.toLong(),
updateStrategy = this@BackupManga.updateStrategy, updateStrategy = this@BackupManga.updateStrategy,
favoriteModifiedAt = this@BackupManga.favoriteModifiedAt,
notes = this@BackupManga.notes, notes = this@BackupManga.notes,
initialized = this@BackupManga.initialized, initialized = this@BackupManga.initialized,
memo = this@BackupManga.memo.toJsonObject(), memo = this@BackupManga.memo.toJsonObject(),
@@ -236,17 +236,13 @@ class BrowseSourceViewModel(
val update = if (manga.favorite) { val update = if (manga.favorite) {
val coverLastModified = manga.removeCovers(coverCache).coverLastModified val coverLastModified = manga.removeCovers(coverCache).coverLastModified
MangaUpdate(manga.id) { MangaUpdate(manga.id) {
favorite = false favoriteAt = null
dateAdded = 0
if (coverLastModified != manga.coverLastModified) this.coverLastModified = coverLastModified if (coverLastModified != manga.coverLastModified) this.coverLastModified = coverLastModified
} }
} else { } else {
setMangaDefaultChapterFlags.await(manga) setMangaDefaultChapterFlags.await(manga)
addTracks.bindEnhancedTrackers(manga, sourceManager.getOrStub(manga.source)) addTracks.bindEnhancedTrackers(manga, sourceManager.getOrStub(manga.source))
MangaUpdate(manga.id) { MangaUpdate(manga.id) { favoriteAt = Clock.System.now().toEpochMilliseconds() }
favorite = true
dateAdded = Clock.System.now().toEpochMilliseconds()
}
} }
updateManga.await(update) updateManga.await(update)
@@ -338,7 +338,7 @@ class LibraryViewModel(
manga1.libraryManga.chapterFetchedAt.compareTo(manga2.libraryManga.chapterFetchedAt) manga1.libraryManga.chapterFetchedAt.compareTo(manga2.libraryManga.chapterFetchedAt)
} }
LibrarySort.Type.DateAdded -> { LibrarySort.Type.DateAdded -> {
manga1.libraryManga.manga.dateAdded.compareTo(manga2.libraryManga.manga.dateAdded) compareValues(manga1.libraryManga.manga.favoriteAt, manga2.libraryManga.manga.favoriteAt)
} }
LibrarySort.Type.TrackerMean -> { LibrarySort.Type.TrackerMean -> {
val item1Score = trackerScores[manga1.id] ?: defaultTrackerScoreSortValue val item1Score = trackerScores[manga1.id] ?: defaultTrackerScoreSortValue
@@ -572,7 +572,7 @@ class LibraryViewModel(
val toDelete = mangas.map { val toDelete = mangas.map {
it.removeCovers(coverCache) it.removeCovers(coverCache)
MangaUpdate(it.id) { MangaUpdate(it.id) {
favorite = false favoriteAt = null
} }
} }
updateManga.awaitAll(toDelete) updateManga.awaitAll(toDelete)
@@ -111,15 +111,13 @@ class MigrateMangaUseCase(
} }
val currentMangaUpdate = MangaUpdate(current.id) { val currentMangaUpdate = MangaUpdate(current.id) {
favorite = false favoriteAt = null
dateAdded = 0
} }
.takeIf { replace } .takeIf { replace }
val targetMangaUpdate = MangaUpdate(target.id) { val targetMangaUpdate = MangaUpdate(target.id) {
favorite = true favoriteAt = current.favoriteAt?.takeIf { replace } ?: Clock.System.now().toEpochMilliseconds()
chapterFlags = current.chapterFlags chapterFlags = current.chapterFlags
viewerFlags = current.viewerFlags viewerFlags = current.viewerFlags
dateAdded = if (replace) current.dateAdded else Clock.System.now().toEpochMilliseconds()
if (MigrationFlag.NOTES in flags) notes = current.notes if (MigrationFlag.NOTES in flags) notes = current.notes
} }
@@ -117,7 +117,7 @@ private fun ComparisonQueryNode.matches(item: LibraryItem): Boolean {
val match = when (field) { val match = when (field) {
ComparisonField.ID -> value.toLongOrNull()?.let { queryComparator.apply(manga.id, it) } ComparisonField.ID -> value.toLongOrNull()?.let { queryComparator.apply(manga.id, it) }
ComparisonField.DATE_ADDED -> compareDates(manga.dateAdded, value) ComparisonField.DATE_ADDED -> manga.favoriteAt?.let { compareDates(it, value) }
ComparisonField.FETCH_INTERVAL -> value.toIntOrNull() ComparisonField.FETCH_INTERVAL -> value.toIntOrNull()
?.let { queryComparator.apply(abs(manga.fetchInterval), it) } ?.let { queryComparator.apply(abs(manga.fetchInterval), it) }
@@ -72,11 +72,11 @@ class RestoreRepositoryImpl(
private fun mergeManga(manga: Manga, dbManga: Manga): Manga { private fun mergeManga(manga: Manga, dbManga: Manga): Manga {
val details = if (dbManga.initialized || !manga.initialized) dbManga else manga val details = if (dbManga.initialized || !manga.initialized) dbManga else manga
return dbManga.copy( return dbManga.copy(
favorite = dbManga.favorite || manga.favorite, favoriteAt = if (dbManga.favorite || manga.favorite) {
dateAdded = listOfNotNull( listOfNotNull(dbManga.favoriteAt, manga.favoriteAt).filter { it > 0 }.minOrNull() ?: 0L
dbManga.dateAdded.takeIf { it > 0 }, } else {
manga.dateAdded.takeIf { it > 0 }, null
).minOrNull() ?: 0L, },
title = details.title, title = details.title,
artist = details.artist, artist = details.artist,
author = details.author, author = details.author,
@@ -93,6 +93,7 @@ class RestoreRepositoryImpl(
private suspend fun updateManga(manga: Manga): Manga { private suspend fun updateManga(manga: Manga): Manga {
database.mangasQueries.updateFromBackup( database.mangasQueries.updateFromBackup(
favoriteAt = manga.favoriteAt,
artist = manga.artist, artist = manga.artist,
author = manga.author, author = manga.author,
description = manga.description, description = manga.description,
@@ -100,13 +101,11 @@ class RestoreRepositoryImpl(
title = manga.title, title = manga.title,
status = manga.status, status = manga.status,
thumbnailUrl = manga.thumbnailUrl, thumbnailUrl = manga.thumbnailUrl,
favorite = manga.favorite,
lastUpdate = manga.lastUpdate, lastUpdate = manga.lastUpdate,
initialized = manga.initialized, initialized = manga.initialized,
viewer = manga.viewerFlags, viewer = manga.viewerFlags,
chapterFlags = manga.chapterFlags, chapterFlags = manga.chapterFlags,
coverLastModified = manga.coverLastModified, coverLastModified = manga.coverLastModified,
dateAdded = manga.dateAdded,
mangaId = manga.id, mangaId = manga.id,
updateStrategy = manga.updateStrategy, updateStrategy = manga.updateStrategy,
notes = manga.notes, notes = manga.notes,
@@ -117,6 +116,7 @@ class RestoreRepositoryImpl(
private suspend fun insertManga(manga: Manga): Long { private suspend fun insertManga(manga: Manga): Long {
return database.mangasQueries.insertReturningId( return database.mangasQueries.insertReturningId(
favoriteAt = manga.favoriteAt,
source = manga.source, source = manga.source,
url = manga.url, url = manga.url,
artist = manga.artist, artist = manga.artist,
@@ -126,7 +126,6 @@ class RestoreRepositoryImpl(
title = manga.title, title = manga.title,
status = manga.status, status = manga.status,
thumbnailUrl = manga.thumbnailUrl, thumbnailUrl = manga.thumbnailUrl,
favorite = manga.favorite,
lastUpdate = manga.lastUpdate, lastUpdate = manga.lastUpdate,
nextUpdate = 0L, nextUpdate = 0L,
calculateInterval = 0L, calculateInterval = 0L,
@@ -134,7 +133,6 @@ class RestoreRepositoryImpl(
viewerFlags = manga.viewerFlags, viewerFlags = manga.viewerFlags,
chapterFlags = manga.chapterFlags, chapterFlags = manga.chapterFlags,
coverLastModified = manga.coverLastModified, coverLastModified = manga.coverLastModified,
dateAdded = manga.dateAdded,
updateStrategy = manga.updateStrategy, updateStrategy = manga.updateStrategy,
notes = manga.notes, notes = manga.notes,
memo = manga.memo, memo = manga.memo,
@@ -18,27 +18,24 @@ object MangaMapper {
title: String, title: String,
status: Long, status: Long,
thumbnailUrl: String?, thumbnailUrl: String?,
favorite: Boolean,
lastUpdate: Long?, lastUpdate: Long?,
nextUpdate: Long?, nextUpdate: Long?,
initialized: Boolean, initialized: Boolean,
viewerFlags: Long, viewerFlags: Long,
chapterFlags: Long, chapterFlags: Long,
coverLastModified: Long, coverLastModified: Long,
dateAdded: Long,
updateStrategy: UpdateStrategy, updateStrategy: UpdateStrategy,
calculateInterval: Long, calculateInterval: Long,
favoriteModifiedAt: Long?,
notes: String, notes: String,
memo: JsonObject, memo: JsonObject,
favoriteAt: Long?,
): Manga = Manga( ): Manga = Manga(
id = id, id = id,
source = source, source = source,
favorite = favorite,
lastUpdate = lastUpdate ?: 0, lastUpdate = lastUpdate ?: 0,
nextUpdate = nextUpdate ?: 0, nextUpdate = nextUpdate ?: 0,
fetchInterval = calculateInterval.toInt(), fetchInterval = calculateInterval.toInt(),
dateAdded = dateAdded, favoriteAt = favoriteAt,
viewerFlags = viewerFlags, viewerFlags = viewerFlags,
chapterFlags = chapterFlags, chapterFlags = chapterFlags,
coverLastModified = coverLastModified, coverLastModified = coverLastModified,
@@ -52,7 +49,6 @@ object MangaMapper {
thumbnailUrl = thumbnailUrl, thumbnailUrl = thumbnailUrl,
updateStrategy = updateStrategy, updateStrategy = updateStrategy,
initialized = initialized, initialized = initialized,
favoriteModifiedAt = favoriteModifiedAt,
notes = notes, notes = notes,
memo = memo, memo = memo,
) )
@@ -68,19 +64,17 @@ object MangaMapper {
title: String, title: String,
status: Long, status: Long,
thumbnailUrl: String?, thumbnailUrl: String?,
favorite: Boolean,
lastUpdate: Long?, lastUpdate: Long?,
nextUpdate: Long?, nextUpdate: Long?,
initialized: Boolean, initialized: Boolean,
viewerFlags: Long, viewerFlags: Long,
chapterFlags: Long, chapterFlags: Long,
coverLastModified: Long, coverLastModified: Long,
dateAdded: Long,
updateStrategy: UpdateStrategy, updateStrategy: UpdateStrategy,
calculateInterval: Long, calculateInterval: Long,
favoriteModifiedAt: Long?,
notes: String, notes: String,
memo: JsonObject, memo: JsonObject,
favoriteAt: Long?,
totalCount: Long, totalCount: Long,
readCount: Double, readCount: Double,
latestUpload: Long, latestUpload: Long,
@@ -100,19 +94,17 @@ object MangaMapper {
title, title,
status, status,
thumbnailUrl, thumbnailUrl,
favorite,
lastUpdate, lastUpdate,
nextUpdate, nextUpdate,
initialized, initialized,
viewerFlags, viewerFlags,
chapterFlags, chapterFlags,
coverLastModified, coverLastModified,
dateAdded,
updateStrategy, updateStrategy,
calculateInterval, calculateInterval,
favoriteModifiedAt,
notes, notes,
memo, memo,
favoriteAt,
), ),
categories = categories.split(",").map { it.toLong() }, categories = categories.split(",").map { it.toLong() },
totalChapters = totalCount, totalChapters = totalCount,
@@ -134,19 +126,17 @@ object MangaMapper {
title: String, title: String,
status: Long, status: Long,
thumbnailUrl: String?, thumbnailUrl: String?,
favorite: Boolean,
lastUpdate: Long?, lastUpdate: Long?,
nextUpdate: Long?, nextUpdate: Long?,
initialized: Boolean, initialized: Boolean,
viewerFlags: Long, viewerFlags: Long,
chapterFlags: Long, chapterFlags: Long,
coverLastModified: Long, coverLastModified: Long,
dateAdded: Long,
updateStrategy: UpdateStrategy, updateStrategy: UpdateStrategy,
calculateInterval: Long, calculateInterval: Long,
favoriteModifiedAt: Long?,
notes: String, notes: String,
memo: JsonObject, memo: JsonObject,
favoriteAt: Long?,
totalCount: Long, totalCount: Long,
): MangaWithChapterCount = MangaWithChapterCount( ): MangaWithChapterCount = MangaWithChapterCount(
manga = mapManga( manga = mapManga(
@@ -160,19 +150,17 @@ object MangaMapper {
title, title,
status, status,
thumbnailUrl, thumbnailUrl,
favorite,
lastUpdate, lastUpdate,
nextUpdate, nextUpdate,
initialized, initialized,
viewerFlags, viewerFlags,
chapterFlags, chapterFlags,
coverLastModified, coverLastModified,
dateAdded,
updateStrategy, updateStrategy,
calculateInterval, calculateInterval,
favoriteModifiedAt,
notes, notes,
memo, memo,
favoriteAt,
), ),
chapterCount = totalCount, chapterCount = totalCount,
) )
@@ -198,7 +198,7 @@ class MangaRepositoryImpl(
title = it.title, title = it.title,
status = it.status, status = it.status,
thumbnailUrl = it.thumbnailUrl, thumbnailUrl = it.thumbnailUrl,
favorite = it.favorite, favoriteAt = it.favoriteAt,
lastUpdate = it.lastUpdate, lastUpdate = it.lastUpdate,
nextUpdate = it.nextUpdate, nextUpdate = it.nextUpdate,
calculateInterval = it.fetchInterval.toLong(), calculateInterval = it.fetchInterval.toLong(),
@@ -206,7 +206,6 @@ class MangaRepositoryImpl(
viewerFlags = it.viewerFlags, viewerFlags = it.viewerFlags,
chapterFlags = it.chapterFlags, chapterFlags = it.chapterFlags,
coverLastModified = it.coverLastModified, coverLastModified = it.coverLastModified,
dateAdded = it.dateAdded,
updateStrategy = it.updateStrategy, updateStrategy = it.updateStrategy,
memo = it.memo, memo = it.memo,
updateTitle = it.title.isNotBlank(), updateTitle = it.title.isNotBlank(),
@@ -247,14 +246,14 @@ class MangaRepositoryImpl(
mangaUpdates.forEach { value -> mangaUpdates.forEach { value ->
with(value) { with(value) {
database.mangasQueries.update( database.mangasQueries.update(
favorite = favorite, favoriteAtSet = isSet(::favoriteAt),
favoriteAt = favoriteAt,
lastUpdate = lastUpdate, lastUpdate = lastUpdate,
nextUpdate = nextUpdate, nextUpdate = nextUpdate,
calculateInterval = fetchInterval?.toLong(), calculateInterval = fetchInterval?.toLong(),
viewer = viewerFlags, viewer = viewerFlags,
chapterFlags = chapterFlags, chapterFlags = chapterFlags,
coverLastModified = coverLastModified, coverLastModified = coverLastModified,
dateAdded = dateAdded,
mangaId = id, mangaId = id,
notes = notes, notes = notes,
) )
@@ -15,33 +15,23 @@ CREATE TABLE mangas(
title TEXT NOT NULL, title TEXT NOT NULL,
status INTEGER NOT NULL, status INTEGER NOT NULL,
thumbnail_url TEXT, thumbnail_url TEXT,
favorite INTEGER AS Boolean NOT NULL,
last_update INTEGER, last_update INTEGER,
next_update INTEGER, next_update INTEGER,
initialized INTEGER AS Boolean NOT NULL, initialized INTEGER AS Boolean NOT NULL,
viewer INTEGER NOT NULL, viewer INTEGER NOT NULL,
chapter_flags INTEGER NOT NULL, chapter_flags INTEGER NOT NULL,
cover_last_modified INTEGER NOT NULL, cover_last_modified INTEGER NOT NULL,
date_added INTEGER NOT NULL,
update_strategy INTEGER AS UpdateStrategy NOT NULL DEFAULT 0, update_strategy INTEGER AS UpdateStrategy NOT NULL DEFAULT 0,
calculate_interval INTEGER DEFAULT 0 NOT NULL, calculate_interval INTEGER DEFAULT 0 NOT NULL,
favorite_modified_at INTEGER,
notes TEXT NOT NULL DEFAULT "", notes TEXT NOT NULL DEFAULT "",
memo BLOB AS JsonObject NOT NULL DEFAULT '{}' memo BLOB AS JsonObject NOT NULL DEFAULT '{}',
favorite_at INTEGER
); );
CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1; CREATE INDEX library_favorite_at_index ON mangas(favorite_at) WHERE favorite_at IS NOT NULL;
CREATE INDEX mangas_url_index ON mangas(url); CREATE INDEX mangas_url_index ON mangas(url);
CREATE INDEX idx_mangas_source ON mangas(source); CREATE INDEX idx_mangas_source ON mangas(source);
CREATE TRIGGER update_last_favorited_at_mangas
AFTER UPDATE OF favorite ON mangas
BEGIN
UPDATE mangas
SET favorite_modified_at = strftime('%s', 'now')
WHERE _id = new._id;
END;
getMangaById: getMangaById:
SELECT * SELECT *
FROM mangas FROM mangas
@@ -58,12 +48,12 @@ LIMIT 1;
getFavorites: getFavorites:
SELECT * SELECT *
FROM mangas FROM mangas
WHERE favorite = 1; WHERE favorite_at IS NOT NULL;
getReadMangaNotInLibrary: getReadMangaNotInLibrary:
SELECT * SELECT *
FROM mangas FROM mangas
WHERE favorite = 0 AND _id IN ( WHERE favorite_at IS NULL AND _id IN (
SELECT DISTINCT chapters.manga_id SELECT DISTINCT chapters.manga_id
FROM chapters FROM chapters
WHERE read = 1 OR last_page_read != 0 WHERE read = 1 OR last_page_read != 0
@@ -77,23 +67,18 @@ getAllMangaSourceAndUrl:
SELECT source, url SELECT source, url
FROM mangas; FROM mangas;
getMangasWithFavoriteTimestamp:
SELECT *
FROM mangas
WHERE favorite_modified_at IS NOT NULL;
getSourceIdWithFavoriteCount: getSourceIdWithFavoriteCount:
SELECT SELECT
source, source,
count(*) count(*)
FROM mangas FROM mangas
WHERE favorite = 1 WHERE favorite_at IS NOT NULL
GROUP BY source; GROUP BY source;
getFavoriteBySourceId: getFavoriteBySourceId:
SELECT * SELECT *
FROM mangas FROM mangas
WHERE favorite = 1 WHERE favorite_at IS NOT NULL
AND source = :sourceId; AND source = :sourceId;
getDuplicateLibraryManga: getDuplicateLibraryManga:
@@ -113,7 +98,7 @@ duplicates AS (
FROM mangas M FROM mangas M
LEFT JOIN track_dupes D LEFT JOIN track_dupes D
ON D.manga_id = _id ON D.manga_id = _id
WHERE favorite = 1 WHERE favorite_at IS NOT NULL
AND _id != :id AND _id != :id
AND (lower(title) LIKE '%' || lower(:title) || '%' OR D.manga_id IS NOT NULL) AND (lower(title) LIKE '%' || lower(:title) || '%' OR D.manga_id IS NOT NULL)
), ),
@@ -141,7 +126,7 @@ getUpcomingManga:
SELECT * SELECT *
FROM mangas FROM mangas
WHERE next_update >= :startOfDay WHERE next_update >= :startOfDay
AND favorite = 1 AND favorite_at IS NOT NULL
AND status IN :statuses AND status IN :statuses
AND ( AND (
-- includedEmpty being true expresses "don't care" state and bypasses the membership filter -- includedEmpty being true expresses "don't care" state and bypasses the membership filter
@@ -179,12 +164,12 @@ SET viewer = 0;
getSourceIdsWithNonLibraryManga: getSourceIdsWithNonLibraryManga:
SELECT source, COUNT(*) AS manga_count SELECT source, COUNT(*) AS manga_count
FROM mangas FROM mangas
WHERE favorite = 0 WHERE favorite_at IS NULL
GROUP BY source; GROUP BY source;
deleteNonLibraryManga: deleteNonLibraryManga:
DELETE FROM mangas DELETE FROM mangas
WHERE favorite = 0 WHERE favorite_at IS NULL
AND source IN :sourceIds AND source IN :sourceIds
AND ( AND (
:keepReadManga = 0 :keepReadManga = 0
@@ -197,19 +182,19 @@ AND (
); );
insertReturningId: insertReturningId:
INSERT INTO mangas(source, url, artist, author, description, genre, title, status, thumbnail_url, favorite, last_update, next_update, initialized, viewer, chapter_flags, cover_last_modified, date_added, update_strategy, calculate_interval, notes, memo) INSERT INTO mangas(source, url, artist, author, description, genre, title, status, thumbnail_url, favorite_at, last_update, next_update, initialized, viewer, chapter_flags, cover_last_modified, update_strategy, calculate_interval, notes, memo)
VALUES (:source, :url, :artist, :author, :description, :genre, :title, :status, :thumbnailUrl, :favorite, :lastUpdate, :nextUpdate, :initialized, :viewerFlags, :chapterFlags, :coverLastModified, :dateAdded, :updateStrategy, :calculateInterval, :notes, :memo) VALUES (:source, :url, :artist, :author, :description, :genre, :title, :status, :thumbnailUrl, :favoriteAt, :lastUpdate, :nextUpdate, :initialized, :viewerFlags, :chapterFlags, :coverLastModified, :updateStrategy, :calculateInterval, :notes, :memo)
RETURNING _id; RETURNING _id;
update: update:
UPDATE mangas SET UPDATE mangas SET
favorite = coalesce(:favorite, favorite), -- NULL is a real value here (not in the library), which coalesce cannot write
favorite_at = CASE WHEN :favoriteAtSet THEN :favoriteAt ELSE favorite_at END,
last_update = coalesce(:lastUpdate, last_update), last_update = coalesce(:lastUpdate, last_update),
next_update = coalesce(:nextUpdate, next_update), next_update = coalesce(:nextUpdate, next_update),
viewer = coalesce(:viewer, viewer), viewer = coalesce(:viewer, viewer),
chapter_flags = coalesce(:chapterFlags, chapter_flags), chapter_flags = coalesce(:chapterFlags, chapter_flags),
cover_last_modified = coalesce(:coverLastModified, cover_last_modified), cover_last_modified = coalesce(:coverLastModified, cover_last_modified),
date_added = coalesce(:dateAdded, date_added),
calculate_interval = coalesce(:calculateInterval, calculate_interval), calculate_interval = coalesce(:calculateInterval, calculate_interval),
notes = coalesce(:notes, notes) notes = coalesce(:notes, notes)
WHERE _id = :mangaId; WHERE _id = :mangaId;
@@ -223,13 +208,12 @@ UPDATE mangas SET
title = coalesce(:title, title), title = coalesce(:title, title),
status = coalesce(:status, status), status = coalesce(:status, status),
thumbnail_url = coalesce(:thumbnailUrl, thumbnail_url), thumbnail_url = coalesce(:thumbnailUrl, thumbnail_url),
favorite = coalesce(:favorite, favorite), favorite_at = :favoriteAt,
last_update = coalesce(:lastUpdate, last_update), last_update = coalesce(:lastUpdate, last_update),
initialized = coalesce(:initialized, initialized), initialized = coalesce(:initialized, initialized),
viewer = coalesce(:viewer, viewer), viewer = coalesce(:viewer, viewer),
chapter_flags = coalesce(:chapterFlags, chapter_flags), chapter_flags = coalesce(:chapterFlags, chapter_flags),
cover_last_modified = coalesce(:coverLastModified, cover_last_modified), cover_last_modified = coalesce(:coverLastModified, cover_last_modified),
date_added = coalesce(:dateAdded, date_added),
update_strategy = coalesce(:updateStrategy, update_strategy), update_strategy = coalesce(:updateStrategy, update_strategy),
notes = coalesce(:notes, notes), notes = coalesce(:notes, notes),
memo = coalesce(:memo, memo) memo = coalesce(:memo, memo)
@@ -253,13 +237,13 @@ WHERE _id = :mangaId;
insertNetworkManga { insertNetworkManga {
-- Insert the manga if it doesn't exist already -- Insert the manga if it doesn't exist already
INSERT INTO mangas( INSERT INTO mangas(
source, url, artist, author, description, genre, title, status, thumbnail_url, favorite, source, url, artist, author, description, genre, title, status, thumbnail_url, favorite_at,
last_update, next_update, initialized, viewer, chapter_flags, cover_last_modified, date_added, last_update, next_update, initialized, viewer, chapter_flags, cover_last_modified,
update_strategy, calculate_interval, memo update_strategy, calculate_interval, memo
) )
SELECT SELECT
:source, :url, :artist, :author, :description, :genre, :title, :status, :thumbnailUrl, :favorite, :source, :url, :artist, :author, :description, :genre, :title, :status, :thumbnailUrl, :favoriteAt,
:lastUpdate, :nextUpdate, :initialized, :viewerFlags, :chapterFlags, :coverLastModified, :dateAdded, :lastUpdate, :nextUpdate, :initialized, :viewerFlags, :chapterFlags, :coverLastModified,
:updateStrategy, :calculateInterval, :memo :updateStrategy, :calculateInterval, :memo
WHERE NOT EXISTS(SELECT 0 FROM mangas WHERE source = :source AND url = :url); WHERE NOT EXISTS(SELECT 0 FROM mangas WHERE source = :source AND url = :url);
@@ -278,7 +262,7 @@ insertNetworkManga {
memo = :memo memo = :memo
WHERE source = :source WHERE source = :source
AND url = :url AND url = :url
AND favorite = 0; AND favorite_at IS NULL;
-- Finally return the manga -- Finally return the manga
SELECT * SELECT *
@@ -0,0 +1,113 @@
-- Merge favorite, date_added and favorite_modified_at into favorite_at
DROP VIEW historyView;
DROP VIEW libraryView;
DROP VIEW updatesView;
DROP TRIGGER update_last_favorited_at_mangas;
DROP INDEX library_favorite_index;
ALTER TABLE mangas ADD COLUMN favorite_at INTEGER;
UPDATE mangas
SET favorite_at = coalesce(nullif(date_added, 0), favorite_modified_at * 1000, 0)
WHERE favorite = 1;
ALTER TABLE mangas DROP COLUMN favorite;
ALTER TABLE mangas DROP COLUMN date_added;
ALTER TABLE mangas DROP COLUMN favorite_modified_at;
CREATE INDEX library_favorite_at_index ON mangas(favorite_at) WHERE favorite_at IS NOT NULL;
CREATE VIEW libraryView AS
SELECT
M.*,
coalesce(C.total, 0) AS totalCount,
coalesce(C.readCount, 0) AS readCount,
coalesce(C.latestUpload, 0) AS latestUpload,
coalesce(C.fetchedAt, 0) AS chapterFetchedAt,
coalesce(C.lastRead, 0) AS lastRead,
coalesce(C.bookmarkCount, 0) AS bookmarkCount,
coalesce(MC.categories, '0') AS categories
FROM mangas M
LEFT JOIN (
SELECT
chapters.manga_id,
count(*) AS total,
sum(read) AS readCount,
coalesce(max(chapters.date_upload), 0) AS latestUpload,
coalesce(max(history.last_read), 0) AS lastRead,
coalesce(max(chapters.date_fetch), 0) AS fetchedAt,
sum(chapters.bookmark) AS bookmarkCount
FROM chapters
LEFT JOIN excluded_scanlators
ON chapters.manga_id = excluded_scanlators.manga_id
AND chapters.scanlator = excluded_scanlators.scanlator
LEFT JOIN history
ON chapters._id = history.chapter_id
WHERE excluded_scanlators.scanlator IS NULL
GROUP BY chapters.manga_id
) AS C
ON M._id = C.manga_id
LEFT JOIN (
SELECT manga_id, group_concat(category_id) AS categories
FROM mangas_categories
GROUP BY manga_id
) AS MC
ON MC.manga_id = M._id
WHERE M.favorite_at IS NOT NULL;
CREATE VIEW updatesView AS
SELECT
mangas._id AS mangaId,
mangas.title AS mangaTitle,
chapters._id AS chapterId,
chapters.name AS chapterName,
chapters.scanlator,
chapters.url AS chapterUrl,
chapters.read,
chapters.bookmark,
chapters.last_page_read,
mangas.source,
mangas.favorite_at IS NOT NULL AS favorite,
mangas.thumbnail_url AS thumbnailUrl,
mangas.cover_last_modified AS coverLastModified,
chapters.date_upload AS dateUpload,
chapters.date_fetch AS datefetch,
excluded_scanlators.scanlator AS excludedScanlator
FROM mangas JOIN chapters
ON mangas._id = chapters.manga_id
LEFT JOIN excluded_scanlators
ON mangas._id = excluded_scanlators.manga_id
AND chapters.scanlator = excluded_scanlators.scanlator
WHERE favorite_at IS NOT NULL
AND date_fetch > favorite_at
ORDER BY date_fetch DESC;
CREATE VIEW historyView AS
SELECT
history._id AS id,
mangas._id AS mangaId,
chapters._id AS chapterId,
mangas.title,
mangas.thumbnail_url AS thumbnailUrl,
mangas.source,
mangas.favorite_at IS NOT NULL AS favorite,
mangas.cover_last_modified,
chapters.chapter_number AS chapterNumber,
history.last_read AS readAt,
history.time_read AS readDuration,
max_last_read.last_read AS maxReadAt,
max_last_read.chapter_id AS maxReadAtChapterId
FROM mangas
JOIN chapters
ON mangas._id = chapters.manga_id
JOIN history
ON chapters._id = history.chapter_id
JOIN (
SELECT chapters.manga_id,chapters._id AS chapter_id, MAX(history.last_read) AS last_read
FROM chapters JOIN history
ON chapters._id = history.chapter_id
GROUP BY chapters.manga_id
) AS max_last_read
ON chapters.manga_id = max_last_read.manga_id;
@@ -6,7 +6,7 @@ SELECT
mangas.title, mangas.title,
mangas.thumbnail_url AS thumbnailUrl, mangas.thumbnail_url AS thumbnailUrl,
mangas.source, mangas.source,
mangas.favorite, mangas.favorite_at IS NOT NULL AS favorite,
mangas.cover_last_modified, mangas.cover_last_modified,
chapters.chapter_number AS chapterNumber, chapters.chapter_number AS chapterNumber,
history.last_read AS readAt, history.last_read AS readAt,
@@ -34,7 +34,7 @@ LEFT JOIN (
GROUP BY manga_id GROUP BY manga_id
) AS MC ) AS MC
ON MC.manga_id = M._id ON MC.manga_id = M._id
WHERE M.favorite = 1; WHERE M.favorite_at IS NOT NULL;
library: library:
SELECT * SELECT *
@@ -10,7 +10,7 @@ SELECT
chapters.bookmark, chapters.bookmark,
chapters.last_page_read, chapters.last_page_read,
mangas.source, mangas.source,
mangas.favorite, mangas.favorite_at IS NOT NULL AS favorite,
mangas.thumbnail_url AS thumbnailUrl, mangas.thumbnail_url AS thumbnailUrl,
mangas.cover_last_modified AS coverLastModified, mangas.cover_last_modified AS coverLastModified,
chapters.date_upload AS dateUpload, chapters.date_upload AS dateUpload,
@@ -21,8 +21,8 @@ ON mangas._id = chapters.manga_id
LEFT JOIN excluded_scanlators LEFT JOIN excluded_scanlators
ON mangas._id = excluded_scanlators.manga_id ON mangas._id = excluded_scanlators.manga_id
AND chapters.scanlator = excluded_scanlators.scanlator AND chapters.scanlator = excluded_scanlators.scanlator
WHERE favorite = 1 WHERE favorite_at IS NOT NULL
AND date_fetch > date_added AND date_fetch > favorite_at
ORDER BY date_fetch DESC; ORDER BY date_fetch DESC;
getRecentUpdates: getRecentUpdates:
@@ -19,11 +19,10 @@ import java.io.Serializable as JavaSerializable
data class Manga( data class Manga(
val id: Long, val id: Long,
val source: Long, val source: Long,
val favorite: Boolean,
val lastUpdate: Long, val lastUpdate: Long,
val nextUpdate: Long, val nextUpdate: Long,
val fetchInterval: Int, val fetchInterval: Int,
val dateAdded: Long, val favoriteAt: Long?,
val viewerFlags: Long, val viewerFlags: Long,
val chapterFlags: Long, val chapterFlags: Long,
val coverLastModified: Long, val coverLastModified: Long,
@@ -37,11 +36,13 @@ data class Manga(
val thumbnailUrl: String?, val thumbnailUrl: String?,
val updateStrategy: UpdateStrategy, val updateStrategy: UpdateStrategy,
val initialized: Boolean, val initialized: Boolean,
val favoriteModifiedAt: Long?,
val notes: String, val notes: String,
val memo: JsonObject, val memo: JsonObject,
) : JavaSerializable { ) : JavaSerializable {
val favorite: Boolean
get() = favoriteAt != null
val expectedNextUpdate: Instant? val expectedNextUpdate: Instant?
get() = nextUpdate get() = nextUpdate
.takeIf { status != SManga.COMPLETED.toLong() } .takeIf { status != SManga.COMPLETED.toLong() }
@@ -115,11 +116,10 @@ data class Manga(
url = "", url = "",
title = "", title = "",
source = -1L, source = -1L,
favorite = false,
lastUpdate = 0L, lastUpdate = 0L,
nextUpdate = 0L, nextUpdate = 0L,
fetchInterval = 0, fetchInterval = 0,
dateAdded = 0L, favoriteAt = null,
viewerFlags = 0L, viewerFlags = 0L,
chapterFlags = 0L, chapterFlags = 0L,
coverLastModified = 0L, coverLastModified = 0L,
@@ -131,7 +131,6 @@ data class Manga(
thumbnailUrl = null, thumbnailUrl = null,
updateStrategy = UpdateStrategy.ALWAYS_UPDATE, updateStrategy = UpdateStrategy.ALWAYS_UPDATE,
initialized = false, initialized = false,
favoriteModifiedAt = null,
notes = "", notes = "",
memo = JsonObject.EMPTY, memo = JsonObject.EMPTY,
) )
@@ -3,11 +3,10 @@ package tachiyomi.domain.manga.model
import mihon.domain.common.PartialUpdate import mihon.domain.common.PartialUpdate
class MangaUpdate(val id: Long, block: MangaUpdate.() -> Unit) : PartialUpdate() { class MangaUpdate(val id: Long, block: MangaUpdate.() -> Unit) : PartialUpdate() {
var favorite: Boolean? by field(null) var favoriteAt: Long? by field(null)
var lastUpdate: Long? by field(null) var lastUpdate: Long? by field(null)
var nextUpdate: Long? by field(null) var nextUpdate: Long? by field(null)
var fetchInterval: Int? by field(null) var fetchInterval: Int? by field(null)
var dateAdded: Long? by field(null)
var viewerFlags: Long? by field(null) var viewerFlags: Long? by field(null)
var chapterFlags: Long? by field(null) var chapterFlags: Long? by field(null)
var coverLastModified: Long? by field(null) var coverLastModified: Long? by field(null)