From b1556515a107cceff04ea51a50a32c720579b831 Mon Sep 17 00:00:00 2001 From: tototomate123 Date: Tue, 24 Feb 2026 18:29:32 +0100 Subject: [PATCH] add game & game_players table and rename password value in user to password_hash --- app/db.py | 35 +++++++++++++++++++++++++++++------ app/routes/auth.py | 4 ++-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/app/db.py b/app/db.py index 426182c..7d7f448 100644 --- a/app/db.py +++ b/app/db.py @@ -22,14 +22,37 @@ def init_db(app): db.execute("PRAGMA foreign_keys=ON;") # create inital tables - db.execute(""" + db.executescript(""" CREATE TABLE IF NOT EXISTS users ( - id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT UNIQUE NOT NULL, - password TEXT NOT NULL, - created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT UNIQUE NOT NULL, + password_hash TEXT NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ); + + CREATE TABLE IF NOT EXISTS games ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + final_fen TEXT, + termination TEXT CHECK(termination IN ('checkmate', 'resignation', 'timeout', 'draw', 'other')) + ); + + CREATE TABLE IF NOT EXISTS game_players ( + game_id INTEGER NOT NULL, + player_id INTEGER NOT NULL, + color TEXT NOT NULL CHECK(color IN ('white', 'black')), + + PRIMARY KEY (game_id, player_id), + UNIQUE (game_id, color), + + FOREIGN KEY (game_id) REFERENCES games(id) ON DELETE CASCADE, + FOREIGN KEY (player_id) REFERENCES users(id) ON DELETE CASCADE + ); + + CREATE INDEX IF NOT EXISTS idx_game_players_game_id + ON game_players(game_id); + + CREATE INDEX IF NOT EXISTS idx_game_players_player_id + ON game_players(player_id); """ ) - - db.commit() \ No newline at end of file diff --git a/app/routes/auth.py b/app/routes/auth.py index 2998c5a..5c02d60 100644 --- a/app/routes/auth.py +++ b/app/routes/auth.py @@ -19,7 +19,7 @@ def login(): (username,) ).fetchone() - if row and check_password_hash(row["password"], password): + if row and check_password_hash(row["password_hash"], password): user = User(id=row["id"], username=row["username"]) login_user(user) @@ -54,7 +54,7 @@ def register(): hashed_password = generate_password_hash(password) db.execute( - "INSERT INTO users (username, password) VALUES (?, ?)", + "INSERT INTO users (username, password_hash) VALUES (?, ?)", (username, hashed_password) ) db.commit()