From ade8c162cdd3308ae30b109e8b4c63a493f9c9af Mon Sep 17 00:00:00 2001 From: Wyatt Verchere Date: Fri, 24 Nov 2023 11:42:06 -0800 Subject: [PATCH] Staging fixes (#766) * fixes bugs * fixed more things * fixes version creation * small change * removed modpack --- ...1700_adds_missing_loader_field_loaders.sql | 45 +++++++++++++++++++ src/routes/v2/projects.rs | 10 ++--- src/routes/v2/version_creation.rs | 35 +++++++++++---- 3 files changed, 75 insertions(+), 15 deletions(-) create mode 100644 migrations/20231122111700_adds_missing_loader_field_loaders.sql diff --git a/migrations/20231122111700_adds_missing_loader_field_loaders.sql b/migrations/20231122111700_adds_missing_loader_field_loaders.sql new file mode 100644 index 00000000..8747a2cb --- /dev/null +++ b/migrations/20231122111700_adds_missing_loader_field_loaders.sql @@ -0,0 +1,45 @@ + +-- Adds missing fields to loader_fields_loaders +INSERT INTO loader_fields_loaders (loader_id, loader_field_id) +SELECT l.id, lf.id FROM loaders l CROSS JOIN loader_fields lf WHERE lf.field = 'game_versions' +AND l.loader = ANY( ARRAY['forge', 'fabric', 'quilt', 'modloader','rift','liteloader', 'neoforge']) +ON CONFLICT (loader_id, loader_field_id) DO NOTHING; + +-- Fixes mrpack variants being added to the wrong enum +-- Luckily, mrpack variants are the only ones set to 2 without metadata +UPDATE loader_field_enum_values SET enum_id = 3 WHERE enum_id = 2 AND metadata IS NULL; + +-- Because it was mislabeled, version_fields for mrpack_loaders were set to null. +-- 1) Update version_fields corresponding to mrpack_loaders to the correct enum_value +UPDATE version_fields vf +SET enum_value = subquery.lfev_id +FROM ( + SELECT vf.version_id, vf.field_id, lfev.id AS lfev_id + FROM version_fields vf + LEFT JOIN versions v ON v.id = vf.version_id + LEFT JOIN loaders_versions lv ON v.id = lv.version_id + LEFT JOIN loaders l ON l.id = lv.loader_id + LEFT JOIN loader_fields lf ON lf.id = vf.field_id + LEFT JOIN loader_field_enum_values lfev ON lfev.value = l.loader AND lf.enum_type = lfev.enum_id + WHERE lf.field = 'mrpack_loaders' AND vf.enum_value IS NULL +) AS subquery +WHERE vf.version_id = subquery.version_id AND vf.field_id = subquery.field_id; + +-- 2) Set those versions to mrpack as their version +INSERT INTO loaders_versions (version_id, loader_id) +SELECT DISTINCT vf.version_id, l.id +FROM version_fields vf +LEFT JOIN loader_fields lf ON lf.id = vf.field_id +CROSS JOIN loaders l +WHERE lf.field = 'mrpack_loaders' +AND l.loader = 'mrpack' +ON CONFLICT DO NOTHING; + +-- 3) Delete the old versions that had mrpack added to them +DELETE FROM loaders_versions lv +WHERE lv.loader_id != (SELECT id FROM loaders WHERE loader = 'mrpack') +AND lv.version_id IN ( + SELECT version_id + FROM loaders_versions + WHERE loader_id = (SELECT id FROM loaders WHERE loader = 'mrpack') +); diff --git a/src/routes/v2/projects.rs b/src/routes/v2/projects.rs index 8755b95c..c1a75db7 100644 --- a/src/routes/v2/projects.rs +++ b/src/routes/v2/projects.rs @@ -117,14 +117,10 @@ pub async fn random_projects_get( let response = v3::projects::random_projects_get(web::Query(count), pool.clone(), redis.clone()).await?; // Convert response to V2 format - match v2_reroute::extract_ok_json::(response).await { + match v2_reroute::extract_ok_json::>(response).await { Ok(project) => { - let version_item = match project.versions.first() { - Some(vid) => version_item::Version::get((*vid).into(), &**pool, &redis).await?, - None => None, - }; - let project = LegacyProject::from(project, version_item); - Ok(HttpResponse::Ok().json(project)) + let legacy_projects = LegacyProject::from_many(project, &**pool, &redis).await?; + Ok(HttpResponse::Ok().json(legacy_projects)) } Err(response) => Ok(response), } diff --git a/src/routes/v2/version_creation.rs b/src/routes/v2/version_creation.rs index 3e1de740..4bfa9613 100644 --- a/src/routes/v2/version_creation.rs +++ b/src/routes/v2/version_creation.rs @@ -100,14 +100,33 @@ pub async fn version_create( fields.insert("client_side".to_string(), json!("required")); fields.insert("server_side".to_string(), json!("optional")); - // TODO: Some kind of handling here to ensure project type is fine. - // We expect the version uploaded to be of loader type modpack, but there might not be a way to check here for that. - // After all, theoretically, they could be creating a genuine 'fabric' mod, and modpack no longer carries information on whether its a mod or modpack, - // as those are out to the versions. + // Handle project type via file extension prediction + let mut project_type = None; + for file_part in &legacy_create.file_parts { + if let Some(ext) = file_part.split('.').last() { + match ext { + "mrpack" => { + project_type = Some("modpack"); + break; + } + // No other type matters + _ => {} + } + break; + } + } - // Ideally this would, if the project 'should' be a modpack: - // - change the loaders to mrpack only - // - add loader fields to the project for the corresponding loaders + // Modpacks now use the "mrpack" loader, and loaders are converted to loader fields. + // Setting of 'project_type' directly is removed, it's loader-based now. + if project_type == Some("modpack") { + fields.insert("mrpack_loaders".to_string(), json!(legacy_create.loaders)); + } + + let loaders = if project_type == Some("modpack") { + vec![Loader("mrpack".to_string())] + } else { + legacy_create.loaders + }; Ok(v3::version_creation::InitialVersionData { project_id: legacy_create.project_id, @@ -117,7 +136,7 @@ pub async fn version_create( version_body: legacy_create.version_body, dependencies: legacy_create.dependencies, release_channel: legacy_create.release_channel, - loaders: legacy_create.loaders, + loaders, featured: legacy_create.featured, primary_file: legacy_create.primary_file, status: legacy_create.status,