Improve SQL table definitions

This commit is contained in:
Kiana Sheibani 2023-12-16 23:09:44 -05:00
parent 54696c1b0e
commit 26c2813b09
Signed by: toki
GPG key ID: 6CB106C25E86A9F7

View file

@ -38,8 +38,7 @@ fn datasets_path(config_dir: &Path) -> std::io::Result<PathBuf> {
pub fn open_datasets(config_dir: &Path) -> sqlite::Result<Connection> { pub fn open_datasets(config_dir: &Path) -> sqlite::Result<Connection> {
let path = datasets_path(config_dir).unwrap(); let path = datasets_path(config_dir).unwrap();
let query = "PRAGMA foreign_keys = ON; let query = "
CREATE TABLE IF NOT EXISTS datasets ( CREATE TABLE IF NOT EXISTS datasets (
name TEXT UNIQUE NOT NULL, name TEXT UNIQUE NOT NULL,
last_sync INTEGER NOT NULL, last_sync INTEGER NOT NULL,
@ -68,10 +67,9 @@ CREATE TABLE IF NOT EXISTS events (
) STRICT; ) STRICT;
CREATE TABLE IF NOT EXISTS sets ( CREATE TABLE IF NOT EXISTS sets (
id TEXT UNIQUE NOT NULL, id TEXT PRIMARY KEY,
event INTEGER NOT NULL, event INTEGER NOT NULL REFERENCES events
FOREIGN KEY(event) REFERENCES events ) STRICT, WITHOUT ROWID;
) STRICT;
"; ";
let connection = sqlite::open(path)?; let connection = sqlite::open(path)?;
@ -139,8 +137,6 @@ pub fn rename_dataset(connection: &Connection, old: &str, new: &str) -> sqlite::
r#"UPDATE datasets SET name = '{1}' WHERE name = '{0}'; r#"UPDATE datasets SET name = '{1}' WHERE name = '{0}';
ALTER TABLE "{0}_players" RENAME TO "{1}_players"; ALTER TABLE "{0}_players" RENAME TO "{1}_players";
ALTER TABLE "{0}_network" RENAME TO "{1}_network"; ALTER TABLE "{0}_network" RENAME TO "{1}_network";
DROP INDEX "{0}_network_A";
CREATE INDEX "{1}_network_A" ON "{1}_network" (player_A);
DROP INDEX "{0}_network_B"; DROP INDEX "{0}_network_B";
CREATE INDEX "{1}_network_B" ON "{1}_network" (player_B);"#, CREATE INDEX "{1}_network_B" ON "{1}_network" (player_B);"#,
old, new old, new
@ -157,7 +153,7 @@ pub fn new_dataset(
let query1 = r#"INSERT INTO datasets VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#; let query1 = r#"INSERT INTO datasets VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"#;
let query2 = format!( let query2 = format!(
r#"CREATE TABLE "{0}_players" ( r#"CREATE TABLE "{0}_players" (
id INTEGER PRIMARY KEY, id INTEGER PRIMARY KEY REFERENCES players,
last_played INTEGER NOT NULL, last_played INTEGER NOT NULL,
deviation REAL NOT NULL, deviation REAL NOT NULL,
volatility REAL NOT NULL, volatility REAL NOT NULL,
@ -182,7 +178,7 @@ CREATE TABLE "{0}_network" (
sets TEXT AS (sets_A || sets_B), sets TEXT AS (sets_A || sets_B),
sets_count INTEGER AS (sets_count_A + sets_count_B), sets_count INTEGER AS (sets_count_A + sets_count_B),
UNIQUE (player_A, player_B), PRIMARY KEY (player_A, player_B),
CHECK (player_A < player_B), CHECK (player_A < player_B),
FOREIGN KEY(player_A) REFERENCES "{0}_players" FOREIGN KEY(player_A) REFERENCES "{0}_players"
ON DELETE CASCADE, ON DELETE CASCADE,