From dab6749c519d53bcff4ded08e59dc45043f5c690 Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 00:27:58 +0200 Subject: [PATCH 01/11] feat: create lobby table --- .../resources/db/migration/V1__initial_migration_schema.sql | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 src/main/resources/db/migration/V1__initial_migration_schema.sql diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql new file mode 100644 index 00000000..ff273513 --- /dev/null +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -0,0 +1,6 @@ +CREATE TABLE lobby +( + lobby_code VARCHAR(255) PRIMARY KEY, + status VARCHAR(255) NOT NULL, + created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL +); From 5c744fc53991d61afb86f4e46b51defa6e1e3a9e Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 01:05:39 +0200 Subject: [PATCH 02/11] feat: add table for player We need to decide as a team if duplicates are allowed or not. For now we leave the constraint as is, can be deleted in t he future --- .../V1__initial_migration_schema.sql | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index ff273513..8cee939b 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -1,6 +1,25 @@ +-- Name --> Type --> Default --> Nullability --> Constraint + CREATE TABLE lobby ( lobby_code VARCHAR(255) PRIMARY KEY, status VARCHAR(255) NOT NULL, created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL ); + +CREATE TABLE player +( + id SERIAL PRIMARY KEY, + lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, + username VARCHAR(255) NOT NULL, + is_host BOOLEAN DEFAULT FALSE NOT NULL, + team VARCHAR(255) NOT NULL, + role VARCHAR(255) NOT NULL, + + CONSTRAINT check_player_team + CHECK (team IN ('RED', 'BLUE')), + CONSTRAINT check_player_role + CHECK (role IN ('OPERATIVE', 'SPYMASTER')), + CONSTRAINT unique_player_per_lobby -- ask team if username in lobby has to be unique or not + UNIQUE (lobby_code, username) +); From 522038610bbb10ba37a347a5805b1a7f62d4ff4d Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 01:29:25 +0200 Subject: [PATCH 03/11] feat: add table for game state --- .../migration/V1__initial_migration_schema.sql | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index 8cee939b..c0291150 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -23,3 +23,21 @@ CREATE TABLE player CONSTRAINT unique_player_per_lobby -- ask team if username in lobby has to be unique or not UNIQUE (lobby_code, username) ); + +CREATE TABLE game_state +( + id SERIAL PRIMARY KEY, + lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, + current_turn VARCHAR(255) NOT NULL, + current_phase VARCHAR(255) NOT NULL, + clue_word VARCHAR(255), + clue_guess_amount INT DEFAULT 0, + remaining_guesses INT DEFAULT 0, + + CONSTRAINT check_game_turn + CHECK (current_turn IN ('RED', 'BLUE')), + CONSTRAINT check_game_phase + CHECK (current_phase IN ('SPYMASTER', 'OPERATIVE')), + CONSTRAINT unique_game_per_lobby + UNIQUE (lobby_code) +); \ No newline at end of file From 63f0927e9d71ff64f379c15faa646f9f2c36cd86 Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 01:40:51 +0200 Subject: [PATCH 04/11] refactor: use BIG instead of normal data types Turns out big variant is not as expensive as expected. As most systems run on 64 bit processors, it might have performance increase. Additionally (source reddit) --- .../db/migration/V1__initial_migration_schema.sql | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index c0291150..8ac2e9d8 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -2,14 +2,14 @@ CREATE TABLE lobby ( - lobby_code VARCHAR(255) PRIMARY KEY, - status VARCHAR(255) NOT NULL, - created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL + lobby_code VARCHAR(255) PRIMARY KEY, + status VARCHAR(255) NOT NULL, + created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL -- plan is to use this to delete stale lobbies ); CREATE TABLE player ( - id SERIAL PRIMARY KEY, + id BIGSERIAL PRIMARY KEY, lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, username VARCHAR(255) NOT NULL, is_host BOOLEAN DEFAULT FALSE NOT NULL, @@ -24,9 +24,10 @@ CREATE TABLE player UNIQUE (lobby_code, username) ); + CREATE TABLE game_state ( - id SERIAL PRIMARY KEY, + id BIGSERIAL PRIMARY KEY, lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, current_turn VARCHAR(255) NOT NULL, current_phase VARCHAR(255) NOT NULL, From 5cb88b2349aa2ed560dc074f0e2fdd0d342b5cd9 Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 01:43:12 +0200 Subject: [PATCH 05/11] refactor: update how primary keys are created to modern standards (source CrunchyData) --- .../resources/db/migration/V1__initial_migration_schema.sql | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index 8ac2e9d8..08cc997b 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -9,7 +9,7 @@ CREATE TABLE lobby CREATE TABLE player ( - id BIGSERIAL PRIMARY KEY, + id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, username VARCHAR(255) NOT NULL, is_host BOOLEAN DEFAULT FALSE NOT NULL, @@ -27,7 +27,7 @@ CREATE TABLE player CREATE TABLE game_state ( - id BIGSERIAL PRIMARY KEY, + id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, current_turn VARCHAR(255) NOT NULL, current_phase VARCHAR(255) NOT NULL, From d981b20fa379a59d29cb9331caced4c690a07702 Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 01:57:56 +0200 Subject: [PATCH 06/11] feat: add table for cards Note: each game will have 25 entries in the card table --- .../V1__initial_migration_schema.sql | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index 08cc997b..150d8ff5 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -41,4 +41,23 @@ CREATE TABLE game_state CHECK (current_phase IN ('SPYMASTER', 'OPERATIVE')), CONSTRAINT unique_game_per_lobby UNIQUE (lobby_code) -); \ No newline at end of file +); + +-- each lobby will have 25 entries in the card table +CREATE TABLE card +( + id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, + position INT NOT NULL, + word VARCHAR(255) NOT NULL, + color VARCHAR(255) NOT NULL, + is_guessed BOOLEAN DEFAULT FALSE NOT NULL, + + CONSTRAINT check_card_position + CHECK (position BETWEEN 0 AND 24), + CONSTRAINT check_card_color + CHECK (color IN ('RED', 'BLUE', 'NEUTRAL', 'ASSASSIN')), + CONSTRAINT unique_card_position_per_game + UNIQUE (lobby_code, position) +); + From ec81c0b38d4b28b073143c3ee8012e54a08b5d04 Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 02:20:44 +0200 Subject: [PATCH 07/11] feat: add table for chat Need to discuss with team if we want to store chat in the first place. If yes, we need to consider if we want to store time stamp or not --- .../db/migration/V1__initial_migration_schema.sql | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index 150d8ff5..d26e74be 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -61,3 +61,15 @@ CREATE TABLE card UNIQUE (lobby_code, position) ); +-- debatable if we even need this, will need to ask team +-- room_key is what ChatHistory uses as the map key (e.g. "lobby", "TEAM_RED", "OPERATIVE_BLUE"). +CREATE TABLE chat_message +( + id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, + lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, + room_key VARCHAR(255) NOT NULL, + sender_username VARCHAR(255) NOT NULL, + content TEXT NOT NULL, + message_type VARCHAR(255) NOT NULL, + sent_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL, -- iirc, frontend plans on saving this?? backend might need to be refactored to store it as well if we decide to have chat logs in DB for retrieval... we would likely need to store time if we want frontend to display correct time +); \ No newline at end of file From 8d3dde39940f8d850d909c0648e1bd2b2388b0b1 Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 02:32:02 +0200 Subject: [PATCH 08/11] refactor: change PK of game_state table Since it is a 1:1 relation, the id is unnecessary and lobby_code is sufficient --- .../db/migration/V1__initial_migration_schema.sql | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index d26e74be..18027cb9 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -27,8 +27,7 @@ CREATE TABLE player CREATE TABLE game_state ( - id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, - lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, + lobby_code VARCHAR(255) PRIMARY KEY REFERENCES lobby (lobby_code) ON DELETE CASCADE, current_turn VARCHAR(255) NOT NULL, current_phase VARCHAR(255) NOT NULL, clue_word VARCHAR(255), @@ -39,8 +38,6 @@ CREATE TABLE game_state CHECK (current_turn IN ('RED', 'BLUE')), CONSTRAINT check_game_phase CHECK (current_phase IN ('SPYMASTER', 'OPERATIVE')), - CONSTRAINT unique_game_per_lobby - UNIQUE (lobby_code) ); -- each lobby will have 25 entries in the card table @@ -71,5 +68,5 @@ CREATE TABLE chat_message sender_username VARCHAR(255) NOT NULL, content TEXT NOT NULL, message_type VARCHAR(255) NOT NULL, - sent_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL, -- iirc, frontend plans on saving this?? backend might need to be refactored to store it as well if we decide to have chat logs in DB for retrieval... we would likely need to store time if we want frontend to display correct time + sent_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() -- iirc, frontend plans on saving this?? backend might need to be refactored to store it as well if we decide to have chat logs in DB for retrieval... we would likely need to store time if we want frontend to display correct time ); \ No newline at end of file From 844eb8142f9b3562e2df9c9c947cdca8fd926ceb Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 02:45:50 +0200 Subject: [PATCH 09/11] fix: delete stray comma --- .../resources/db/migration/V1__initial_migration_schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index 18027cb9..190d90cf 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -37,7 +37,7 @@ CREATE TABLE game_state CONSTRAINT check_game_turn CHECK (current_turn IN ('RED', 'BLUE')), CONSTRAINT check_game_phase - CHECK (current_phase IN ('SPYMASTER', 'OPERATIVE')), + CHECK (current_phase IN ('SPYMASTER', 'OPERATIVE')) ); -- each lobby will have 25 entries in the card table From a3b18d62ebdc03ff797bad0f1fc7774c5c5a9f27 Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 15:31:19 +0200 Subject: [PATCH 10/11] refactor: change length of varchar for DB entries While the diskspace usage stays the same for all values sub 255, the searching might suffer from using large sizes. Also security concerns/ malicious inputs/ wrong inputs can be minimized by not allowing a standard large varchar size --- .../V1__initial_migration_schema.sql | 33 +++++++++---------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index 190d90cf..423a2a44 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -2,19 +2,18 @@ CREATE TABLE lobby ( - lobby_code VARCHAR(255) PRIMARY KEY, - status VARCHAR(255) NOT NULL, + lobby_code VARCHAR(5) PRIMARY KEY, created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() NOT NULL -- plan is to use this to delete stale lobbies ); CREATE TABLE player ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, - lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, - username VARCHAR(255) NOT NULL, + lobby_code VARCHAR(5) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, + username VARCHAR(20) NOT NULL, is_host BOOLEAN DEFAULT FALSE NOT NULL, - team VARCHAR(255) NOT NULL, - role VARCHAR(255) NOT NULL, + team VARCHAR(4) NOT NULL, + role VARCHAR(9) NOT NULL, CONSTRAINT check_player_team CHECK (team IN ('RED', 'BLUE')), @@ -27,10 +26,10 @@ CREATE TABLE player CREATE TABLE game_state ( - lobby_code VARCHAR(255) PRIMARY KEY REFERENCES lobby (lobby_code) ON DELETE CASCADE, - current_turn VARCHAR(255) NOT NULL, - current_phase VARCHAR(255) NOT NULL, - clue_word VARCHAR(255), + lobby_code VARCHAR(5) PRIMARY KEY REFERENCES lobby (lobby_code) ON DELETE CASCADE, + current_turn VARCHAR(4) NOT NULL, + current_phase VARCHAR(9) NOT NULL, + clue_word VARCHAR(20), clue_guess_amount INT DEFAULT 0, remaining_guesses INT DEFAULT 0, @@ -44,10 +43,10 @@ CREATE TABLE game_state CREATE TABLE card ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, - lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, + lobby_code VARCHAR(5) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, position INT NOT NULL, - word VARCHAR(255) NOT NULL, - color VARCHAR(255) NOT NULL, + word VARCHAR(11) NOT NULL, + color VARCHAR(8) NOT NULL, is_guessed BOOLEAN DEFAULT FALSE NOT NULL, CONSTRAINT check_card_position @@ -63,10 +62,10 @@ CREATE TABLE card CREATE TABLE chat_message ( id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, - lobby_code VARCHAR(255) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, - room_key VARCHAR(255) NOT NULL, - sender_username VARCHAR(255) NOT NULL, + lobby_code VARCHAR(5) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, + room_key VARCHAR(20) NOT NULL, + sender_username VARCHAR(20) NOT NULL, content TEXT NOT NULL, - message_type VARCHAR(255) NOT NULL, + message_type VARCHAR(10) NOT NULL, sent_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() -- iirc, frontend plans on saving this?? backend might need to be refactored to store it as well if we decide to have chat logs in DB for retrieval... we would likely need to store time if we want frontend to display correct time ); \ No newline at end of file From 4b8a45a6bf48af0805b8ef5da384e6137a5620ff Mon Sep 17 00:00:00 2001 From: XtophB Date: Tue, 2 Jun 2026 20:55:00 +0200 Subject: [PATCH 11/11] chore: remove chat table, team decision --- .../db/migration/V1__initial_migration_schema.sql | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/main/resources/db/migration/V1__initial_migration_schema.sql b/src/main/resources/db/migration/V1__initial_migration_schema.sql index 423a2a44..44e624c9 100644 --- a/src/main/resources/db/migration/V1__initial_migration_schema.sql +++ b/src/main/resources/db/migration/V1__initial_migration_schema.sql @@ -55,17 +55,4 @@ CREATE TABLE card CHECK (color IN ('RED', 'BLUE', 'NEUTRAL', 'ASSASSIN')), CONSTRAINT unique_card_position_per_game UNIQUE (lobby_code, position) -); - --- debatable if we even need this, will need to ask team --- room_key is what ChatHistory uses as the map key (e.g. "lobby", "TEAM_RED", "OPERATIVE_BLUE"). -CREATE TABLE chat_message -( - id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY, - lobby_code VARCHAR(5) NOT NULL REFERENCES lobby (lobby_code) ON DELETE CASCADE, - room_key VARCHAR(20) NOT NULL, - sender_username VARCHAR(20) NOT NULL, - content TEXT NOT NULL, - message_type VARCHAR(10) NOT NULL, - sent_at TIMESTAMP WITHOUT TIME ZONE DEFAULT NOW() -- iirc, frontend plans on saving this?? backend might need to be refactored to store it as well if we decide to have chat logs in DB for retrieval... we would likely need to store time if we want frontend to display correct time ); \ No newline at end of file