You've already forked AstralRinth
forked from didirus/AstralRinth
Query optimization (#235)
* Optimize version queries and decrease some query complexity * Run formatter
This commit is contained in:
@@ -95,9 +95,8 @@ pub async fn maven_metadata(
|
||||
}
|
||||
let version_names = sqlx::query!(
|
||||
"
|
||||
SELECT version_number, release_channels.channel channel
|
||||
SELECT version_number, version_type
|
||||
FROM versions
|
||||
LEFT JOIN release_channels ON release_channels.id = versions.release_channel
|
||||
WHERE mod_id = $1
|
||||
",
|
||||
data.inner.id as database::models::ids::ProjectId
|
||||
@@ -117,7 +116,7 @@ pub async fn maven_metadata(
|
||||
.to_string(),
|
||||
release: version_names
|
||||
.iter()
|
||||
.rfind(|x| x.channel == "release")
|
||||
.rfind(|x| x.version_type == "release")
|
||||
.map_or("", |x| &x.version_number)
|
||||
.to_string(),
|
||||
versions: Versions {
|
||||
|
||||
@@ -729,11 +729,6 @@ async fn create_initial_version(
|
||||
// Randomly generate a new id to be used for the version
|
||||
let version_id: VersionId = models::generate_version_id(transaction).await?.into();
|
||||
|
||||
let release_channel =
|
||||
models::ChannelId::get_id(version_data.release_channel.as_str(), &mut *transaction)
|
||||
.await?
|
||||
.expect("Release Channel not found in database");
|
||||
|
||||
let game_versions = version_data
|
||||
.game_versions
|
||||
.iter()
|
||||
@@ -786,8 +781,8 @@ async fn create_initial_version(
|
||||
dependencies,
|
||||
game_versions,
|
||||
loaders,
|
||||
release_channel,
|
||||
featured: version_data.featured,
|
||||
version_type: version_data.release_channel.to_string(),
|
||||
};
|
||||
|
||||
Ok(version)
|
||||
|
||||
@@ -186,13 +186,6 @@ async fn version_create_inner(
|
||||
|
||||
let version_id: VersionId = models::generate_version_id(transaction).await?.into();
|
||||
|
||||
let release_channel = models::ChannelId::get_id(
|
||||
version_create_data.release_channel.as_str(),
|
||||
&mut *transaction,
|
||||
)
|
||||
.await?
|
||||
.expect("Release channel not found in database");
|
||||
|
||||
let project_type = sqlx::query!(
|
||||
"
|
||||
SELECT name FROM project_types pt
|
||||
@@ -255,7 +248,7 @@ async fn version_create_inner(
|
||||
dependencies,
|
||||
game_versions,
|
||||
loaders,
|
||||
release_channel,
|
||||
version_type: version_create_data.release_channel.to_string(),
|
||||
featured: version_create_data.featured,
|
||||
});
|
||||
|
||||
|
||||
@@ -377,7 +377,7 @@ pub async fn get_versions_from_hashes(
|
||||
"
|
||||
SELECT h.hash hash, h.algorithm algorithm, f.version_id version_id FROM hashes h
|
||||
INNER JOIN files f ON h.file_id = f.id
|
||||
WHERE h.algorithm = $2 AND h.hash IN (SELECT * FROM UNNEST($1::bytea[]))
|
||||
WHERE h.algorithm = $2 AND h.hash = ANY($1::bytea[])
|
||||
",
|
||||
hashes_parsed.as_slice(),
|
||||
file_data.algorithm
|
||||
@@ -425,7 +425,7 @@ pub async fn download_files(
|
||||
SELECT f.url url, h.hash hash, h.algorithm algorithm, f.version_id version_id, v.mod_id project_id FROM hashes h
|
||||
INNER JOIN files f ON h.file_id = f.id
|
||||
INNER JOIN versions v ON v.id = f.version_id
|
||||
WHERE h.algorithm = $2 AND h.hash IN (SELECT * FROM UNNEST($1::bytea[]))
|
||||
WHERE h.algorithm = $2 AND h.hash = ANY($1::bytea[])
|
||||
",
|
||||
hashes_parsed.as_slice(),
|
||||
file_data.algorithm
|
||||
@@ -476,7 +476,7 @@ pub async fn update_files(
|
||||
SELECT f.url url, h.hash hash, h.algorithm algorithm, f.version_id version_id, v.mod_id project_id FROM hashes h
|
||||
INNER JOIN files f ON h.file_id = f.id
|
||||
INNER JOIN versions v ON v.id = f.version_id
|
||||
WHERE h.algorithm = $2 AND h.hash IN (SELECT * FROM UNNEST($1::bytea[]))
|
||||
WHERE h.algorithm = $2 AND h.hash = ANY($1::bytea[])
|
||||
",
|
||||
hashes_parsed.as_slice(),
|
||||
update_data.algorithm
|
||||
|
||||
@@ -160,7 +160,7 @@ pub fn convert_version(
|
||||
changelog_url: data.changelog_url,
|
||||
date_published: data.date_published,
|
||||
downloads: data.downloads as u32,
|
||||
version_type: match data.release_channel.as_str() {
|
||||
version_type: match data.version_type.as_str() {
|
||||
"release" => VersionType::Release,
|
||||
"beta" => VersionType::Beta,
|
||||
"alpha" => VersionType::Alpha,
|
||||
@@ -301,24 +301,13 @@ pub async fn version_edit(
|
||||
}
|
||||
|
||||
if let Some(version_type) = &new_version.version_type {
|
||||
let channel = database::models::ids::ChannelId::get_id(
|
||||
version_type.as_str(),
|
||||
&mut *transaction,
|
||||
)
|
||||
.await?
|
||||
.ok_or_else(|| {
|
||||
ApiError::InvalidInputError(
|
||||
"No database entry for version type provided.".to_string(),
|
||||
)
|
||||
})?;
|
||||
|
||||
sqlx::query!(
|
||||
"
|
||||
UPDATE versions
|
||||
SET release_channel = $1
|
||||
SET version_type = $1
|
||||
WHERE (id = $2)
|
||||
",
|
||||
channel as database::models::ids::ChannelId,
|
||||
version_type.as_str(),
|
||||
id as database::models::ids::VersionId,
|
||||
)
|
||||
.execute(&mut *transaction)
|
||||
|
||||
Reference in New Issue
Block a user