add game & game_players table and rename password value in user to password_hash
This commit is contained in:
@@ -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,
|
||||
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()
|
||||
+2
-2
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user