diff options
author | Ilion Beyst <ilion.beyst@gmail.com> | 2022-07-05 20:34:20 +0200 |
---|---|---|
committer | Ilion Beyst <ilion.beyst@gmail.com> | 2022-07-05 20:34:20 +0200 |
commit | b3df5c6f8cc59e099a2f1db3df8089af4abca02e (patch) | |
tree | 8b717834ab72165610b1548bb080a3299d936936 /planetwars-server | |
parent | 8a47b948eb88f1e0ff649880a70f9416306bac92 (diff) | |
download | planetwars.dev-b3df5c6f8cc59e099a2f1db3df8089af4abca02e.tar.xz planetwars.dev-b3df5c6f8cc59e099a2f1db3df8089af4abca02e.zip |
migrate code_bundles to bot_versions
Diffstat (limited to 'planetwars-server')
-rw-r--r-- | planetwars-server/src/db/bots.rs | 21 | ||||
-rw-r--r-- | planetwars-server/src/db/matches.rs | 14 | ||||
-rw-r--r-- | planetwars-server/src/lib.rs | 5 | ||||
-rw-r--r-- | planetwars-server/src/modules/bots.rs | 2 | ||||
-rw-r--r-- | planetwars-server/src/modules/matches.rs | 5 | ||||
-rw-r--r-- | planetwars-server/src/modules/ranking.rs | 2 | ||||
-rw-r--r-- | planetwars-server/src/routes/bots.rs | 2 | ||||
-rw-r--r-- | planetwars-server/src/routes/matches.rs | 3 | ||||
-rw-r--r-- | planetwars-server/src/schema.rs | 21 |
9 files changed, 37 insertions, 38 deletions
diff --git a/planetwars-server/src/db/bots.rs b/planetwars-server/src/db/bots.rs index 108c692..964deaa 100644 --- a/planetwars-server/src/db/bots.rs +++ b/planetwars-server/src/db/bots.rs @@ -1,7 +1,7 @@ use diesel::prelude::*; use serde::{Deserialize, Serialize}; -use crate::schema::{bots, code_bundles}; +use crate::schema::{bot_versions, bots}; use chrono; #[derive(Insertable)] @@ -44,38 +44,39 @@ pub fn find_all_bots(conn: &PgConnection) -> QueryResult<Vec<Bot>> { } #[derive(Insertable)] -#[table_name = "code_bundles"] +#[table_name = "bot_versions"] pub struct NewCodeBundle<'a> { pub bot_id: Option<i32>, - pub path: &'a str, + pub code_bundle_path: &'a str, } #[derive(Queryable, Serialize, Deserialize, Debug)] pub struct CodeBundle { pub id: i32, pub bot_id: Option<i32>, - pub path: String, + pub code_bundle_path: Option<String>, pub created_at: chrono::NaiveDateTime, + pub container_digest: Option<String>, } pub fn create_code_bundle( new_code_bundle: &NewCodeBundle, conn: &PgConnection, ) -> QueryResult<CodeBundle> { - diesel::insert_into(code_bundles::table) + 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<Vec<CodeBundle>> { - code_bundles::table - .filter(code_bundles::bot_id.eq(bot_id)) + 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<CodeBundle> { - code_bundles::table - .filter(code_bundles::bot_id.eq(bot_id)) - .order(code_bundles::created_at.desc()) + bot_versions::table + .filter(bot_versions::bot_id.eq(bot_id)) + .order(bot_versions::created_at.desc()) .first(conn) } diff --git a/planetwars-server/src/db/matches.rs b/planetwars-server/src/db/matches.rs index 54fd113..d9d893c 100644 --- a/planetwars-server/src/db/matches.rs +++ b/planetwars-server/src/db/matches.rs @@ -6,7 +6,7 @@ use diesel::{ }; use diesel::{Connection, GroupedBy, PgConnection, QueryResult}; -use crate::schema::{bots, code_bundles, match_players, matches}; +use crate::schema::{bot_versions, bots, match_players, matches}; use super::bots::{Bot, CodeBundle}; @@ -93,10 +93,10 @@ pub fn list_matches(conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> { let match_players = MatchPlayer::belonging_to(&matches) .left_join( - code_bundles::table - .on(match_players::code_bundle_id.eq(code_bundles::id.nullable())), + bot_versions::table + .on(match_players::code_bundle_id.eq(bot_versions::id.nullable())), ) - .left_join(bots::table.on(code_bundles::bot_id.eq(bots::id.nullable()))) + .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) .load::<FullMatchPlayerData>(conn)? .grouped_by(&matches); @@ -146,10 +146,10 @@ pub fn find_match(id: i32, conn: &PgConnection) -> QueryResult<FullMatchData> { let match_players = MatchPlayer::belonging_to(&match_base) .left_join( - code_bundles::table - .on(match_players::code_bundle_id.eq(code_bundles::id.nullable())), + bot_versions::table + .on(match_players::code_bundle_id.eq(bot_versions::id.nullable())), ) - .left_join(bots::table.on(code_bundles::bot_id.eq(bots::id.nullable()))) + .left_join(bots::table.on(bot_versions::bot_id.eq(bots::id.nullable()))) .load::<FullMatchPlayerData>(conn)?; let res = FullMatchData { diff --git a/planetwars-server/src/lib.rs b/planetwars-server/src/lib.rs index 8798945..7076604 100644 --- a/planetwars-server/src/lib.rs +++ b/planetwars-server/src/lib.rs @@ -83,10 +83,7 @@ pub fn api() -> Router { "/bots/:bot_id/upload", post(routes::bots::upload_code_multipart), ) - .route( - "/matches", - get(routes::matches::list_matches), - ) + .route("/matches", get(routes::matches::list_matches)) .route("/matches/:match_id", get(routes::matches::get_match_data)) .route( "/matches/:match_id/log", diff --git a/planetwars-server/src/modules/bots.rs b/planetwars-server/src/modules/bots.rs index 843e48d..ddc1589 100644 --- a/planetwars-server/src/modules/bots.rs +++ b/planetwars-server/src/modules/bots.rs @@ -17,7 +17,7 @@ pub fn save_code_bundle( let new_code_bundle = db::bots::NewCodeBundle { bot_id, - path: &bundle_name, + code_bundle_path: &bundle_name, }; db::bots::create_code_bundle(&new_code_bundle, conn) } diff --git a/planetwars-server/src/modules/matches.rs b/planetwars-server/src/modules/matches.rs index 6d9261d..7d6a1dc 100644 --- a/planetwars-server/src/modules/matches.rs +++ b/planetwars-server/src/modules/matches.rs @@ -98,7 +98,8 @@ impl RunMatch { } pub fn code_bundle_to_botspec(code_bundle: &db::bots::CodeBundle) -> Box<dyn BotSpec> { - let bundle_path = PathBuf::from(BOTS_DIR).join(&code_bundle.path); + // TODO: get rid of this unwrap + let bundle_path = PathBuf::from(BOTS_DIR).join(code_bundle.code_bundle_path.as_ref().unwrap()); Box::new(DockerBotSpec { code_path: bundle_path, @@ -126,5 +127,5 @@ async fn run_match_task( db::matches::save_match_result(match_id, result, &conn).expect("could not save match result"); - return outcome; + outcome } diff --git a/planetwars-server/src/modules/ranking.rs b/planetwars-server/src/modules/ranking.rs index 72156ee..b1ad0da 100644 --- a/planetwars-server/src/modules/ranking.rs +++ b/planetwars-server/src/modules/ranking.rs @@ -1,8 +1,8 @@ use crate::{db::bots::Bot, DbPool}; use crate::db; -use diesel::{PgConnection, QueryResult}; use crate::modules::matches::{MatchPlayer, RunMatch}; +use diesel::{PgConnection, QueryResult}; use rand::seq::SliceRandom; use std::collections::HashMap; use std::mem; diff --git a/planetwars-server/src/routes/bots.rs b/planetwars-server/src/routes/bots.rs index df0c4d0..6d5d7df 100644 --- a/planetwars-server/src/routes/bots.rs +++ b/planetwars-server/src/routes/bots.rs @@ -215,7 +215,7 @@ pub async fn upload_code_multipart( let bundle = bots::NewCodeBundle { bot_id: Some(bot.id), - path: &folder_name, + code_bundle_path: &folder_name, }; let code_bundle = bots::create_code_bundle(&bundle, &conn).expect("Failed to create code bundle"); diff --git a/planetwars-server/src/routes/matches.rs b/planetwars-server/src/routes/matches.rs index 5f95ce9..0c1bee4 100644 --- a/planetwars-server/src/routes/matches.rs +++ b/planetwars-server/src/routes/matches.rs @@ -1,7 +1,7 @@ -use std::path::PathBuf; use axum::{extract::Path, Json}; use hyper::StatusCode; use serde::{Deserialize, Serialize}; +use std::path::PathBuf; use crate::{ db::matches::{self, MatchState}, @@ -46,7 +46,6 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch { } } - pub async fn get_match_data( Path(match_id): Path<i32>, conn: DatabaseConnection, diff --git a/planetwars-server/src/schema.rs b/planetwars-server/src/schema.rs index 92acc8e..d632a32 100644 --- a/planetwars-server/src/schema.rs +++ b/planetwars-server/src/schema.rs @@ -5,10 +5,12 @@ table! { use diesel::sql_types::*; use crate::db_types::*; - bots (id) { + bot_versions (id) { id -> Int4, - owner_id -> Nullable<Int4>, - name -> Text, + bot_id -> Nullable<Int4>, + code_bundle_path -> Nullable<Text>, + created_at -> Timestamp, + container_digest -> Nullable<Text>, } } @@ -16,11 +18,10 @@ table! { use diesel::sql_types::*; use crate::db_types::*; - code_bundles (id) { + bots (id) { id -> Int4, - bot_id -> Nullable<Int4>, - path -> Text, - created_at -> Timestamp, + owner_id -> Nullable<Int4>, + name -> Text, } } @@ -81,16 +82,16 @@ table! { } } +joinable!(bot_versions -> bots (bot_id)); joinable!(bots -> users (owner_id)); -joinable!(code_bundles -> bots (bot_id)); -joinable!(match_players -> code_bundles (code_bundle_id)); +joinable!(match_players -> bot_versions (code_bundle_id)); joinable!(match_players -> matches (match_id)); joinable!(ratings -> bots (bot_id)); joinable!(sessions -> users (user_id)); allow_tables_to_appear_in_same_query!( + bot_versions, bots, - code_bundles, match_players, matches, ratings, |