From 5380e3387d343d50e73bb00d731c4f00f16d2b34 Mon Sep 17 00:00:00 2001 From: chunningham Date: Thu, 20 Jul 2023 11:46:42 +0200 Subject: [PATCH] fix duplicate del/inv/rev insertion (#150) --- Cargo.lock | 8 ++++---- kepler-core/src/models/delegation.rs | 14 ++++++++++---- kepler-core/src/models/invocation.rs | 13 ++++++++++--- kepler-core/src/models/revocation.rs | 13 ++++++++++--- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3f968428..10763bd4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2844,7 +2844,7 @@ dependencies = [ [[package]] name = "kepler" -version = "0.1.0" +version = "0.2.0" dependencies = [ "anyhow", "aws-config", @@ -2899,7 +2899,7 @@ dependencies = [ [[package]] name = "kepler-lib" -version = "0.1.0" +version = "0.2.0" dependencies = [ "async-trait", "base64 0.13.1", @@ -2926,7 +2926,7 @@ dependencies = [ [[package]] name = "kepler-sdk" -version = "0.1.0" +version = "0.2.0" dependencies = [ "base64 0.13.1", "hex", @@ -2944,7 +2944,7 @@ dependencies = [ [[package]] name = "kepler-sdk-wasm" -version = "0.2.0" +version = "0.3.0" dependencies = [ "console_error_panic_hook", "getrandom 0.2.9", diff --git a/kepler-core/src/models/delegation.rs b/kepler-core/src/models/delegation.rs index 9673f796..494d7830 100644 --- a/kepler-core/src/models/delegation.rs +++ b/kepler-core/src/models/delegation.rs @@ -2,7 +2,7 @@ use crate::hash::Hash; use crate::types::{Facts, Resource}; use crate::{events::Delegation, models::*, relationships::*, util}; use kepler_lib::{authorization::KeplerDelegation, resolver::DID_METHODS}; -use sea_orm::{entity::prelude::*, ConnectionTrait}; +use sea_orm::{entity::prelude::*, sea_query::OnConflict, ConnectionTrait}; use time::OffsetDateTime; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] @@ -227,7 +227,7 @@ async fn save( let hash: Hash = crate::hash::hash(&serialization); // save delegation - Entity::insert(ActiveModel::from(Model { + match Entity::insert(ActiveModel::from(Model { id: hash, delegator: delegation.delegator, delegatee: delegation.delegate, @@ -237,8 +237,15 @@ async fn save( facts: None, serialization, })) + .on_conflict(OnConflict::column(Column::Id).do_nothing().to_owned()) .exec(db) - .await?; + .await + { + Err(DbErr::RecordNotInserted) => return Ok(hash), + r => { + r?; + } + }; // save abilities if !delegation.capabilities.is_empty() { @@ -270,7 +277,6 @@ async fn save( } async fn save_actors(actors: &[&str], db: &C) -> Result<(), DbErr> { - use sea_orm::sea_query::OnConflict; match actor::Entity::insert_many( actors .iter() diff --git a/kepler-core/src/models/invocation.rs b/kepler-core/src/models/invocation.rs index 8d7eaec6..ab9c7c29 100644 --- a/kepler-core/src/models/invocation.rs +++ b/kepler-core/src/models/invocation.rs @@ -7,7 +7,7 @@ use super::super::{ use crate::hash::Hash; use crate::types::{Facts, OrbitIdWrap, Resource}; use kepler_lib::{authorization::KeplerInvocation, resolver::DID_METHODS}; -use sea_orm::{entity::prelude::*, Condition, ConnectionTrait, QueryOrder}; +use sea_orm::{entity::prelude::*, sea_query::OnConflict, Condition, ConnectionTrait, QueryOrder}; use time::OffsetDateTime; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] @@ -191,15 +191,22 @@ async fn save( let hash = crate::hash::hash(&serialization); let issued_at = time.unwrap_or_else(OffsetDateTime::now_utc); - Entity::insert(ActiveModel::from(Model { + match Entity::insert(ActiveModel::from(Model { id: hash, issued_at, serialization, facts: None, invoker: invocation.invoker, })) + .on_conflict(OnConflict::column(Column::Id).do_nothing().to_owned()) .exec(db) - .await?; + .await + { + Err(DbErr::RecordNotInserted) => return Ok(hash), + r => { + r?; + } + }; // save invoked abilities if !invocation.capabilities.is_empty() { diff --git a/kepler-core/src/models/revocation.rs b/kepler-core/src/models/revocation.rs index 9ba1ad7a..bbd94ef0 100644 --- a/kepler-core/src/models/revocation.rs +++ b/kepler-core/src/models/revocation.rs @@ -1,7 +1,7 @@ use super::super::{events::Revocation, models::*, relationships::*}; use crate::hash::{hash, Hash}; use kepler_lib::authorization::KeplerRevocation; -use sea_orm::{entity::prelude::*, ConnectionTrait}; +use sea_orm::{entity::prelude::*, sea_query::OnConflict, ConnectionTrait}; use time::OffsetDateTime; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)] @@ -95,14 +95,21 @@ pub(crate) async fn process( return Err(RevocationError::UnauthorizedRevoker(r.revoker).into()); }; - Entity::insert(ActiveModel::from(Model { + match Entity::insert(ActiveModel::from(Model { id: hash, serialization, revoker: r.revoker, revoked: delegation.id, })) + .on_conflict(OnConflict::column(Column::Id).do_nothing().to_owned()) .exec(db) - .await?; + .await + { + Err(DbErr::RecordNotInserted) => return Ok(hash), + r => { + r?; + } + }; if !r.parents.is_empty() { parent_delegations::Entity::insert_many(r.parents.into_iter().map(|p| {