migrate code_bundles to bot_versions

This commit is contained in:
Ilion Beyst 2022-07-05 20:34:20 +02:00
parent 8a47b948eb
commit b3df5c6f8c
9 changed files with 42 additions and 43 deletions

View file

@ -1,7 +1,7 @@
use diesel::prelude::*; use diesel::prelude::*;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use crate::schema::{bots, code_bundles}; use crate::schema::{bot_versions, bots};
use chrono; use chrono;
#[derive(Insertable)] #[derive(Insertable)]
@ -44,38 +44,39 @@ pub fn find_all_bots(conn: &PgConnection) -> QueryResult<Vec<Bot>> {
} }
#[derive(Insertable)] #[derive(Insertable)]
#[table_name = "code_bundles"] #[table_name = "bot_versions"]
pub struct NewCodeBundle<'a> { pub struct NewCodeBundle<'a> {
pub bot_id: Option<i32>, pub bot_id: Option<i32>,
pub path: &'a str, pub code_bundle_path: &'a str,
} }
#[derive(Queryable, Serialize, Deserialize, Debug)] #[derive(Queryable, Serialize, Deserialize, Debug)]
pub struct CodeBundle { pub struct CodeBundle {
pub id: i32, pub id: i32,
pub bot_id: Option<i32>, pub bot_id: Option<i32>,
pub path: String, pub code_bundle_path: Option<String>,
pub created_at: chrono::NaiveDateTime, pub created_at: chrono::NaiveDateTime,
pub container_digest: Option<String>,
} }
pub fn create_code_bundle( pub fn create_code_bundle(
new_code_bundle: &NewCodeBundle, new_code_bundle: &NewCodeBundle,
conn: &PgConnection, conn: &PgConnection,
) -> QueryResult<CodeBundle> { ) -> QueryResult<CodeBundle> {
diesel::insert_into(code_bundles::table) diesel::insert_into(bot_versions::table)
.values(new_code_bundle) .values(new_code_bundle)
.get_result(conn) .get_result(conn)
} }
pub fn find_bot_code_bundles(bot_id: i32, conn: &PgConnection) -> QueryResult<Vec<CodeBundle>> { pub fn find_bot_code_bundles(bot_id: i32, conn: &PgConnection) -> QueryResult<Vec<CodeBundle>> {
code_bundles::table bot_versions::table
.filter(code_bundles::bot_id.eq(bot_id)) .filter(bot_versions::bot_id.eq(bot_id))
.get_results(conn) .get_results(conn)
} }
pub fn active_code_bundle(bot_id: i32, conn: &PgConnection) -> QueryResult<CodeBundle> { pub fn active_code_bundle(bot_id: i32, conn: &PgConnection) -> QueryResult<CodeBundle> {
code_bundles::table bot_versions::table
.filter(code_bundles::bot_id.eq(bot_id)) .filter(bot_versions::bot_id.eq(bot_id))
.order(code_bundles::created_at.desc()) .order(bot_versions::created_at.desc())
.first(conn) .first(conn)
} }

View file

