From d7b7585dd70f9d41184cf88c2ecbd88341898c38 Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Wed, 6 Jul 2022 22:41:27 +0200 Subject: [PATCH] rename code_bundle to bot_version --- .../down.sql | 4 +++- .../up.sql | 2 ++ planetwars-server/src/db/bots.rs | 8 ++++---- planetwars-server/src/db/matches.rs | 12 ++++++------ planetwars-server/src/modules/bot_api.rs | 2 +- planetwars-server/src/modules/bots.rs | 2 +- planetwars-server/src/modules/matches.rs | 4 ++-- planetwars-server/src/modules/ranking.rs | 2 +- planetwars-server/src/routes/bots.rs | 8 ++++---- planetwars-server/src/routes/demo.rs | 14 +++++++------- planetwars-server/src/routes/matches.rs | 4 ++-- planetwars-server/src/schema.rs | 4 ++-- 12 files changed, 35 insertions(+), 31 deletions(-) diff --git a/planetwars-server/migrations/2022-07-04-200149_code_bundle_to_bot_version/down.sql b/planetwars-server/migrations/2022-07-04-200149_code_bundle_to_bot_version/down.sql index bc2d7d6..89058fe 100644 --- a/planetwars-server/migrations/2022-07-04-200149_code_bundle_to_bot_version/down.sql +++ b/planetwars-server/migrations/2022-07-04-200149_code_bundle_to_bot_version/down.sql @@ -1,4 +1,6 @@ +ALTER TABLE match_players RENAME COLUMN bot_version_id TO code_bundle_id; + ALTER TABLE bot_versions DROP COLUMN container_digest; -ALTER TABLE bot_versions RENAME COLUMN code_bundle_path to path; +ALTER TABLE bot_versions RENAME COLUMN code_bundle_path TO path; ALTER TABLE bot_versions ALTER COLUMN path SET NOT NULL; ALTER TABLE bot_versions RENAME TO code_bundles; diff --git a/planetwars-server/migrations/2022-07-04-200149_code_bundle_to_bot_version/up.sql b/planetwars-server/migrations/2022-07-04-200149_code_bundle_to_bot_version/up.sql index 0b28df7..91afc0c 100644 --- a/planetwars-server/migrations/2022-07-04-200149_code_bundle_to_bot_version/up.sql +++ b/planetwars-server/migrations/2022-07-04-200149_code_bundle_to_bot_version/up.sql @@ -2,3 +2,5 @@ ALTER TABLE code_bundles RENAME TO bot_versions; ALTER TABLE bot_versions RENAME COLUMN path to code_bundle_path; ALTER TABLE bot_versions ALTER COLUMN code_bundle_path DROP NOT NULL; ALTER TABLE bot_versions ADD COLUMN container_digest TEXT; + +ALTER TABLE match_players RENAME COLUMN code_bundle_id TO bot_version_id; diff --git a/planetwars-server/src/db/bots.rs b/planetwars-server/src/db/bots.rs index 964deaa..53c11b1 100644 --- a/planetwars-server/src/db/bots.rs +++ b/planetwars-server/src/db/bots.rs @@ -51,7 +51,7 @@ pub struct NewCodeBundle<'a> { } #[derive(Queryable, Serialize, Deserialize, Debug)] -pub struct CodeBundle { +pub struct BotVersion { pub id: i32, pub bot_id: Option, pub code_bundle_path: Option, @@ -62,19 +62,19 @@ pub struct CodeBundle { pub fn create_code_bundle( new_code_bundle: &NewCodeBundle, conn: &PgConnection, -) -> QueryResult { +) -> QueryResult { diesel::insert_into(bot_versions::table) .values(new_code_bundle) .get_result(conn) } -pub fn find_bot_code_bundles(bot_id: i32, conn: &PgConnection) -> QueryResult> { +pub fn find_bot_versions(bot_id: i32, conn: &PgConnection) -> QueryResult> { bot_versions::table .filter(bot_versions::bot_id.eq(bot_id)) .get_results(conn) } -pub fn active_code_bundle(bot_id: i32, conn: &PgConnection) -> QueryResult { +pub fn active_bot_version(bot_id: i32, conn: &PgConnection) -> QueryResult { bot_versions::table .filter(bot_versions::bot_id.eq(bot_id)) .order(bot_versions::created_at.desc()) diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index d9d893c..6590a37 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -8,7 +8,7 @@ use diesel::{Connection, GroupedBy, PgConnection, QueryResult}; use crate::schema::{bot_versions, bots, match_players, matches}; -use super::bots::{Bot, CodeBundle}; +use super::bots::{Bot, BotVersion}; #[derive(Insertable)] #[table_name = "matches"] @@ -25,7 +25,7 @@ pub struct NewMatchPlayer { /// player id within the match pub player_id: i32, /// id of the bot behind this player - pub code_bundle_id: Option, + pub bot_version_id: Option, } #[derive(Queryable, Identifiable)] @@ -67,7 +67,7 @@ pub fn create_match( .map(|(num, player_data)| NewMatchPlayer { match_id: match_base.id, player_id: num as i32, - code_bundle_id: player_data.code_bundle_id, + bot_version_id: player_data.code_bundle_id, }) .collect::>(); @@ -94,7 +94,7 @@ pub fn list_matches(conn: &PgConnection) -> QueryResult> { let match_players = MatchPlayer::belonging_to(&matches) .left_join( bot_versions::table - .on(match_players::code_bundle_id.eq(bot_versions::id.nullable())), + .on(match_players::bot_version_id.eq(bot_versions::id.nullable())), ) .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) .load::(conn)? @@ -123,7 +123,7 @@ pub struct FullMatchData { // #[primary_key(base.match_id, base::player_id)] pub struct FullMatchPlayerData { pub base: MatchPlayer, - pub code_bundle: Option, + pub bot_version: Option, pub bot: Option, } @@ -147,7 +147,7 @@ pub fn find_match(id: i32, conn: &PgConnection) -> QueryResult { let match_players = MatchPlayer::belonging_to(&match_base) .left_join( bot_versions::table - .on(match_players::code_bundle_id.eq(bot_versions::id.nullable())), + .on(match_players::bot_version_id.eq(bot_versions::id.nullable())), ) .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) .load::(conn)?; diff --git a/planetwars-server/src/modules/bot_api.rs b/planetwars-server/src/modules/bot_api.rs index 0ecbf71..6324010 100644 --- a/planetwars-server/src/modules/bot_api.rs +++ b/planetwars-server/src/modules/bot_api.rs @@ -104,7 +104,7 @@ impl pb::bot_api_service_server::BotApiService for BotApiServer { let opponent = db::bots::find_bot_by_name(&match_request.opponent_name, &conn) .map_err(|_| Status::not_found("opponent not found"))?; - let opponent_code_bundle = db::bots::active_code_bundle(opponent.id, &conn) + let opponent_code_bundle = db::bots::active_bot_version(opponent.id, &conn) .map_err(|_| Status::not_found("opponent has no code"))?; let player_key = gen_alphanumeric(32); diff --git a/planetwars-server/src/modules/bots.rs b/planetwars-server/src/modules/bots.rs index ddc1589..cd26ee0 100644 --- a/planetwars-server/src/modules/bots.rs +++ b/planetwars-server/src/modules/bots.rs @@ -8,7 +8,7 @@ pub fn save_code_bundle( bot_code: &str, bot_id: Option, conn: &PgConnection, -) -> QueryResult { +) -> QueryResult { let bundle_name = gen_alphanumeric(16); let code_bundle_dir = PathBuf::from(BOTS_DIR).join(&bundle_name); diff --git a/planetwars-server/src/modules/matches.rs b/planetwars-server/src/modules/matches.rs index 7d6a1dc..4a5a980 100644 --- a/planetwars-server/src/modules/matches.rs +++ b/planetwars-server/src/modules/matches.rs @@ -29,7 +29,7 @@ pub struct MatchPlayer { } impl MatchPlayer { - pub fn from_code_bundle(code_bundle: &db::bots::CodeBundle) -> Self { + pub fn from_code_bundle(code_bundle: &db::bots::BotVersion) -> Self { MatchPlayer { bot_spec: code_bundle_to_botspec(code_bundle), code_bundle_id: Some(code_bundle.id), @@ -97,7 +97,7 @@ impl RunMatch { } } -pub fn code_bundle_to_botspec(code_bundle: &db::bots::CodeBundle) -> Box { +pub fn code_bundle_to_botspec(code_bundle: &db::bots::BotVersion) -> Box { // TODO: get rid of this unwrap let bundle_path = PathBuf::from(BOTS_DIR).join(code_bundle.code_bundle_path.as_ref().unwrap()); diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs index b1ad0da..751c35e 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -39,7 +39,7 @@ async fn play_ranking_match(selected_bots: Vec, db_pool: DbPool) { let db_conn = db_pool.get().await.expect("could not get db pool"); let mut code_bundles = Vec::new(); for bot in &selected_bots { - let code_bundle = db::bots::active_code_bundle(bot.id, &db_conn) + let code_bundle = db::bots::active_bot_version(bot.id, &db_conn) .expect("could not get active code bundle"); code_bundles.push(code_bundle); } diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index 6d5d7df..1ffedef 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -11,7 +11,7 @@ use std::io::Cursor; use std::path::PathBuf; use thiserror; -use crate::db::bots::{self, CodeBundle}; +use crate::db::bots::{self, BotVersion}; use crate::db::ratings::{self, RankedBot}; use crate::db::users::User; use crate::modules::bots::save_code_bundle; @@ -148,8 +148,8 @@ pub async fn get_bot( Path(bot_id): Path, ) -> Result, StatusCode> { let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; - let bundles = bots::find_bot_code_bundles(bot.id, &conn) - .map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; + let bundles = + bots::find_bot_versions(bot.id, &conn).map_err(|_| StatusCode::INTERNAL_SERVER_ERROR)?; Ok(Json(json!({ "bot": bot, "bundles": bundles, @@ -183,7 +183,7 @@ pub async fn upload_code_multipart( user: User, Path(bot_id): Path, mut multipart: Multipart, -) -> Result, StatusCode> { +) -> Result, StatusCode> { let bots_dir = PathBuf::from(BOTS_DIR); let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?; diff --git a/planetwars-server/src/routes/demo.rs b/planetwars-server/src/routes/demo.rs index 33dc02d..1747bfe 100644 --- a/planetwars-server/src/routes/demo.rs +++ b/planetwars-server/src/routes/demo.rs @@ -39,16 +39,16 @@ pub async fn submit_bot( let opponent = db::bots::find_bot_by_name(&opponent_name, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; - let opponent_code_bundle = - db::bots::active_code_bundle(opponent.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; + let opponent_bot_version = + db::bots::active_bot_version(opponent.id, &conn).map_err(|_| StatusCode::BAD_REQUEST)?; - let player_code_bundle = save_code_bundle(¶ms.code, None, &conn) + let player_bot_version = save_code_bundle(¶ms.code, None, &conn) // TODO: can we recover from this? .expect("could not save bot code"); let mut run_match = RunMatch::from_players(vec![ - MatchPlayer::from_code_bundle(&player_code_bundle), - MatchPlayer::from_code_bundle(&opponent_code_bundle), + MatchPlayer::from_code_bundle(&player_bot_version), + MatchPlayer::from_code_bundle(&opponent_bot_version), ]); let match_data = run_match .store_in_database(&conn) @@ -61,12 +61,12 @@ pub async fn submit_bot( match_players: vec![ FullMatchPlayerData { base: match_data.match_players[0].clone(), - code_bundle: Some(player_code_bundle), + bot_version: Some(player_bot_version), bot: None, }, FullMatchPlayerData { base: match_data.match_players[1].clone(), - code_bundle: Some(opponent_code_bundle), + bot_version: Some(opponent_bot_version), bot: Some(opponent), }, ], diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 0c1bee4..f33a5f1 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -18,7 +18,7 @@ pub struct ApiMatch { #[derive(Serialize, Deserialize)] pub struct ApiMatchPlayer { - code_bundle_id: Option, + bot_version_id: Option, bot_id: Option, bot_name: Option, } @@ -38,7 +38,7 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch { .match_players .iter() .map(|_p| ApiMatchPlayer { - code_bundle_id: _p.code_bundle.as_ref().map(|cb| cb.id), + bot_version_id: _p.bot_version.as_ref().map(|cb| cb.id), bot_id: _p.bot.as_ref().map(|b| b.id), bot_name: _p.bot.as_ref().map(|b| b.name.clone()), }) diff --git a/planetwars-server/src/schema.rs b/planetwars-server/src/schema.rs index d632a32..0606ac4 100644 --- a/planetwars-server/src/schema.rs +++ b/planetwars-server/src/schema.rs @@ -32,7 +32,7 @@ table! { match_players (match_id, player_id) { match_id -> Int4, player_id -> Int4, - code_bundle_id -> Nullable, + bot_version_id -> Nullable, } } @@ -84,7 +84,7 @@ table! { joinable!(bot_versions -> bots (bot_id)); joinable!(bots -> users (owner_id)); -joinable!(match_players -> bot_versions (code_bundle_id)); +joinable!(match_players -> bot_versions (bot_version_id)); joinable!(match_players -> matches (match_id)); joinable!(ratings -> bots (bot_id)); joinable!(sessions -> users (user_id));