Modifies sql queries to use CTEs (#773)

* fixes huge slowodwn on version item

* changes!

* fixes, touch ups, indices

* clippy prepare
This commit is contained in:
Wyatt Verchere
2023-11-30 11:10:56 -08:00
committed by GitHub
parent ed33dd2127
commit 58093a9438
19 changed files with 957 additions and 549 deletions

View File

@@ -8,7 +8,7 @@ use actix_web::{
use async_trait::async_trait;
use bytes::Bytes;
use chrono::{DateTime, Utc};
use labrinth::{search::SearchResults, util::actix::AppendsMultipart};
use labrinth::{models::projects::Project, search::SearchResults, util::actix::AppendsMultipart};
use rust_decimal::Decimal;
use serde_json::json;
@@ -182,6 +182,12 @@ impl ApiProject for ApiV3 {
}
impl ApiV3 {
pub async fn get_project_deserialized(&self, id_or_slug: &str, pat: &str) -> Project {
let resp = self.get_project(id_or_slug, pat).await;
assert_eq!(resp.status(), 200);
test::read_body_json(resp).await
}
pub async fn search_deserialized(
&self,
query: Option<&str>,

View File

@@ -13,6 +13,7 @@ use crate::common::dummy_data::TestFile;
mod common;
#[actix_rt::test]
async fn creating_loader_fields() {
with_test_environment(None, |test_env: TestEnvironment<ApiV3>| async move {
let api = &test_env.api;
@@ -24,7 +25,18 @@ async fn creating_loader_fields() {
.project_alpha
.project_id
.clone();
let alpha_project_id = serde_json::from_str(&format!("\"{}\"", alpha_project_id)).unwrap();
let alpha_project_id_parsed = test_env
.dummy
.as_ref()
.unwrap()
.project_alpha
.project_id_parsed;
let beta_project_id_parsed = test_env
.dummy
.as_ref()
.unwrap()
.project_beta
.project_id_parsed;
let alpha_version_id = &test_env
.dummy
.as_ref()
@@ -39,7 +51,7 @@ async fn creating_loader_fields() {
// - Create version
let resp = api
.add_public_version(
alpha_project_id,
alpha_project_id_parsed,
"1.0.0",
TestFile::build_random_jar(),
None,
@@ -72,7 +84,7 @@ async fn creating_loader_fields() {
// - Create version
let resp = api
.add_public_version(
alpha_project_id,
alpha_project_id_parsed,
"1.0.0",
TestFile::build_random_jar(),
None,
@@ -105,7 +117,7 @@ async fn creating_loader_fields() {
// - Create version
let resp = api
.add_public_version(
alpha_project_id,
alpha_project_id_parsed,
"1.0.0",
TestFile::build_random_jar(),
None,
@@ -127,7 +139,7 @@ async fn creating_loader_fields() {
// - Create version
let resp = api
.add_public_version(
alpha_project_id,
alpha_project_id_parsed,
"1.0.0",
TestFile::build_random_jar(),
None,
@@ -149,7 +161,7 @@ async fn creating_loader_fields() {
// - Create version
let resp: actix_web::dev::ServiceResponse = api
.add_public_version(
alpha_project_id,
alpha_project_id_parsed,
"1.0.0",
TestFile::build_random_jar(),
None,
@@ -189,7 +201,7 @@ async fn creating_loader_fields() {
// - Create version
let resp = api
.add_public_version(
alpha_project_id,
alpha_project_id_parsed,
"1.0.0",
TestFile::build_random_jar(),
None,
@@ -224,7 +236,7 @@ async fn creating_loader_fields() {
// - Create version
let v = api
.add_public_version_deserialized(
alpha_project_id,
alpha_project_id_parsed,
"1.0.0",
TestFile::build_random_jar(),
None,
@@ -260,7 +272,7 @@ async fn creating_loader_fields() {
// - Create
let v = api
.add_public_version_deserialized(
alpha_project_id,
alpha_project_id_parsed,
"1.0.0",
TestFile::build_random_jar(),
None,
@@ -309,6 +321,59 @@ async fn creating_loader_fields() {
v.fields.get("game_versions").unwrap(),
&json!(["1.20.1", "1.20.2"])
);
// Now that we've created a version, we need to make sure that the Project's loader fields are updated (aggregate)
// First, add a new version
api.add_public_version_deserialized(
alpha_project_id_parsed,
"1.0.1",
TestFile::build_random_jar(),
None,
Some(
serde_json::from_value(json!([{
"op": "add",
"path": "/game_versions",
"value": ["1.20.5"]
}, {
"op": "add",
"path": "/singleplayer",
"value": false
}]))
.unwrap(),
),
USER_USER_PAT,
)
.await;
// Also, add one to the beta project
api.add_public_version_deserialized(
beta_project_id_parsed,
"1.0.1",
TestFile::build_random_jar(),
None,
Some(
serde_json::from_value(json!([{
"op": "add",
"path": "/game_versions",
"value": ["1.20.4"]
}]))
.unwrap(),
),
USER_USER_PAT,
)
.await;
let project = api
.get_project_deserialized(&alpha_project_id.to_string(), USER_USER_PAT)
.await;
assert_eq!(
project.fields.get("game_versions").unwrap(),
&[json!("1.20.1"), json!("1.20.2"), json!("1.20.5")]
);
assert_eq!(
project.fields.get("singleplayer").unwrap(),
&[json!(false), json!(true)]
);
})
.await
}

View File

@@ -13,7 +13,9 @@ use common::environment::{with_test_environment, with_test_environment_all};
use futures::StreamExt;
use labrinth::database::models::version_item::VERSIONS_NAMESPACE;
use labrinth::models::ids::base62_impl::parse_base62;
use labrinth::models::projects::{VersionId, VersionStatus, VersionType};
use labrinth::models::projects::{
Dependency, DependencyType, VersionId, VersionStatus, VersionType,
};
use labrinth::routes::v3::version_file::FileUpdateData;
use serde_json::json;
@@ -381,6 +383,13 @@ pub async fn test_patch_version() {
let api = &test_env.api;
let alpha_version_id = &test_env.dummy.as_ref().unwrap().project_alpha.version_id;
let beta_project_id = &test_env.dummy.as_ref().unwrap().project_beta.project_id;
let beta_project_id_parsed = &test_env
.dummy
.as_ref()
.unwrap()
.project_beta
.project_id_parsed;
// // First, we do some patch requests that should fail.
// // Failure because the user is not authorized.
@@ -419,7 +428,11 @@ pub async fn test_patch_version() {
"version_number": "1.3.0",
"changelog": "new changelog",
"version_type": "beta",
// // "dependencies": [], TODO: test this
"dependencies": [{
"project_id": beta_project_id,
"dependency_type": "required",
"file_name": "dummy_file_name"
}],
"game_versions": ["1.20.5"],
"loaders": ["forge"],
"featured": false,
@@ -443,6 +456,15 @@ pub async fn test_patch_version() {
version.version_type,
serde_json::from_str::<VersionType>("\"beta\"").unwrap()
);
assert_eq!(
version.dependencies,
vec![Dependency {
project_id: Some(*beta_project_id_parsed),
version_id: None,
file_name: Some("dummy_file_name".to_string()),
dependency_type: DependencyType::Required
}]
);
assert_eq!(version.loaders, vec!["forge".to_string()]);
assert!(!version.featured);
assert_eq!(version.status, VersionStatus::from_string("draft"));