use a transaction for committing invites to db
This commit is contained in:
parent
8ee362b991
commit
94e26f8080
2 changed files with 10 additions and 4 deletions
|
@ -3,6 +3,7 @@ use std::time::Duration;
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use julid::Julid;
|
use julid::Julid;
|
||||||
use parse_duration::parse;
|
use parse_duration::parse;
|
||||||
|
use serde::Serialize;
|
||||||
use sqlx::SqlitePool;
|
use sqlx::SqlitePool;
|
||||||
use what2watch::{conf::Config, get_db_pool, Invitation, User};
|
use what2watch::{conf::Config, get_db_pool, Invitation, User};
|
||||||
|
|
||||||
|
@ -50,7 +51,9 @@ fn main() {
|
||||||
|
|
||||||
let invites = rt.block_on(async {
|
let invites = rt.block_on(async {
|
||||||
ensure_omega(&pool).await;
|
ensure_omega(&pool).await;
|
||||||
generate_invites(quest, num, &pool).await
|
let invites = generate_invites(quest, num, &pool).await;
|
||||||
|
pool.close().await;
|
||||||
|
invites
|
||||||
});
|
});
|
||||||
for invite in invites {
|
for invite in invites {
|
||||||
println!("{base_url}/signup/{invite}");
|
println!("{base_url}/signup/{invite}");
|
||||||
|
|
|
@ -42,13 +42,14 @@ impl Default for Invitation {
|
||||||
|
|
||||||
impl Invitation {
|
impl Invitation {
|
||||||
pub async fn commit(&self, db: &SqlitePool) -> Result<Julid, CreateInviteError> {
|
pub async fn commit(&self, db: &SqlitePool) -> Result<Julid, CreateInviteError> {
|
||||||
sqlx::query_scalar(
|
let mut tx = db.begin().await.unwrap();
|
||||||
|
let id = sqlx::query_scalar(
|
||||||
"insert into invites (owner, expires_at, remaining) values (?, ?, ?) returning id",
|
"insert into invites (owner, expires_at, remaining) values (?, ?, ?) returning id",
|
||||||
)
|
)
|
||||||
.bind(self.owner)
|
.bind(self.owner)
|
||||||
.bind(self.expires_at)
|
.bind(self.expires_at)
|
||||||
.bind(self.remaining)
|
.bind(self.remaining)
|
||||||
.fetch_optional(db)
|
.fetch_optional(&mut *tx)
|
||||||
.await
|
.await
|
||||||
.map_err(|e| {
|
.map_err(|e| {
|
||||||
tracing::debug!("Got error creating invite: {e}");
|
tracing::debug!("Got error creating invite: {e}");
|
||||||
|
@ -64,7 +65,9 @@ impl Invitation {
|
||||||
CreateInviteErrorKind::Unknown
|
CreateInviteErrorKind::Unknown
|
||||||
}
|
}
|
||||||
})?
|
})?
|
||||||
.ok_or(CreateInviteErrorKind::Unknown.into())
|
.ok_or(CreateInviteErrorKind::Unknown.into());
|
||||||
|
tx.commit().await.unwrap();
|
||||||
|
id
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(owner: Julid) -> Self {
|
pub fn new(owner: Julid) -> Self {
|
||||||
|
|
Loading…
Reference in a new issue