Tweak database table and view schema

This commit is contained in:
AntsyLich 2024-09-05 18:21:31 +06:00
parent 8fd1239bea
commit afcb6bd9fc
No known key found for this signature in database
13 changed files with 346 additions and 89 deletions

View File

@ -330,13 +330,13 @@ class MangaRestorer(
null null
} else { } else {
// New history entry // New history entry
item.copy(chapterId = chapter._id) item.copy(chapterId = chapter.id)
} }
} }
// Update history entry // Update history entry
item.copy( item.copy(
id = dbHistory._id, id = dbHistory.id,
chapterId = dbHistory.chapter_id, chapterId = dbHistory.chapter_id,
readAt = max(item.readAt?.time ?: 0L, dbHistory.last_read?.time ?: 0L) readAt = max(item.readAt?.time ?: 0L, dbHistory.last_read?.time ?: 0L)
.takeIf { it > 0L } .takeIf { it > 0L }

View File

@ -1,17 +1,18 @@
CREATE TABLE categories( CREATE TABLE categories(
_id INTEGER NOT NULL PRIMARY KEY, id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL, name TEXT NOT NULL,
sort INTEGER NOT NULL, `order` INTEGER NOT NULL,
flags INTEGER NOT NULL flags INTEGER NOT NULL,
UNIQUE(`order`) ON CONFLICT ABORT
); );
-- Insert system category -- Insert system category
INSERT OR IGNORE INTO categories(_id, name, sort, flags) VALUES (0, "", -1, 0); INSERT OR IGNORE INTO categories(id, name, `order`, flags) VALUES (0, "", -1, 0);
-- Disallow deletion of default category -- Disallow deletion of default category
CREATE TRIGGER IF NOT EXISTS system_category_delete_trigger BEFORE DELETE CREATE TRIGGER IF NOT EXISTS system_category_delete_trigger BEFORE DELETE
ON categories ON categories
BEGIN SELECT CASE BEGIN SELECT CASE
WHEN old._id <= 0 THEN WHEN old.id <= 0 THEN
RAISE(ABORT, "System category can't be deleted") RAISE(ABORT, "System category can't be deleted")
END; END;
END; END;
@ -19,47 +20,46 @@ END;
getCategory: getCategory:
SELECT * SELECT *
FROM categories FROM categories
WHERE _id = :id WHERE id = :id;
LIMIT 1;
getCategories: getCategories:
SELECT SELECT
_id AS id, id,
name, name,
sort AS `order`, `order`,
flags flags
FROM categories FROM categories
ORDER BY sort; ORDER BY `order`;
getCategoriesByMangaId: getCategoriesByMangaId:
SELECT SELECT
C._id AS id, C.id,
C.name, C.name,
C.sort AS `order`, C.`order`,
C.flags C.flags
FROM categories C FROM categories C
JOIN mangas_categories MC JOIN mangas_categories MC
ON C._id = MC.category_id ON C.id = MC.category_id
WHERE MC.manga_id = :mangaId; WHERE MC.manga_id = :mangaId;
insert: insert:
INSERT INTO categories(name, sort, flags) INSERT INTO categories(name, `order`, flags)
VALUES (:name, :order, :flags); VALUES (:name, :order, :flags);
delete: delete:
DELETE FROM categories DELETE FROM categories
WHERE _id = :categoryId; WHERE id = :categoryId;
update: update:
UPDATE categories UPDATE categories
SET name = coalesce(:name, name), SET name = coalesce(:name, name),
sort = coalesce(:order, sort), `order` = coalesce(:order, `order`),
flags = coalesce(:flags, flags) flags = coalesce(:flags, flags)
WHERE _id = :categoryId; WHERE id = :categoryId;
updateAllFlags: updateAllFlags:
UPDATE categories SET UPDATE categories SET
flags = coalesce(?, flags); flags = coalesce(?, flags);
selectLastInsertedRowId: selectLastInsertedRowId:
SELECT last_insert_rowid(); SELECT last_insert_rowid();

View File

@ -1,7 +1,7 @@
import kotlin.Boolean; import kotlin.Boolean;
CREATE TABLE chapters( CREATE TABLE chapters(
_id INTEGER NOT NULL PRIMARY KEY, id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL, manga_id INTEGER NOT NULL,
url TEXT NOT NULL, url TEXT NOT NULL,
name TEXT NOT NULL, name TEXT NOT NULL,
@ -16,20 +16,20 @@ CREATE TABLE chapters(
last_modified_at INTEGER NOT NULL DEFAULT 0, last_modified_at INTEGER NOT NULL DEFAULT 0,
version INTEGER NOT NULL DEFAULT 0, version INTEGER NOT NULL DEFAULT 0,
is_syncing INTEGER NOT NULL DEFAULT 0, is_syncing INTEGER NOT NULL DEFAULT 0,
FOREIGN KEY(manga_id) REFERENCES mangas (_id) UNIQUE(manga_id, url) ON CONFLICT IGNORE,
FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE ON DELETE CASCADE
); );
CREATE INDEX chapters_manga_id_index ON chapters(manga_id); CREATE INDEX chapters_manga_id_index ON chapters(manga_id);
CREATE INDEX chapters_unread_by_manga_index ON chapters(manga_id, read) WHERE read = 0;
CREATE TRIGGER update_last_modified_at_chapters CREATE TRIGGER update_last_modified_at_chapters
AFTER UPDATE ON chapters AFTER UPDATE ON chapters
FOR EACH ROW FOR EACH ROW
BEGIN BEGIN
UPDATE chapters UPDATE chapters
SET last_modified_at = strftime('%s', 'now') SET last_modified_at = strftime('%s', 'now')
WHERE _id = new._id; WHERE id = new.id;
END; END;
CREATE TRIGGER update_chapter_and_manga_version AFTER UPDATE ON chapters CREATE TRIGGER update_chapter_and_manga_version AFTER UPDATE ON chapters
@ -41,17 +41,17 @@ WHEN new.is_syncing = 0 AND (
BEGIN BEGIN
-- Update the chapter version -- Update the chapter version
UPDATE chapters SET version = version + 1 UPDATE chapters SET version = version + 1
WHERE _id = new._id; WHERE id = new.id;
-- Update the manga version -- Update the manga version
UPDATE mangas SET version = version + 1 UPDATE mangas SET version = version + 1
WHERE _id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE _id = new.manga_id) = 0; WHERE id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE id = new.manga_id) = 0;
END; END;
getChapterById: getChapterById:
SELECT * SELECT *
FROM chapters FROM chapters
WHERE _id = :id; WHERE id = :id;
getChaptersByMangaId: getChaptersByMangaId:
SELECT C.* SELECT C.*
@ -89,7 +89,7 @@ AND manga_id = :mangaId;
removeChaptersWithIds: removeChaptersWithIds:
DELETE FROM chapters DELETE FROM chapters
WHERE _id IN :chapterIds; WHERE id IN :chapterIds;
resetIsSyncing: resetIsSyncing:
UPDATE chapters UPDATE chapters
@ -115,7 +115,7 @@ SET manga_id = coalesce(:mangaId, manga_id),
date_upload = coalesce(:dateUpload, date_upload), date_upload = coalesce(:dateUpload, date_upload),
version = coalesce(:version, version), version = coalesce(:version, version),
is_syncing = coalesce(:isSyncing, is_syncing) is_syncing = coalesce(:isSyncing, is_syncing)
WHERE _id = :chapterId; WHERE id = :chapterId;
selectLastInsertedRowId: selectLastInsertedRowId:
SELECT last_insert_rowid(); SELECT last_insert_rowid();

View File

@ -1,7 +1,8 @@
CREATE TABLE excluded_scanlators( CREATE TABLE excluded_scanlators(
manga_id INTEGER NOT NULL, manga_id INTEGER NOT NULL,
scanlator TEXT NOT NULL, scanlator TEXT NOT NULL,
FOREIGN KEY(manga_id) REFERENCES mangas (_id) UNIQUE(manga_id, scanlator) ON CONFLICT IGNORE,
FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE ON DELETE CASCADE
); );

View File

@ -1,54 +1,55 @@
import java.util.Date; import java.util.Date;
CREATE TABLE history( CREATE TABLE history(
_id INTEGER NOT NULL PRIMARY KEY, id INTEGER NOT NULL PRIMARY KEY,
chapter_id INTEGER NOT NULL UNIQUE, chapter_id INTEGER NOT NULL UNIQUE,
last_read INTEGER AS Date, last_read INTEGER AS Date,
time_read INTEGER NOT NULL, time_read INTEGER NOT NULL,
FOREIGN KEY(chapter_id) REFERENCES chapters (_id) UNIQUE(chapter_id) ON CONFLICT IGNORE,
FOREIGN KEY(chapter_id) REFERENCES chapters (id)
ON DELETE CASCADE ON DELETE CASCADE
); );
CREATE INDEX history_history_chapter_id_index ON history(chapter_id); CREATE INDEX history_chapter_id_index ON history(chapter_id);
getHistoryByMangaId: getHistoryByMangaId:
SELECT SELECT
H._id, H.id,
H.chapter_id, H.chapter_id,
H.last_read, H.last_read,
H.time_read H.time_read
FROM history H FROM history H
JOIN chapters C JOIN chapters C
ON H.chapter_id = C._id ON H.chapter_id = C.id
WHERE C.manga_id = :mangaId AND C._id = H.chapter_id; WHERE C.manga_id = :mangaId AND C.id = H.chapter_id;
getHistoryByChapterUrl: getHistoryByChapterUrl:
SELECT SELECT
H._id, H.id,
H.chapter_id, H.chapter_id,
H.last_read, H.last_read,
H.time_read H.time_read
FROM history H FROM history H
JOIN chapters C JOIN chapters C
ON H.chapter_id = C._id ON H.chapter_id = C.id
WHERE C.url = :chapterUrl AND C._id = H.chapter_id; WHERE C.url = :chapterUrl AND C.id = H.chapter_id;
resetHistoryById: resetHistoryById:
UPDATE history UPDATE history
SET last_read = 0 SET last_read = 0
WHERE _id = :historyId; WHERE id = :historyId;
resetHistoryByMangaId: resetHistoryByMangaId:
UPDATE history UPDATE history
SET last_read = 0 SET last_read = 0
WHERE _id IN ( WHERE id IN (
SELECT H._id SELECT H.id
FROM mangas M FROM mangas M
INNER JOIN chapters C INNER JOIN chapters C
ON M._id = C.manga_id ON M.id = C.manga_id
INNER JOIN history H INNER JOIN history H
ON C._id = H.chapter_id ON C.id = H.chapter_id
WHERE M._id = :mangaId WHERE M.id = :mangaId
); );
removeAllHistory: removeAllHistory:

View File

@ -1,5 +1,5 @@
CREATE TABLE manga_sync( CREATE TABLE manga_sync(
_id INTEGER NOT NULL PRIMARY KEY, id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL, manga_id INTEGER NOT NULL,
sync_id INTEGER NOT NULL, sync_id INTEGER NOT NULL,
remote_id INTEGER NOT NULL, remote_id INTEGER NOT NULL,
@ -12,11 +12,13 @@ CREATE TABLE manga_sync(
remote_url TEXT NOT NULL, remote_url TEXT NOT NULL,
start_date INTEGER NOT NULL, start_date INTEGER NOT NULL,
finish_date INTEGER NOT NULL, finish_date INTEGER NOT NULL,
UNIQUE (manga_id, sync_id) ON CONFLICT REPLACE, UNIQUE(manga_id, sync_id) ON CONFLICT IGNORE,
FOREIGN KEY(manga_id) REFERENCES mangas (_id) FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE ON DELETE CASCADE
); );
CREATE INDEX manga_sync_manga_id_index ON manga_sync(manga_id);
delete: delete:
DELETE FROM manga_sync DELETE FROM manga_sync
WHERE manga_id = :mangaId AND sync_id = :syncId; WHERE manga_id = :mangaId AND sync_id = :syncId;
@ -28,7 +30,7 @@ FROM manga_sync;
getTrackById: getTrackById:
SELECT * SELECT *
FROM manga_sync FROM manga_sync
WHERE _id = :id; WHERE id = :id;
getTracksByMangaId: getTracksByMangaId:
SELECT * SELECT *
@ -54,4 +56,4 @@ SET
remote_url = coalesce(:trackingUrl, remote_url), remote_url = coalesce(:trackingUrl, remote_url),
start_date = coalesce(:startDate, start_date), start_date = coalesce(:startDate, start_date),
finish_date = coalesce(:finishDate, finish_date) finish_date = coalesce(:finishDate, finish_date)
WHERE _id = :id; WHERE id = :id;

View File

@ -4,7 +4,7 @@ import kotlin.Boolean;
import kotlin.String; import kotlin.String;
CREATE TABLE mangas( CREATE TABLE mangas(
_id INTEGER NOT NULL PRIMARY KEY, id INTEGER NOT NULL PRIMARY KEY,
source INTEGER NOT NULL, source INTEGER NOT NULL,
url TEXT NOT NULL, url TEXT NOT NULL,
artist TEXT, artist TEXT,
@ -27,7 +27,8 @@ CREATE TABLE mangas(
last_modified_at INTEGER NOT NULL DEFAULT 0, last_modified_at INTEGER NOT NULL DEFAULT 0,
favorite_modified_at INTEGER, favorite_modified_at INTEGER,
version INTEGER NOT NULL DEFAULT 0, version INTEGER NOT NULL DEFAULT 0,
is_syncing INTEGER NOT NULL DEFAULT 0 is_syncing INTEGER NOT NULL DEFAULT 0,
UNIQUE(source, url) ON CONFLICT IGNORE
); );
CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1; CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1;
@ -38,7 +39,7 @@ AFTER UPDATE OF favorite ON mangas
BEGIN BEGIN
UPDATE mangas UPDATE mangas
SET favorite_modified_at = strftime('%s', 'now') SET favorite_modified_at = strftime('%s', 'now')
WHERE _id = new._id; WHERE id = new.id;
END; END;
CREATE TRIGGER update_last_modified_at_mangas CREATE TRIGGER update_last_modified_at_mangas
@ -47,13 +48,13 @@ FOR EACH ROW
BEGIN BEGIN
UPDATE mangas UPDATE mangas
SET last_modified_at = strftime('%s', 'now') SET last_modified_at = strftime('%s', 'now')
WHERE _id = new._id; WHERE id = new.id;
END; END;
CREATE TRIGGER update_manga_version AFTER UPDATE ON mangas CREATE TRIGGER update_manga_version AFTER UPDATE ON mangas
BEGIN BEGIN
UPDATE mangas SET version = version + 1 UPDATE mangas SET version = version + 1
WHERE _id = new._id AND new.is_syncing = 0 AND ( WHERE id = new.id AND new.is_syncing = 0 AND (
new.url != old.url OR new.url != old.url OR
new.description != old.description OR new.description != old.description OR
new.favorite != old.favorite new.favorite != old.favorite
@ -63,15 +64,13 @@ END;
getMangaById: getMangaById:
SELECT * SELECT *
FROM mangas FROM mangas
WHERE _id = :id; WHERE id = :id;
-- TODO: this should ideally never really have more than 1 result
getMangaByUrlAndSource: getMangaByUrlAndSource:
SELECT * SELECT *
FROM mangas FROM mangas
WHERE url = :url WHERE url = :url
AND source = :source AND source = :source;
LIMIT 1;
getFavorites: getFavorites:
SELECT * SELECT *
@ -110,7 +109,7 @@ SELECT *
FROM mangas FROM mangas
WHERE favorite = 1 WHERE favorite = 1
AND LOWER(title) = :title AND LOWER(title) = :title
AND _id != :id; AND id != :id;
getUpcomingManga: getUpcomingManga:
SELECT * SELECT *
@ -167,7 +166,7 @@ UPDATE mangas SET
calculate_interval = coalesce(:calculateInterval, calculate_interval), calculate_interval = coalesce(:calculateInterval, calculate_interval),
version = coalesce(:version, version), version = coalesce(:version, version),
is_syncing = coalesce(:isSyncing, is_syncing) is_syncing = coalesce(:isSyncing, is_syncing)
WHERE _id = :mangaId; WHERE id = :mangaId;
selectLastInsertedRowId: selectLastInsertedRowId:
SELECT last_insert_rowid(); SELECT last_insert_rowid();

View File

@ -1,18 +1,21 @@
CREATE TABLE mangas_categories( CREATE TABLE mangas_categories(
_id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL, manga_id INTEGER NOT NULL,
category_id INTEGER NOT NULL, category_id INTEGER NOT NULL,
FOREIGN KEY(category_id) REFERENCES categories (_id) UNIQUE(manga_id, category_id) ON CONFLICT IGNORE,
FOREIGN KEY(category_id) REFERENCES categories(id)
ON DELETE CASCADE, ON DELETE CASCADE,
FOREIGN KEY(manga_id) REFERENCES mangas (_id) FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE ON DELETE CASCADE
); );
CREATE INDEX mangas_categories_manga_id_index ON mangas_categories(manga_id);
CREATE INDEX mangas_categories_category_id_index ON mangas_categories(category_id);
CREATE TRIGGER insert_manga_category_update_version AFTER INSERT ON mangas_categories CREATE TRIGGER insert_manga_category_update_version AFTER INSERT ON mangas_categories
BEGIN BEGIN
UPDATE mangas UPDATE mangas
SET version = version + 1 SET version = version + 1
WHERE _id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE _id = new.manga_id) = 0; WHERE id = new.manga_id AND (SELECT is_syncing FROM mangas WHERE id = new.manga_id) = 0;
END; END;
insert: insert:
@ -21,4 +24,4 @@ VALUES (:mangaId, :categoryId);
deleteMangaCategoryByMangaId: deleteMangaCategoryByMangaId:
DELETE FROM mangas_categories DELETE FROM mangas_categories
WHERE manga_id = :mangaId; WHERE manga_id = :mangaId;

View File

@ -1,5 +1,5 @@
CREATE TABLE sources( CREATE TABLE sources(
_id INTEGER NOT NULL PRIMARY KEY, id INTEGER NOT NULL PRIMARY KEY,
lang TEXT NOT NULL, lang TEXT NOT NULL,
name TEXT NOT NULL name TEXT NOT NULL
); );
@ -11,14 +11,14 @@ FROM sources;
findOne: findOne:
SELECT * SELECT *
FROM sources FROM sources
WHERE _id = :id; WHERE id = :id;
upsert: upsert:
INSERT INTO sources(_id, lang, name) INSERT INTO sources(id, lang, name)
VALUES (:id, :lang, :name) VALUES (:id, :lang, :name)
ON CONFLICT(_id) ON CONFLICT(id)
DO UPDATE DO UPDATE
SET SET
lang = :lang, lang = :lang,
name = :name name = :name
WHERE _id = :id; WHERE id = :id;

View File

@ -0,0 +1,252 @@
import eu.kanade.tachiyomi.source.model.UpdateStrategy;
import java.util.Date;
import kotlin.Boolean;
import kotlin.String;
import kotlin.collections.List;
DROP INDEX IF EXISTS chapters_manga_id_index;
DROP INDEX IF EXISTS chapters_unread_by_manga_index;
DROP INDEX IF EXISTS history_history_chapter_id_index;
DROP INDEX IF EXISTS library_favorite_index;
DROP INDEX IF EXISTS mangas_url_index;
DROP VIEW IF EXISTS historyView;
DROP VIEW IF EXISTS libraryView;
DROP VIEW IF EXISTS updatesView;
ALTER TABLE mangas RENAME TO manga_temp;
CREATE TABLE mangas(
id INTEGER NOT NULL PRIMARY KEY,
source INTEGER NOT NULL,
url TEXT NOT NULL,
artist TEXT,
author TEXT,
description TEXT,
genre TEXT AS List<String>,
title TEXT NOT NULL,
status INTEGER NOT NULL,
thumbnail_url TEXT,
favorite INTEGER AS Boolean NOT NULL,
last_update INTEGER,
next_update INTEGER,
initialized INTEGER AS Boolean NOT NULL,
viewer INTEGER NOT NULL,
chapter_flags INTEGER NOT NULL,
cover_last_modified INTEGER NOT NULL,
date_added INTEGER NOT NULL,
update_strategy INTEGER AS UpdateStrategy NOT NULL DEFAULT 0,
calculate_interval INTEGER DEFAULT 0 NOT NULL,
last_modified_at INTEGER NOT NULL DEFAULT 0,
favorite_modified_at INTEGER,
version INTEGER NOT NULL DEFAULT 0,
is_syncing INTEGER NOT NULL DEFAULT 0,
UNIQUE(source, url) ON CONFLICT IGNORE
);
INSERT INTO mangas
SELECT
_id, 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, last_modified_at, favorite_modified_at, version, is_syncing
FROM manga_temp;
ALTER TABLE categories RENAME TO categories_temp;
CREATE TABLE categories(
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
`order` INTEGER NOT NULL,
flags INTEGER NOT NULL,
UNIQUE(`order`) ON CONFLICT ABORT
);
INSERT INTO categories
SELECT _id, name, (RANK() OVER (ORDER BY sort)) - 2, flags
FROM categories_temp;
ALTER TABLE chapters RENAME TO chapters_temp;
CREATE TABLE chapters(
id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL,
url TEXT NOT NULL,
name TEXT NOT NULL,
scanlator TEXT,
read INTEGER AS Boolean NOT NULL,
bookmark INTEGER AS Boolean NOT NULL,
last_page_read INTEGER NOT NULL,
chapter_number REAL NOT NULL,
source_order INTEGER NOT NULL,
date_fetch INTEGER NOT NULL,
date_upload INTEGER NOT NULL,
last_modified_at INTEGER NOT NULL DEFAULT 0,
version INTEGER NOT NULL DEFAULT 0,
is_syncing INTEGER NOT NULL DEFAULT 0,
UNIQUE(manga_id, url) ON CONFLICT IGNORE,
FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE
);
INSERT INTO chapters
SELECT
_id, manga_id, url, name, scanlator, read, bookmark, last_page_read, chapter_number, source_order,
date_fetch, date_upload, last_modified_at, version, is_syncing
FROM chapters_temp;
ALTER TABLE excluded_scanlators RENAME TO excluded_scanlators_temp;
CREATE TABLE excluded_scanlators(
manga_id INTEGER NOT NULL,
scanlator TEXT NOT NULL,
UNIQUE(manga_id, scanlator) ON CONFLICT IGNORE,
FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE
);
INSERT INTO excluded_scanlators
SELECT manga_id, scanlator
FROM excluded_scanlators_temp;
ALTER TABLE history RENAME TO history_temp;
CREATE TABLE history(
id INTEGER NOT NULL PRIMARY KEY,
chapter_id INTEGER NOT NULL UNIQUE,
last_read INTEGER AS Date,
time_read INTEGER NOT NULL,
FOREIGN KEY(chapter_id) REFERENCES chapters (id)
ON DELETE CASCADE
);
INSERT INTO history
SELECT history_id, history_chapter_id, history_last_read, history_time_read
FROM history_temp;
ALTER TABLE mangas_categories RENAME TO mangas_categories_temp;
CREATE TABLE mangas_categories(
manga_id INTEGER NOT NULL,
category_id INTEGER NOT NULL,
UNIQUE(manga_id, category_id) ON CONFLICT IGNORE,
FOREIGN KEY(category_id) REFERENCES categories(id)
ON DELETE CASCADE,
FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE
);
INSERT INTO mangas_categories
SELECT manga_id, category_id
FROM mangas_categories_temp;
ALTER TABLE manga_sync RENAME TO manga_sync_temp;
CREATE TABLE manga_sync(
id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL,
sync_id INTEGER NOT NULL,
remote_id INTEGER NOT NULL,
library_id INTEGER,
title TEXT NOT NULL,
last_chapter_read REAL NOT NULL,
total_chapters INTEGER NOT NULL,
status INTEGER NOT NULL,
score REAL NOT NULL,
remote_url TEXT NOT NULL,
start_date INTEGER NOT NULL,
finish_date INTEGER NOT NULL,
UNIQUE(manga_id, sync_id) ON CONFLICT IGNORE,
FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE
);
INSERT INTO manga_sync
SELECT
_id, manga_id, sync_id, remote_id, library_id, title, last_chapter_read, total_chapters, status,
score, remote_url, start_date, finish_date
FROM manga_sync_temp;
CREATE INDEX chapters_manga_id_index ON chapters(manga_id);
CREATE INDEX excluded_scanlators_manga_id_index ON excluded_scanlators(manga_id);
CREATE INDEX history_chapter_id_index ON history(chapter_id);
CREATE INDEX manga_sync_manga_id_index ON manga_sync(manga_id);
CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1;
CREATE INDEX mangas_url_index ON mangas(url);
CREATE INDEX mangas_categories_manga_id_index ON mangas_categories(manga_id);
CREATE INDEX mangas_categories_category_id_index ON mangas_categories(category_id);
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,
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;
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.category_id, 0) AS category
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 mangas_categories AS MC
ON MC.manga_id = M.id
WHERE M.favorite = 1;
CREATE VIEW updatesView AS
SELECT
mangas.id AS mangaId,
mangas.title AS mangaTitle,
chapters.id AS chapterId,
chapters.name AS chapterName,
chapters.scanlator,
chapters.read,
chapters.bookmark,
chapters.last_page_read,
mangas.source,
mangas.favorite,
mangas.thumbnail_url AS thumbnailUrl,
mangas.cover_last_modified AS coverLastModified,
chapters.date_upload AS dateUpload,
chapters.date_fetch AS datefetch
FROM mangas JOIN chapters
ON mangas.id = chapters.manga_id
WHERE favorite = 1
AND date_fetch > date_added
ORDER BY date_fetch DESC;
DROP TABLE IF EXISTS manga_temp;
DROP TABLE IF EXISTS categories_temp;
DROP TABLE IF EXISTS chapters_temp;
DROP TABLE IF EXISTS history_temp;
DROP TABLE IF EXISTS mangas_categories_temp;
DROP TABLE IF EXISTS manga_sync_temp;

View File

@ -1,8 +1,8 @@
CREATE VIEW historyView AS CREATE VIEW historyView AS
SELECT SELECT
history._id AS id, history.id AS id,
mangas._id AS mangaId, mangas.id AS mangaId,
chapters._id AS chapterId, chapters.id AS chapterId,
mangas.title, mangas.title,
mangas.thumbnail_url AS thumbnailUrl, mangas.thumbnail_url AS thumbnailUrl,
mangas.source, mangas.source,
@ -15,16 +15,15 @@ SELECT
max_last_read.chapter_id AS maxReadAtChapterId max_last_read.chapter_id AS maxReadAtChapterId
FROM mangas FROM mangas
JOIN chapters JOIN chapters
ON mangas._id = chapters.manga_id ON mangas.id = chapters.manga_id
JOIN history JOIN history
ON chapters._id = history.chapter_id ON chapters.id = history.chapter_id
JOIN ( JOIN (
SELECT chapters.manga_id,chapters._id AS chapter_id, MAX(history.last_read) AS last_read SELECT chapters.manga_id,chapters.id AS chapter_id, MAX(history.last_read) AS last_read
FROM chapters JOIN history FROM chapters JOIN history
ON chapters._id = history.chapter_id ON chapters.id = history.chapter_id
GROUP BY chapters.manga_id GROUP BY chapters.manga_id
) AS max_last_read ) AS max_last_read;
ON chapters.manga_id = max_last_read.manga_id;
history: history:
SELECT SELECT

View File

@ -23,13 +23,13 @@ LEFT JOIN(
ON chapters.manga_id = excluded_scanlators.manga_id ON chapters.manga_id = excluded_scanlators.manga_id
AND chapters.scanlator = excluded_scanlators.scanlator AND chapters.scanlator = excluded_scanlators.scanlator
LEFT JOIN history LEFT JOIN history
ON chapters._id = history.chapter_id ON chapters.id = history.chapter_id
WHERE excluded_scanlators.scanlator IS NULL WHERE excluded_scanlators.scanlator IS NULL
GROUP BY chapters.manga_id GROUP BY chapters.manga_id
) AS C ) AS C
ON M._id = C.manga_id ON M.id = C.manga_id
LEFT JOIN mangas_categories AS MC LEFT JOIN mangas_categories AS MC
ON MC.manga_id = M._id ON MC.manga_id = M.id
WHERE M.favorite = 1; WHERE M.favorite = 1;
library: library:

View File

@ -1,8 +1,8 @@
CREATE VIEW updatesView AS CREATE VIEW updatesView AS
SELECT SELECT
mangas._id AS mangaId, mangas.id AS mangaId,
mangas.title AS mangaTitle, mangas.title AS mangaTitle,
chapters._id AS chapterId, chapters.id AS chapterId,
chapters.name AS chapterName, chapters.name AS chapterName,
chapters.scanlator, chapters.scanlator,
chapters.read, chapters.read,
@ -15,7 +15,7 @@ SELECT
chapters.date_upload AS dateUpload, chapters.date_upload AS dateUpload,
chapters.date_fetch AS datefetch chapters.date_fetch AS datefetch
FROM mangas JOIN chapters FROM mangas JOIN chapters
ON mangas._id = chapters.manga_id ON mangas.id = chapters.manga_id
WHERE favorite = 1 WHERE favorite = 1
AND date_fetch > date_added AND date_fetch > date_added
ORDER BY date_fetch DESC; ORDER BY date_fetch DESC;