Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
Risky dedups
Browse files Browse the repository at this point in the history
  • Loading branch information
OmegaJak committed Oct 9, 2023
1 parent 2d01a5f commit 55c3e3a
Show file tree
Hide file tree
Showing 7 changed files with 365 additions and 372 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,7 @@ woothee = "0.13.0"

lettre = "0.10.4"

derive-new = "0.5.9"

[dev-dependencies]
actix-http = "3.4.0"
54 changes: 38 additions & 16 deletions src/database/models/project_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,38 @@ impl GalleryItem {
}
}

#[derive(derive_new::new)]
pub struct ModCategory {
project_id: ProjectId,
category_id: CategoryId,
is_additional: bool,
}

impl ModCategory {
pub async fn insert_many(
items: Vec<Self>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), DatabaseError> {
let (project_ids, category_ids, is_additionals): (Vec<_>, Vec<_>, Vec<_>) = items
.into_iter()
.map(|mc| (mc.project_id.0, mc.category_id.0, mc.is_additional))
.multiunzip();
sqlx::query!(
"
INSERT INTO mods_categories (joining_mod_id, joining_category_id, is_additional)
SELECT * FROM UNNEST ($1::bigint[], $2::int[], $3::bool[])
",
&project_ids[..],
&category_ids[..],
&is_additionals[..]
)
.execute(&mut *transaction)
.await?;

Ok(())
}
}

#[derive(Clone)]
pub struct ProjectBuilder {
pub project_id: ProjectId,
Expand Down Expand Up @@ -199,27 +231,17 @@ impl ProjectBuilder {

GalleryItem::insert_many(gallery_items, self.project_id, &mut *transaction).await?;

let project_id = self.project_id.0;
let (project_ids, category_ids, is_additionals): (Vec<_>, Vec<_>, Vec<_>) = categories
let project_id = self.project_id;
let mod_categories = categories
.into_iter()
.map(|c| (project_id, c.0, false))
.map(|c| ModCategory::new(project_id, c, false))
.chain(
additional_categories
.into_iter()
.map(|c| (project_id, c.0, true)),
.map(|c| ModCategory::new(project_id, c, true)),
)
.multiunzip();
sqlx::query!(
"
INSERT INTO mods_categories (joining_mod_id, joining_category_id, is_additional)
SELECT * FROM UNNEST ($1::bigint[], $2::int[], $3::bool[])
",
&project_ids[..],
&category_ids[..],
&is_additionals[..]
)
.execute(&mut *transaction)
.await?;
.collect_vec();
ModCategory::insert_many(mod_categories, &mut *transaction).await?;

Project::update_game_versions(self.project_id, &mut *transaction).await?;
Project::update_loaders(self.project_id, &mut *transaction).await?;
Expand Down
2 changes: 0 additions & 2 deletions src/database/models/thread_item.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::os::unix::thread;

use super::ids::*;
use crate::database::models::DatabaseError;
use crate::models::threads::{MessageBody, ThreadType};
Expand Down
56 changes: 51 additions & 5 deletions src/database/models/version_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,8 +241,37 @@ impl VersionBuilder {

DependencyBuilder::insert_many(dependencies, self.version_id, transaction).await?;

let (loader_ids, version_ids): (Vec<_>, Vec<_>) =
loaders.iter().map(|l| (l.0, version_id.0)).unzip();
let loader_versions = loaders
.iter()
.map(|l| LoaderVersion::new(*l, version_id))
.collect_vec();
LoaderVersion::insert_many(loader_versions, &mut *transaction).await?;

let game_version_versions = game_versions
.iter()
.map(|v| VersionVersion::new(*v, version_id))
.collect_vec();
VersionVersion::insert_many(game_version_versions, &mut *transaction).await?;

Ok(self.version_id)
}
}

#[derive(derive_new::new)]
pub struct LoaderVersion {
pub loader_id: LoaderId,
pub version_id: VersionId,
}

impl LoaderVersion {
pub async fn insert_many(
items: Vec<Self>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), DatabaseError> {
let (loader_ids, version_ids): (Vec<_>, Vec<_>) = items
.iter()
.map(|l| (l.loader_id.0, l.version_id.0))
.unzip();
sqlx::query!(
"
INSERT INTO loaders_versions (loader_id, version_id)
Expand All @@ -254,8 +283,25 @@ impl VersionBuilder {
.execute(&mut *transaction)
.await?;

let (game_version_ids, version_ids): (Vec<_>, Vec<_>) =
game_versions.iter().map(|v| (v.0, version_id.0)).unzip();
Ok(())
}
}

#[derive(derive_new::new)]
pub struct VersionVersion {
pub game_version_id: GameVersionId,
pub joining_version_id: VersionId,
}

impl VersionVersion {
pub async fn insert_many(
items: Vec<Self>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<(), DatabaseError> {
let (game_version_ids, version_ids): (Vec<_>, Vec<_>) = items
.into_iter()
.map(|i| (i.game_version_id.0, i.joining_version_id.0))
.unzip();
sqlx::query!(
"
INSERT INTO game_versions_versions (game_version_id, joining_version_id)
Expand All @@ -267,7 +313,7 @@ impl VersionBuilder {
.execute(&mut *transaction)
.await?;

Ok(self.version_id)
Ok(())
}
}

Expand Down
Loading

0 comments on commit 55c3e3a

Please sign in to comment.