From aa066ef5bb9ed043feafd0e87e219cb34cce35c5 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Tue, 23 Aug 2022 20:00:21 +0200 Subject: [PATCH] create maps table --- .../migrations/2022-08-23-174628_maps/down.sql | 3 +++ .../migrations/2022-08-23-174628_maps/up.sql | 7 +++++++ planetwars-server/src/db/matches.rs | 10 ++-------- planetwars-server/src/routes/matches.rs | 7 ++----- planetwars-server/src/schema.rs | 14 ++++++++++++++ 5 files changed, 28 insertions(+), 13 deletions(-) create mode 100644 planetwars-server/migrations/2022-08-23-174628_maps/down.sql create mode 100644 planetwars-server/migrations/2022-08-23-174628_maps/up.sql diff --git a/planetwars-server/migrations/2022-08-23-174628_maps/down.sql b/planetwars-server/migrations/2022-08-23-174628_maps/down.sql new file mode 100644 index 0000000..7489122 --- /dev/null +++ b/planetwars-server/migrations/2022-08-23-174628_maps/down.sql @@ -0,0 +1,3 @@ +ALTER TABLE matches DROP COLUMN map_id; + +DROP TABLE maps; \ No newline at end of file diff --git a/planetwars-server/migrations/2022-08-23-174628_maps/up.sql b/planetwars-server/migrations/2022-08-23-174628_maps/up.sql new file mode 100644 index 0000000..c66fc9f --- /dev/null +++ b/planetwars-server/migrations/2022-08-23-174628_maps/up.sql @@ -0,0 +1,7 @@ +CREATE TABLE maps ( + id SERIAL PRIMARY KEY, + name TEXT UNIQUE NOT NULL, + file_path TEXT NOT NULL +); + +ALTER TABLE matches ADD COLUMN map_id INTEGER REFERENCES maps(id); \ No newline at end of file diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 9f096df..77d39ef 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -41,6 +41,7 @@ pub struct MatchBase { pub created_at: NaiveDateTime, pub winner: Option, pub is_public: bool, + pub map_id: Option, } #[derive(Queryable, Identifiable, Associations, Clone)] @@ -166,14 +167,7 @@ pub fn list_bot_matches( bot_versions::table.on(match_players::bot_version_id.eq(bot_versions::id.nullable())), ) .filter(bot_versions::bot_id.eq(bot_id)) - .select(( - matches::id, - matches::state, - matches::log_path, - matches::created_at, - matches::winner, - matches::is_public, - )) + .select(matches::all_columns) .into_boxed(); let matches = diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 44bdaf1..71c4409 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -79,11 +79,8 @@ pub async fn list_recent_matches( matches.truncate(requested_count); } - let api_matches = matches - .into_iter() - .map(match_data_to_api) - .collect(); - + let api_matches = matches.into_iter().map(match_data_to_api).collect(); + Ok(Json(ListMatchesResponse { matches: api_matches, has_next, diff --git a/planetwars-server/src/schema.rs b/planetwars-server/src/schema.rs index 70889b1..adc6555 100644 --- a/planetwars-server/src/schema.rs +++ b/planetwars-server/src/schema.rs @@ -26,6 +26,17 @@ table! { } } +table! { + use diesel::sql_types::*; + use crate::db_types::*; + + maps (id) { + id -> Int4, + name -> Text, + file_path -> Text, + } +} + table! { use diesel::sql_types::*; use crate::db_types::*; @@ -48,6 +59,7 @@ table! { created_at -> Timestamp, winner -> Nullable, is_public -> Bool, + map_id -> Nullable, } } @@ -87,12 +99,14 @@ table! { joinable!(bots -> users (owner_id)); joinable!(match_players -> bot_versions (bot_version_id)); joinable!(match_players -> matches (match_id)); +joinable!(matches -> maps (map_id)); joinable!(ratings -> bots (bot_id)); joinable!(sessions -> users (user_id)); allow_tables_to_appear_in_same_query!( bot_versions, bots, + maps, match_players, matches, ratings,