Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cargo sqlx prepare improvement #772

Merged
merged 2 commits into from
Nov 30, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

23 changes: 23 additions & 0 deletions src/queue/payouts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::de::DeserializeOwned;
use serde::{Deserialize, Serialize};
use serde_json::{json, Value};
use sha2::Sha256;
use sqlx::postgres::PgQueryResult;
use sqlx::PgPool;
use std::collections::HashMap;

Expand Down Expand Up @@ -550,3 +551,25 @@ pub async fn process_payout(

Ok(())
}

// Used for testing, should be the same as the above function
pub async fn insert_payouts(
insert_user_ids: Vec<i64>,
insert_project_ids: Vec<i64>,
insert_payouts: Vec<Decimal>,
insert_starts: Vec<DateTime<Utc>>,
transaction: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> sqlx::Result<PgQueryResult> {
sqlx::query!(
"
INSERT INTO payouts_values (user_id, mod_id, amount, created)
SELECT * FROM UNNEST ($1::bigint[], $2::bigint[], $3::numeric[], $4::timestamptz[])
",
&insert_user_ids[..],
&insert_project_ids[..],
&insert_payouts[..],
&insert_starts[..]
)
.execute(&mut **transaction)
.await
}
19 changes: 9 additions & 10 deletions tests/analytics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use common::{
use itertools::Itertools;
use labrinth::models::ids::base62_impl::parse_base62;
use labrinth::models::teams::ProjectPermissions;
use labrinth::queue::payouts;
use rust_decimal::{prelude::ToPrimitive, Decimal};

mod common;
Expand Down Expand Up @@ -55,19 +56,17 @@ pub async fn analytics_revenue() {
insert_starts.push(*time);
}

sqlx::query!(
"
INSERT INTO payouts_values (user_id, mod_id, amount, created)
SELECT * FROM UNNEST ($1::bigint[], $2::bigint[], $3::numeric[], $4::timestamptz[])
",
&insert_user_ids[..],
&insert_project_ids[..],
&insert_payouts[..],
&insert_starts[..]
let mut transaction = pool.begin().await.unwrap();
payouts::insert_payouts(
insert_user_ids,
insert_project_ids,
insert_payouts,
insert_starts,
&mut transaction,
)
.execute(&pool)
.await
.unwrap();
transaction.commit().await.unwrap();

let day = 86400;

Expand Down
Loading