2023-10-03 20:47:38 +00:00
|
|
|
---------------------------------------------------------------
|
|
|
|
------------------------ Normal Tables ------------------------
|
|
|
|
---------------------------------------------------------------
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
-- Authentication
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
|
|
id TEXT NOT NULL PRIMARY KEY,
|
|
|
|
|
|
|
|
pass TEXT NOT NULL,
|
2024-01-28 02:02:08 +00:00
|
|
|
auth_hash TEXT NOT NULL,
|
2023-09-18 23:57:18 +00:00
|
|
|
admin BOOLEAN NOT NULL DEFAULT 0 CHECK (admin IN (0, 1)),
|
2024-03-11 17:13:26 +00:00
|
|
|
timezone TEXT NOT NULL DEFAULT 'Europe/London',
|
2023-09-18 23:57:18 +00:00
|
|
|
|
2023-10-06 01:04:57 +00:00
|
|
|
created_at DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now'))
|
2023-09-18 23:57:18 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
-- Books / Documents
|
|
|
|
CREATE TABLE IF NOT EXISTS documents (
|
|
|
|
id TEXT NOT NULL PRIMARY KEY,
|
|
|
|
|
|
|
|
md5 TEXT,
|
2024-05-18 20:47:26 +00:00
|
|
|
basepath TEXT,
|
2023-09-18 23:57:18 +00:00
|
|
|
filepath TEXT,
|
2023-09-23 18:14:57 +00:00
|
|
|
coverfile TEXT,
|
2023-09-18 23:57:18 +00:00
|
|
|
title TEXT,
|
|
|
|
author TEXT,
|
|
|
|
series TEXT,
|
|
|
|
series_index INTEGER,
|
|
|
|
lang TEXT,
|
|
|
|
description TEXT,
|
2023-10-01 23:17:22 +00:00
|
|
|
words INTEGER,
|
2023-09-23 18:14:57 +00:00
|
|
|
|
|
|
|
gbid TEXT,
|
2023-09-18 23:57:18 +00:00
|
|
|
olid TEXT,
|
2023-09-23 18:14:57 +00:00
|
|
|
isbn10 TEXT,
|
|
|
|
isbn13 TEXT,
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
synced BOOLEAN NOT NULL DEFAULT 0 CHECK (synced IN (0, 1)),
|
|
|
|
deleted BOOLEAN NOT NULL DEFAULT 0 CHECK (deleted IN (0, 1)),
|
|
|
|
|
2023-10-06 01:04:57 +00:00
|
|
|
updated_at DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
|
|
|
created_at DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now'))
|
2023-09-18 23:57:18 +00:00
|
|
|
);
|
|
|
|
|
2023-09-23 18:14:57 +00:00
|
|
|
-- Metadata
|
|
|
|
CREATE TABLE IF NOT EXISTS metadata (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
document_id TEXT NOT NULL,
|
|
|
|
|
|
|
|
title TEXT,
|
|
|
|
author TEXT,
|
|
|
|
description TEXT,
|
|
|
|
gbid TEXT,
|
|
|
|
olid TEXT,
|
|
|
|
isbn10 TEXT,
|
|
|
|
isbn13 TEXT,
|
|
|
|
|
2023-10-06 01:04:57 +00:00
|
|
|
created_at DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
2023-09-23 18:14:57 +00:00
|
|
|
|
|
|
|
FOREIGN KEY (document_id) REFERENCES documents (id)
|
|
|
|
);
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
-- Devices
|
|
|
|
CREATE TABLE IF NOT EXISTS devices (
|
|
|
|
id TEXT NOT NULL PRIMARY KEY,
|
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
|
|
|
|
device_name TEXT NOT NULL,
|
2023-10-06 01:04:57 +00:00
|
|
|
last_synced DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
|
|
|
created_at DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
2023-09-18 23:57:18 +00:00
|
|
|
sync BOOLEAN NOT NULL DEFAULT 1 CHECK (sync IN (0, 1)),
|
|
|
|
|
|
|
|
FOREIGN KEY (user_id) REFERENCES users (id)
|
|
|
|
);
|
|
|
|
|
|
|
|
-- User Document Progress
|
|
|
|
CREATE TABLE IF NOT EXISTS document_progress (
|
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
document_id TEXT NOT NULL,
|
|
|
|
device_id TEXT NOT NULL,
|
|
|
|
|
|
|
|
percentage REAL NOT NULL,
|
|
|
|
progress TEXT NOT NULL,
|
2023-10-06 01:04:57 +00:00
|
|
|
created_at DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
FOREIGN KEY (user_id) REFERENCES users (id),
|
|
|
|
FOREIGN KEY (document_id) REFERENCES documents (id),
|
|
|
|
FOREIGN KEY (device_id) REFERENCES devices (id),
|
|
|
|
PRIMARY KEY (user_id, document_id, device_id)
|
|
|
|
);
|
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
-- Read Activity
|
|
|
|
CREATE TABLE IF NOT EXISTS activity (
|
2023-09-18 23:57:18 +00:00
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
document_id TEXT NOT NULL,
|
|
|
|
device_id TEXT NOT NULL,
|
|
|
|
|
|
|
|
start_time DATETIME NOT NULL,
|
2023-11-03 23:38:31 +00:00
|
|
|
start_percentage REAL NOT NULL,
|
|
|
|
end_percentage REAL NOT NULL,
|
|
|
|
|
2023-10-03 20:47:38 +00:00
|
|
|
duration INTEGER NOT NULL,
|
2023-10-06 01:04:57 +00:00
|
|
|
created_at DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now')),
|
2023-09-18 23:57:18 +00:00
|
|
|
|
|
|
|
FOREIGN KEY (user_id) REFERENCES users (id),
|
|
|
|
FOREIGN KEY (document_id) REFERENCES documents (id),
|
|
|
|
FOREIGN KEY (device_id) REFERENCES devices (id)
|
|
|
|
);
|
|
|
|
|
2024-02-02 01:05:35 +00:00
|
|
|
-- Settings
|
|
|
|
CREATE TABLE IF NOT EXISTS settings (
|
|
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
|
|
|
|
|
|
name TEXT NOT NULL,
|
|
|
|
value TEXT NOT NULL,
|
|
|
|
|
|
|
|
created_at DATETIME NOT NULL DEFAULT (STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now'))
|
|
|
|
);
|
|
|
|
|
2023-10-03 20:47:38 +00:00
|
|
|
---------------------------------------------------------------
|
|
|
|
----------------------- Temporary Tables ----------------------
|
|
|
|
---------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Temporary User Streaks Table (Cached from View)
|
|
|
|
CREATE TEMPORARY TABLE IF NOT EXISTS user_streaks (
|
|
|
|
user_id TEXT NOT NULL,
|
|
|
|
window TEXT NOT NULL,
|
|
|
|
|
|
|
|
max_streak INTEGER NOT NULL,
|
|
|
|
max_streak_start_date TEXT NOT NULL,
|
|
|
|
max_streak_end_date TEXT NOT NULL,
|
|
|
|
|
|
|
|
current_streak INTEGER NOT NULL,
|
|
|
|
current_streak_start_date TEXT NOT NULL,
|
|
|
|
current_streak_end_date TEXT NOT NULL
|
|
|
|
);
|
|
|
|
|
2024-01-24 04:00:51 +00:00
|
|
|
-- Temporary Document User Statistics Table (Cached from View)
|
2023-10-03 20:47:38 +00:00
|
|
|
CREATE TEMPORARY TABLE IF NOT EXISTS document_user_statistics (
|
|
|
|
document_id TEXT NOT NULL,
|
|
|
|
user_id TEXT NOT NULL,
|
2024-01-24 04:00:51 +00:00
|
|
|
percentage REAL NOT NULL,
|
2023-10-03 20:47:38 +00:00
|
|
|
last_read TEXT NOT NULL,
|
2023-11-03 23:38:31 +00:00
|
|
|
read_percentage REAL NOT NULL,
|
2024-01-24 04:00:51 +00:00
|
|
|
|
|
|
|
total_time_seconds INTEGER NOT NULL,
|
|
|
|
total_words_read INTEGER NOT NULL,
|
|
|
|
total_wpm REAL NOT NULL,
|
|
|
|
|
|
|
|
yearly_time_seconds INTEGER NOT NULL,
|
|
|
|
yearly_words_read INTEGER NOT NULL,
|
|
|
|
yearly_wpm REAL NOT NULL,
|
|
|
|
|
|
|
|
monthly_time_seconds INTEGER NOT NULL,
|
|
|
|
monthly_words_read INTEGER NOT NULL,
|
|
|
|
monthly_wpm REAL NOT NULL,
|
|
|
|
|
|
|
|
weekly_time_seconds INTEGER NOT NULL,
|
|
|
|
weekly_words_read INTEGER NOT NULL,
|
|
|
|
weekly_wpm REAL NOT NULL,
|
2023-11-04 01:37:26 +00:00
|
|
|
|
|
|
|
UNIQUE(document_id, user_id) ON CONFLICT REPLACE
|
2023-10-03 20:47:38 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
---------------------------------------------------------------
|
|
|
|
--------------------------- Indexes ---------------------------
|
|
|
|
---------------------------------------------------------------
|
|
|
|
|
2023-11-03 23:38:31 +00:00
|
|
|
CREATE INDEX IF NOT EXISTS activity_start_time ON activity (start_time);
|
|
|
|
CREATE INDEX IF NOT EXISTS activity_user_id ON activity (user_id);
|
|
|
|
CREATE INDEX IF NOT EXISTS activity_user_id_document_id ON activity (
|
2023-10-03 11:37:14 +00:00
|
|
|
user_id,
|
|
|
|
document_id
|
|
|
|
);
|
|
|
|
|
2024-01-26 00:22:57 +00:00
|
|
|
DROP VIEW IF EXISTS view_user_streaks;
|
|
|
|
DROP VIEW IF EXISTS view_document_user_statistics;
|
|
|
|
|
2023-10-03 20:47:38 +00:00
|
|
|
---------------------------------------------------------------
|
|
|
|
--------------------------- Triggers --------------------------
|
|
|
|
---------------------------------------------------------------
|
|
|
|
|
|
|
|
-- Update Trigger
|
|
|
|
CREATE TRIGGER IF NOT EXISTS update_documents_updated_at
|
|
|
|
BEFORE UPDATE ON documents BEGIN
|
|
|
|
UPDATE documents
|
2023-10-06 01:04:57 +00:00
|
|
|
SET updated_at = STRFTIME('%Y-%m-%dT%H:%M:%SZ', 'now')
|
2023-10-03 20:47:38 +00:00
|
|
|
WHERE id = old.id;
|
|
|
|
END;
|