-- players table CREATE TABLE IF NOT EXISTS bingo.players ( id serial UNIQUE PRIMARY KEY, username varchar(32) NOT NULL, expire timestamp DEFAULT (NOW() + interval '24 hours' ) ); -- lobbys table CREATE TABLE IF NOT EXISTS bingo.lobbys ( id serial UNIQUE PRIMARY KEY, admin_id serial references bingo.players(id) ON DELETE SET NULL, grid_size integer DEFAULT 3, expire timestamp DEFAULT (NOW() + interval '1 hour' ) ); -- lobbys-players table CREATE TABLE IF NOT EXISTS bingo.lobby_players ( player_id serial references bingo.players(id) ON DELETE CASCADE, lobby_id serial references bingo.lobbys(id) ON DELETE CASCADE, score integer DEFAULT 0, PRIMARY KEY (player_id, lobby_id) ); -- words table CREATE TABLE IF NOT EXISTS bingo.words ( id serial UNIQUE PRIMARY KEY, lobby_id serial references bingo.lobbys(id) ON DELETE CASCADE, heared integer DEFAULT 0, content varchar(254) NOT NULL ); -- grids table CREATE TABLE IF NOT EXISTS bingo.grids ( id serial UNIQUE PRIMARY KEY, player_id serial references bingo.players(id) ON DELETE CASCADE, lobby_id serial references bingo.lobbys(id) ON DELETE CASCADE ); -- grids_words table CREATE TABLE IF NOT EXISTS bingo.grid_words ( grid_id serial references bingo.grids(id) ON DELETE CASCADE, word_id serial references bingo.words(id) ON DELETE CASCADE, grid_row integer NOT NULL, grid_column integer NOT NULL, PRIMARY KEY (grid_id, word_id) ); -- messages table CREATE TABLE IF NOT EXISTS bingo.messages ( id serial UNIQUE PRIMARY KEY, content varchar(255) NOT NULL, player_id serial references bingo.players(id) ON DELETE SET NULL, lobby_id serial references bingo.lobbys(id) ON DELETE CASCADE, type varchar(8), created timestamp DEFAULT NOW() ); -- rounds table CREATE TABLE IF NOT EXISTS bingo.rounds ( id serial UNIQUE PRIMARY KEY, start timestamp DEFAULT NOW(), finish timestamp, status varchar(8) DEFAULT 'undefined', lobby_id serial references bingo.lobbys(id) ON DELETE CASCADE, winner serial references bingo.players(id) ON DELETE SET NULL ); -- altering ALTER TABLE bingo.lobbys ADD COLUMN IF NOT EXISTS current_round serial references bingo.rounds(id) ON DELETE SET NULL;