Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tweak database table and view schema #1195

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -330,13 +330,13 @@ class MangaRestorer(
null
} else {
// New history entry
item.copy(chapterId = chapter._id)
item.copy(chapterId = chapter.id)
}
}

// Update history entry
item.copy(
id = dbHistory._id,
id = dbHistory.id,
chapterId = dbHistory.chapter_id,
readAt = max(item.readAt?.time ?: 0L, dbHistory.last_read?.time ?: 0L)
.takeIf { it > 0L }
Expand Down
36 changes: 18 additions & 18 deletions data/src/main/sqldelight/tachiyomi/data/categories.sq
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
CREATE TABLE categories(
_id INTEGER NOT NULL PRIMARY KEY,
id INTEGER NOT NULL PRIMARY KEY,
name TEXT NOT NULL,
sort INTEGER NOT NULL,
flags INTEGER NOT NULL
`order` INTEGER NOT NULL,
flags INTEGER NOT NULL,
UNIQUE(`order`) ON CONFLICT ABORT
);

-- 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
CREATE TRIGGER IF NOT EXISTS system_category_delete_trigger BEFORE DELETE
ON categories
BEGIN SELECT CASE
WHEN old._id <= 0 THEN
WHEN old.id <= 0 THEN
RAISE(ABORT, "System category can't be deleted")
END;
END;

getCategory:
SELECT *
FROM categories
WHERE _id = :id
LIMIT 1;
WHERE id = :id;

getCategories:
SELECT
_id AS id,
id,
name,
sort AS `order`,
`order`,
flags
FROM categories
ORDER BY sort;
ORDER BY `order`;

getCategoriesByMangaId:
SELECT
C._id AS id,
C.id,
C.name,
C.sort AS `order`,
C.`order`,
C.flags
FROM categories C
JOIN mangas_categories MC
ON C._id = MC.category_id
ON C.id = MC.category_id
WHERE MC.manga_id = :mangaId;

insert:
INSERT INTO categories(name, sort, flags)
INSERT INTO categories(name, `order`, flags)
VALUES (:name, :order, :flags);

delete:
DELETE FROM categories
WHERE _id = :categoryId;
WHERE id = :categoryId;

update:
UPDATE categories
SET name = coalesce(:name, name),
sort = coalesce(:order, sort),
`order` = coalesce(:order, `order`),
flags = coalesce(:flags, flags)
WHERE _id = :categoryId;
WHERE id = :categoryId;

updateAllFlags:
UPDATE categories SET
flags = coalesce(?, flags);

selectLastInsertedRowId:
SELECT last_insert_rowid();
SELECT last_insert_rowid();
24 changes: 12 additions & 12 deletions data/src/main/sqldelight/tachiyomi/data/chapters.sq
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import kotlin.Boolean;

CREATE TABLE chapters(
_id INTEGER NOT NULL PRIMARY KEY,
id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL,
url TEXT NOT NULL,
name TEXT NOT NULL,
Expand All @@ -16,20 +16,20 @@ CREATE TABLE chapters(
last_modified_at INTEGER NOT NULL DEFAULT 0,
version 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
);

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
AFTER UPDATE ON chapters
FOR EACH ROW
BEGIN
UPDATE chapters
SET last_modified_at = strftime('%s', 'now')
WHERE _id = new._id;
UPDATE chapters
SET last_modified_at = strftime('%s', 'now')
WHERE id = new.id;
END;

CREATE TRIGGER update_chapter_and_manga_version AFTER UPDATE ON chapters
Expand All @@ -41,17 +41,17 @@ WHEN new.is_syncing = 0 AND (
BEGIN
-- Update the chapter version
UPDATE chapters SET version = version + 1
WHERE _id = new._id;
WHERE id = new.id;

-- Update the manga version
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;

getChapterById:
SELECT *
FROM chapters
WHERE _id = :id;
WHERE id = :id;

getChaptersByMangaId:
SELECT C.*
Expand Down Expand Up @@ -89,7 +89,7 @@ AND manga_id = :mangaId;

removeChaptersWithIds:
DELETE FROM chapters
WHERE _id IN :chapterIds;
WHERE id IN :chapterIds;

resetIsSyncing:
UPDATE chapters
Expand All @@ -115,7 +115,7 @@ SET manga_id = coalesce(:mangaId, manga_id),
date_upload = coalesce(:dateUpload, date_upload),
version = coalesce(:version, version),
is_syncing = coalesce(:isSyncing, is_syncing)
WHERE _id = :chapterId;
WHERE id = :chapterId;

selectLastInsertedRowId:
SELECT last_insert_rowid();
SELECT last_insert_rowid();
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
CREATE TABLE excluded_scanlators(
manga_id INTEGER 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
);

Expand Down
31 changes: 16 additions & 15 deletions data/src/main/sqldelight/tachiyomi/data/history.sq
Original file line number Diff line number Diff line change
@@ -1,54 +1,55 @@
import java.util.Date;

CREATE TABLE history(
_id INTEGER NOT NULL PRIMARY KEY,
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)
UNIQUE(chapter_id) ON CONFLICT IGNORE,
FOREIGN KEY(chapter_id) REFERENCES chapters (id)
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:
SELECT
H._id,
H.id,
H.chapter_id,
H.last_read,
H.time_read
FROM history H
JOIN chapters C
ON H.chapter_id = C._id
WHERE C.manga_id = :mangaId AND C._id = H.chapter_id;
ON H.chapter_id = C.id
WHERE C.manga_id = :mangaId AND C.id = H.chapter_id;

getHistoryByChapterUrl:
SELECT
H._id,
H.id,
H.chapter_id,
H.last_read,
H.time_read
FROM history H
JOIN chapters C
ON H.chapter_id = C._id
WHERE C.url = :chapterUrl AND C._id = H.chapter_id;
ON H.chapter_id = C.id
WHERE C.url = :chapterUrl AND C.id = H.chapter_id;

resetHistoryById:
UPDATE history
SET last_read = 0
WHERE _id = :historyId;
WHERE id = :historyId;

resetHistoryByMangaId:
UPDATE history
SET last_read = 0
WHERE _id IN (
SELECT H._id
WHERE id IN (
SELECT H.id
FROM mangas M
INNER JOIN chapters C
ON M._id = C.manga_id
ON M.id = C.manga_id
INNER JOIN history H
ON C._id = H.chapter_id
WHERE M._id = :mangaId
ON C.id = H.chapter_id
WHERE M.id = :mangaId
);

removeAllHistory:
Expand Down
12 changes: 7 additions & 5 deletions data/src/main/sqldelight/tachiyomi/data/manga_sync.sq
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
CREATE TABLE manga_sync(
_id INTEGER NOT NULL PRIMARY KEY,
id INTEGER NOT NULL PRIMARY KEY,
manga_id INTEGER NOT NULL,
sync_id INTEGER NOT NULL,
remote_id INTEGER NOT NULL,
Expand All @@ -12,11 +12,13 @@ CREATE TABLE manga_sync(
remote_url TEXT NOT NULL,
start_date INTEGER NOT NULL,
finish_date INTEGER NOT NULL,
UNIQUE (manga_id, sync_id) ON CONFLICT REPLACE,
FOREIGN KEY(manga_id) REFERENCES mangas (_id)
UNIQUE(manga_id, sync_id) ON CONFLICT IGNORE,
FOREIGN KEY(manga_id) REFERENCES mangas(id)
ON DELETE CASCADE
);

CREATE INDEX manga_sync_manga_id_index ON manga_sync(manga_id);

delete:
DELETE FROM manga_sync
WHERE manga_id = :mangaId AND sync_id = :syncId;
Expand All @@ -28,7 +30,7 @@ FROM manga_sync;
getTrackById:
SELECT *
FROM manga_sync
WHERE _id = :id;
WHERE id = :id;

getTracksByMangaId:
SELECT *
Expand All @@ -54,4 +56,4 @@ SET
remote_url = coalesce(:trackingUrl, remote_url),
start_date = coalesce(:startDate, start_date),
finish_date = coalesce(:finishDate, finish_date)
WHERE _id = :id;
WHERE id = :id;
23 changes: 11 additions & 12 deletions data/src/main/sqldelight/tachiyomi/data/mangas.sq
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import kotlin.Boolean;
import kotlin.String;

CREATE TABLE mangas(
_id INTEGER NOT NULL PRIMARY KEY,
id INTEGER NOT NULL PRIMARY KEY,
source INTEGER NOT NULL,
url TEXT NOT NULL,
artist TEXT,
Expand All @@ -27,7 +27,8 @@ CREATE TABLE mangas(
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
is_syncing INTEGER NOT NULL DEFAULT 0,
UNIQUE(source, url) ON CONFLICT IGNORE
);

CREATE INDEX library_favorite_index ON mangas(favorite) WHERE favorite = 1;
Expand All @@ -38,7 +39,7 @@ AFTER UPDATE OF favorite ON mangas
BEGIN
UPDATE mangas
SET favorite_modified_at = strftime('%s', 'now')
WHERE _id = new._id;
WHERE id = new.id;
END;

CREATE TRIGGER update_last_modified_at_mangas
Expand All @@ -47,13 +48,13 @@ FOR EACH ROW
BEGIN
UPDATE mangas
SET last_modified_at = strftime('%s', 'now')
WHERE _id = new._id;
WHERE id = new.id;
END;

CREATE TRIGGER update_manga_version AFTER UPDATE ON mangas
BEGIN
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.description != old.description OR
new.favorite != old.favorite
Expand All @@ -63,15 +64,13 @@ END;
getMangaById:
SELECT *
FROM mangas
WHERE _id = :id;
WHERE id = :id;

-- TODO: this should ideally never really have more than 1 result
getMangaByUrlAndSource:
SELECT *
FROM mangas
WHERE url = :url
AND source = :source
LIMIT 1;
AND source = :source;

getFavorites:
SELECT *
Expand All @@ -81,7 +80,7 @@ WHERE favorite = 1;
getReadMangaNotInLibrary:
SELECT *
FROM mangas
WHERE favorite = 0 AND _id IN (
WHERE favorite = 0 AND id IN (
SELECT DISTINCT chapters.manga_id
FROM chapters
WHERE read = 1 OR last_page_read != 0
Expand Down Expand Up @@ -119,7 +118,7 @@ SELECT *
FROM mangas
WHERE favorite = 1
AND LOWER(title) = :title
AND _id != :id;
AND id != :id;

getUpcomingManga:
SELECT *
Expand Down Expand Up @@ -176,7 +175,7 @@ UPDATE mangas SET
calculate_interval = coalesce(:calculateInterval, calculate_interval),
version = coalesce(:version, version),
is_syncing = coalesce(:isSyncing, is_syncing)
WHERE _id = :mangaId;
WHERE id = :mangaId;

selectLastInsertedRowId:
SELECT last_insert_rowid();
Loading