From 6559f676c35373dbe954ebde2f8b04cdf748dd0c Mon Sep 17 00:00:00 2001 From: Ty Date: Sun, 10 Mar 2024 22:22:42 -0600 Subject: [PATCH] Fix empty claim imports and support transaction rollback if claims insert fails --- src/commands/profiles/import.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/commands/profiles/import.rs b/src/commands/profiles/import.rs index 72ba7ec..c7be84f 100644 --- a/src/commands/profiles/import.rs +++ b/src/commands/profiles/import.rs @@ -6,7 +6,7 @@ use asp::{ utils::jwt::JwtSerialize, }; use clap::Parser; -use sea_orm::{EntityTrait, IntoActiveValue, SqlErr}; +use sea_orm::{EntityTrait, IntoActiveValue, SqlErr, TransactionTrait as _}; use crate::{ commands::AspmSubcommand, @@ -59,6 +59,8 @@ impl AspmSubcommand for ProfilesImportCommand { let (key, profile) = AriadneSignatureProfile::decode_and_verify(&profile, fingerprint) .context("The provided or fetched profile was invalid and unable to be imported")?; + let txn = state.db.begin().await?; + match Profiles::insert(profiles::ActiveModel { alias: self.alias.join(" ").into_active_value(), key: key.fingerprint.clone().into_active_value(), @@ -77,7 +79,7 @@ impl AspmSubcommand for ProfilesImportCommand { .map(|color| color.to_string()) .into_active_value(), }) - .exec(&state.db) + .exec(&txn) .await { Ok(_) => { @@ -86,10 +88,13 @@ impl AspmSubcommand for ProfilesImportCommand { uri: claim.into_active_value(), ..Default::default() })) - .exec(&state.db) + .on_empty_do_nothing() + .exec(&txn) .await + // No rollback needed here as an error will cause txn to be dropped, which causes a rollback .context("Unable to insert claims into database")?; + txn.commit().await.context("Unable to commit transaction")?; println!( "Successfully imported profile with fingerprint {}", key.fingerprint