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

Commit

Permalink
fix duplicate del/inv/rev insertion (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
chunningham authored Jul 20, 2023
1 parent f4813c4 commit 5380e33
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 14 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

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

14 changes: 10 additions & 4 deletions kepler-core/src/models/delegation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -227,7 +227,7 @@ async fn save<C: ConnectionTrait>(
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,
Expand All @@ -237,8 +237,15 @@ async fn save<C: ConnectionTrait>(
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() {
Expand Down Expand Up @@ -270,7 +277,6 @@ async fn save<C: ConnectionTrait>(
}

async fn save_actors<C: ConnectionTrait>(actors: &[&str], db: &C) -> Result<(), DbErr> {
use sea_orm::sea_query::OnConflict;
match actor::Entity::insert_many(
actors
.iter()
Expand Down
13 changes: 10 additions & 3 deletions kepler-core/src/models/invocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -191,15 +191,22 @@ async fn save<C: ConnectionTrait>(
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() {
Expand Down
13 changes: 10 additions & 3 deletions kepler-core/src/models/revocation.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -95,14 +95,21 @@ pub(crate) async fn process<C: ConnectionTrait>(
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| {
Expand Down

0 comments on commit 5380e33

Please sign in to comment.