@ -6,7 +6,7 @@ use diesel::{
}; };
use diesel::{Connection, GroupedBy, PgConnection, QueryResult}; 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}; 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) let match_players = MatchPlayer::belonging_to(&matches)
.left_join( .left_join(
code_bundles::table bot_versions::table
.on(match_players::code_bundle_id.eq(code_bundles::id.nullable())), .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)? .load::<FullMatchPlayerData>(conn)?
.grouped_by(&matches); .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) let match_players = MatchPlayer::belonging_to(&match_base)
.left_join( .left_join(
code_bundles::table bot_versions::table
.on(match_players::code_bundle_id.eq(code_bundles::id.nullable())), .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)?; .load::<FullMatchPlayerData>(conn)?;
let res = FullMatchData { let res = FullMatchData {

View file

@ -83,10 +83,7 @@ pub fn api() -> Router {
"/bots/:bot_id/upload", "/bots/:bot_id/upload",
post(routes::bots::upload_code_multipart), post(routes::bots::upload_code_multipart),
) )
.route( .route("/matches", get(routes::matches::list_matches))
"/matches",
get(routes::matches::list_matches),
)
.route("/matches/:match_id", get(routes::matches::get_match_data)) .route("/matches/:match_id", get(routes::matches::get_match_data))
.route( .route(
"/matches/:match_id/log", "/matches/:match_id/log",

View file

@ -17,7 +17,7 @@ pub fn save_code_bundle(
let new_code_bundle = db::bots::NewCodeBundle { let new_code_bundle = db::bots::NewCodeBundle {
bot_id, bot_id,
path: &bundle_name, code_bundle_path: &bundle_name,
}; };
db::bots::create_code_bundle(&new_code_bundle, conn) db::bots::create_code_bundle(&new_code_bundle, conn)
} }

View file

@ -98,7 +98,8 @@ impl RunMatch {
} }
pub fn code_bundle_to_botspec(code_bundle: &db::bots::CodeBundle) -> Box<dyn BotSpec> { 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 { Box::new(DockerBotSpec {
code_path: bundle_path, 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"); db::matches::save_match_result(match_id, result, &conn).expect("could not save match result");
return outcome; outcome
} }

View file

@ -1,8 +1,8 @@
use crate::{db::bots::Bot, DbPool}; use crate::{db::bots::Bot, DbPool};
use crate::db; use crate::db;
use diesel::{PgConnection, QueryResult};
use crate::modules::matches::{MatchPlayer, RunMatch}; use crate::modules::matches::{MatchPlayer, RunMatch};
use diesel::{PgConnection, QueryResult};
use rand::seq::SliceRandom; use rand::seq::SliceRandom;
use std::collections::HashMap; use std::collections::HashMap;
use std::mem; use std::mem;

View file

@ -215,7 +215,7 @@ pub async fn upload_code_multipart(
let bundle = bots::NewCodeBundle { let bundle = bots::NewCodeBundle {
bot_id: Some(bot.id), bot_id: Some(bot.id),
path: &folder_name, code_bundle_path: &folder_name,
}; };
let code_bundle = let code_bundle =
bots::create_code_bundle(&bundle, &conn).expect("Failed to create code bundle"); bots::create_code_bundle(&bundle, &conn).expect("Failed to create code bundle");

View file

@ -1,7 +1,7 @@
use std::path::PathBuf;
use axum::{extract::Path, Json}; use axum::{extract::Path, Json};
use hyper::StatusCode; use hyper::StatusCode;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use crate::{ use crate::{
db::matches::{self, MatchState}, db::matches::{self, MatchState},
@ -46,7 +46,6 @@ pub fn match_data_to_api(data: matches::FullMatchData) -> ApiMatch {
} }
} }
pub async fn get_match_data( pub async fn get_match_data(
Path(match_id): Path<i32>, Path(match_id): Path<i32>,
conn: DatabaseConnection, conn: DatabaseConnection,

View file

@ -1,6 +1,19 @@
// This file is autogenerated by diesel // This file is autogenerated by diesel
#![allow(unused_imports)] #![allow(unused_imports)]
table! {
use diesel::sql_types::*;
use crate::db_types::*;
bot_versions (id) {
id -> Int4,
bot_id -> Nullable<Int4>,
code_bundle_path -> Nullable<Text>,
created_at -> Timestamp,
container_digest -> Nullable<Text>,
}
}
table! { table! {
use diesel::sql_types::*; use diesel::sql_types::*;
use crate::db_types::*; use crate::db_types::*;
@ -12,18 +25,6 @@ table! {
} }
} }
table! {
use diesel::sql_types::*;
use crate::db_types::*;
code_bundles (id) {
id -> Int4,
bot_id -> Nullable<Int4>,
path -> Text,
created_at -> Timestamp,
}
}
table! { table! {
use diesel::sql_types::*; use diesel::sql_types::*;
use crate::db_types::*; use crate::db_types::*;
@ -81,16 +82,16 @@ table! {
} }
} }
joinable!(bot_versions -> bots (bot_id));
joinable!(bots -> users (owner_id)); joinable!(bots -> users (owner_id));
joinable!(code_bundles -> bots (bot_id)); joinable!(match_players -> bot_versions (code_bundle_id));
joinable!(match_players -> code_bundles (code_bundle_id));
joinable!(match_players -> matches (match_id)); joinable!(match_players -> matches (match_id));
joinable!(ratings -> bots (bot_id)); joinable!(ratings -> bots (bot_id));
joinable!(sessions -> users (user_id)); joinable!(sessions -> users (user_id));
allow_tables_to_appear_in_same_query!( allow_tables_to_appear_in_same_query!(
bot_versions,
bots, bots,
code_bundles,
match_players, match_players,
matches, matches,
ratings, ratings,