diff --git a/sql/file_history.sql b/sql/file_history.sql index c1659d6..5cd63d9 100644 --- a/sql/file_history.sql +++ b/sql/file_history.sql @@ -1,6 +1,44 @@ -CREATE TABLE file_history ( - id integer PRIMARY KEY AUTOINCREMENT, - directory varchar(200) NOT NULL, - filename varchar(200) NOT NULL, - watch_date datetime NOT NULL +DROP TABLE IF EXISTS temp_file_history; + +CREATE TABLE IF NOT EXISTS temp_file_history ( + id integer PRIMARY KEY AUTOINCREMENT, + directory varchar(200) NOT NULL, + filename varchar(200) NOT NULL, + watch_date DATETIME NOT NULL, + CHECK (LENGTH(directory) > 0), + CHECK (LENGTH(filename) > 0), + UNIQUE (directory, filename) ); + +-- make sure table has been set up at least once +CREATE TABLE IF NOT EXISTS file_history ( + id integer PRIMARY KEY AUTOINCREMENT, + directory varchar(200) NOT NULL, + filename varchar(200) NOT NULL, + watch_date DATETIME NOT NULL, + CHECK (LENGTH(directory) > 0), + CHECK (LENGTH(filename) > 0), + UNIQUE (directory, filename) +); + + +INSERT OR IGNORE INTO temp_file_history +SELECT * FROM file_history; + +DROP TABLE file_history; + +CREATE TABLE file_history ( + id integer PRIMARY KEY AUTOINCREMENT, + directory varchar(200) NOT NULL, + filename varchar(200) NOT NULL, + watch_date DATETIME NOT NULL, + CHECK (LENGTH(directory) > 0), + CHECK (LENGTH(filename) > 0), + UNIQUE (directory, filename) +); +CREATE UNIQUE INDEX dir_filename_idx ON file_history(directory, filename); + +INSERT INTO file_history +SELECT * FROM temp_file_history; + +DROP TABLE temp_file_history; diff --git a/sql/search_history_tbl.sql b/sql/search_history_tbl.sql index 2311666..538a361 100644 --- a/sql/search_history_tbl.sql +++ b/sql/search_history_tbl.sql @@ -1,6 +1,38 @@ -CREATE TABLE search_history ( +DROP TABLE IF EXISTS temp_search_history; + +CREATE TABLE IF NOT EXISTS temp_search_history ( id integer PRIMARY KEY AUTOINCREMENT, - anime_name varchar(200), - search_date datetime NOT NULL + anime_name varchar(200) NOT NULL, + search_date DATETIME NOT NULL, + CHECK (LENGTH(anime_name) > 0), + UNIQUE (anime_name, search_date) ); +CREATE TABLE IF NOT EXISTS search_history ( + id integer PRIMARY KEY AUTOINCREMENT, + anime_name varchar(200) NOT NULL, + search_date DATETIME NOT NULL, + CHECK (LENGTH(anime_name) > 0), + UNIQUE (anime_name, search_date) +); + +INSERT OR IGNORE INTO temp_search_history +SELECT * FROM search_history; + +DROP TABLE IF EXISTS search_history; + +CREATE TABLE search_history ( + id integer PRIMARY KEY AUTOINCREMENT, + anime_name varchar(200) NOT NULL, + search_date DATETIME NOT NULL, + CHECK (LENGTH(anime_name) > 0), + UNIQUE (anime_name, search_date) +); + +CREATE UNIQUE INDEX anime_search_idx ON search_history (anime_name, search_date); + +INSERT INTO search_history +SELECT * FROM temp_search_history; + +DROP TABLE temp_search_history; + diff --git a/sql/watch_history_tbl.sql b/sql/watch_history_tbl.sql index 2c8e0f3..4a6e9dc 100644 --- a/sql/watch_history_tbl.sql +++ b/sql/watch_history_tbl.sql @@ -1,7 +1,41 @@ -CREATE TABLE watch_history ( - id integer PRIMARY KEY AUTOINCREMENT, - anime_name varchar(200) NOT NULL, - episode_number integer NOT NULL, - watch_date datetime NOT NULL +DROP TABLE IF EXISTS temp_watch_history; +CREATE TABLE temp_watch_history ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + anime_name VARCHAR(200) NOT NULL, + episode_number INTEGER NOT NULL, + watch_date DATETIME NOT NULL, + CHECK(LENGTH(anime_name) > 0), + CHECK(episode_number > 0), + UNIQUE(anime_name, episode_number, watch_date) ); +CREATE TABLE IF NOT EXISTS watch_history ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + anime_name VARCHAR(200) NOT NULL, + episode_number INTEGER NOT NULL, + watch_date DATETIME NOT NULL, + CHECK(LENGTH(anime_name) > 0), + CHECK(episode_number > 0), + UNIQUE(anime_name, episode_number, watch_date) +); + +INSERT OR IGNORE INTO temp_watch_history +SELECT * FROM watch_history; + +DROP TABLE watch_history; + +CREATE TABLE watch_history ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + anime_name VARCHAR(200) NOT NULL, + episode_number INTEGER NOT NULL, + watch_date DATETIME NOT NULL, + CHECK(LENGTH(anime_name) > 0), + CHECK(episode_number > 0), + UNIQUE(anime_name, episode_number, watch_date) +); +CREATE UNIQUE INDEX IF NOT EXISTS watch_anime_episode_idx ON watch_history(anime_name, episode_number); + +INSERT INTO watch_history +SELECT * FROM temp_watch_history; + +DROP TABLE IF EXISTS temp_watch_history;