add game & game_players table and rename password value in user to password_hash

This commit is contained in:
2026-02-24 18:29:32 +01:00
parent 83119a2443
commit b1556515a1
2 changed files with 31 additions and 8 deletions
+29 -6
View File
@@ -22,14 +22,37 @@ def init_db(app):
db.execute("PRAGMA foreign_keys=ON;") db.execute("PRAGMA foreign_keys=ON;")
# create inital tables # create inital tables
db.execute(""" db.executescript("""
CREATE TABLE IF NOT EXISTS users ( CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT UNIQUE NOT NULL, id INTEGER PRIMARY KEY AUTOINCREMENT,username TEXT UNIQUE NOT NULL,
password TEXT NOT NULL, password_hash TEXT NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP 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() db.commit()
+2 -2
View File
@@ -19,7 +19,7 @@ def login():
(username,) (username,)
).fetchone() ).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"]) user = User(id=row["id"], username=row["username"])
login_user(user) login_user(user)
@@ -54,7 +54,7 @@ def register():
hashed_password = generate_password_hash(password) hashed_password = generate_password_hash(password)
db.execute( db.execute(
"INSERT INTO users (username, password) VALUES (?, ?)", "INSERT INTO users (username, password_hash) VALUES (?, ?)",
(username, hashed_password) (username, hashed_password)
) )
db.commit() db.commit()