rename code_bundle to bot_version

This commit is contained in:
Ilion Beyst 2022-07-06 22:41:27 +02:00
parent b3df5c6f8c
commit d7b7585dd7
12 changed files with 35 additions and 31 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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<i32>,
pub code_bundle_path: Option<String>,
@ -62,19 +62,19 @@ pub struct CodeBundle {
pub fn create_code_bundle(
new_code_bundle: &NewCodeBundle,
conn: &PgConnection,
) -> QueryResult<CodeBundle> {
) -> QueryResult<BotVersion> {
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>> {
pub fn find_bot_versions(bot_id: i32, conn: &PgConnection) -> QueryResult<Vec<BotVersion>> {
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> {
pub fn active_bot_version(bot_id: i32, conn: &PgConnection) -> QueryResult<BotVersion> {
bot_versions::table
.filter(bot_versions::bot_id.eq(bot_id))
.order(bot_versions::created_at.desc())

View file

@ -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<i32>,
pub bot_version_id: Option<i32>,
}
#[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::<Vec<_>>();
@ -94,7 +94,7 @@ pub fn list_matches(conn: &PgConnection) -> QueryResult<Vec<FullMatchData>> {
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::<FullMatchPlayerData>(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<CodeBundle>,
pub bot_version: Option<BotVersion>,
pub bot: Option<Bot>,
}
@ -147,7 +147,7 @@ pub fn find_match(id: i32, conn: &PgConnection) -> QueryResult<FullMatchData> {
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::<FullMatchPlayerData>(conn)?;

View file

@ -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);

View file

@ -8,7 +8,7 @@ pub fn save_code_bundle(
bot_code: &str,
bot_id: Option<i32>,
conn: &PgConnection,
) -> QueryResult<db::bots::CodeBundle> {
) -> QueryResult<db::bots::BotVersion> {
let bundle_name = gen_alphanumeric(16);
let code_bundle_dir = PathBuf::from(BOTS_DIR).join(&bundle_name);

View file

@ -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<dyn BotSpec> {
pub fn code_bundle_to_botspec(code_bundle: &db::bots::BotVersion) -> Box<dyn BotSpec> {
// TODO: get rid of this unwrap
let bundle_path = PathBuf::from(BOTS_DIR).join(code_bundle.code_bundle_path.as_ref().unwrap());

View file

@ -39,7 +39,7 @@ async fn play_ranking_match(selected_bots: Vec<Bot>, 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);
}

View file

@ -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<i32>,
) -> Result<Json<JsonValue>, 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<i32>,
mut multipart: Multipart,
) -> Result<Json<CodeBundle>, StatusCode> {
) -> Result<Json<BotVersion>, StatusCode> {
let bots_dir = PathBuf::from(BOTS_DIR);
let bot = bots::find_bot(bot_id, &conn).map_err(|_| StatusCode::NOT_FOUND)?;

View file

@ -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(&params.code, None, &conn)
let player_bot_version = save_code_bundle(&params.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),
},
],

View file

@ -18,7 +18,7 @@ pub struct ApiMatch {
#[derive(Serialize, Deserialize)]
pub struct ApiMatchPlayer {
code_bundle_id: Option<i32>,
bot_version_id: Option<i32>,
bot_id: Option<i32>,
bot_name: Option<String>,
}
@ -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()),
})

View file

@ -32,7 +32,7 @@ table! {
match_players (match_id, player_id) {
match_id -> Int4,
player_id -> Int4,
code_bundle_id -> Nullable<Int4>,
bot_version_id -> Nullable<Int4>,
}
}
@ -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));