This commit is contained in:
2025-09-11 15:41:48 +02:00
parent e7b964078c
commit 86c2cd0d70
6 changed files with 799 additions and 99 deletions

View File

@@ -23,39 +23,54 @@ const migrations = [
await conn.query(`
CREATE TABLE IF NOT EXISTS users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL
username VARCHAR(255) NOT NULL
)
`);
await conn.query(`
CREATE TABLE IF NOT EXISTS user_settings (
user INT NOT NULL,
input_method INT,
PRIMARY KEY (user),
FOREIGN KEY (user) REFERENCES users(id) ON DELETE CASCADE
)
`);
await conn.query(`
CREATE TABLE IF NOT EXISTS games (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL UNIQUE
)
`);
await conn.query(`
CREATE TABLE IF NOT EXISTS scores (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT NOT NULL,
game_id INT NOT NULL,
score INT NOT NULL,
winner INT,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (game_id) REFERENCES games(id),
UNIQUE(user_id, game_id)
FOREIGN KEY (winner) REFERENCES users(id) ON DELETE SET NULL
)
`);
await conn.query(`
CREATE INDEX IF NOT EXISTS idx_scores_game_score
ON scores(game_id, score DESC)
CREATE TABLE IF NOT EXISTS game_players (
id INT PRIMARY KEY AUTO_INCREMENT,
game INT NOT NULL,
user INT NOT NULL,
FOREIGN KEY (game) REFERENCES games(id) ON DELETE CASCADE,
FOREIGN KEY (user) REFERENCES users(id) ON DELETE CASCADE,
UNIQUE (game, user)
)
`);
await pool.query(
`INSERT INTO games (name) VALUES (?) ON DUPLICATE KEY UPDATE id=id`,
['darts']
);
await conn.query(`
CREATE TABLE IF NOT EXISTS turns (
id INT PRIMARY KEY AUTO_INCREMENT,
game INT NOT NULL,
user INT NOT NULL,
round_number INT NOT NULL,
start_points INT,
first_throw INT,
second_throw INT,
third_throw INT,
end_points INT,
FOREIGN KEY (game) REFERENCES games(id) ON DELETE CASCADE,
FOREIGN KEY (user) REFERENCES users(id) ON DELETE CASCADE
)
`);
}
}
];