Skip to content

Commit

Permalink
Clean up sqlite policy rules after deletion
Browse files Browse the repository at this point in the history
Signed-off-by: bcmmbaga <[email protected]>
  • Loading branch information
bcmmbaga committed Nov 27, 2024
1 parent 41b4e31 commit 875b8d6
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions management/server/sql_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -1449,17 +1449,21 @@ func (s *SqlStore) SavePolicy(ctx context.Context, lockStrength LockingStrength,
}

func (s *SqlStore) DeletePolicy(ctx context.Context, lockStrength LockingStrength, accountID, policyID string) error {
result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}).
Delete(&Policy{}, accountAndIDQueryCondition, accountID, policyID)
if err := result.Error; err != nil {
err := s.db.Transaction(func(tx *gorm.DB) error {
result := tx.Clauses(clause.Locking{Strength: string(lockStrength)}).
Delete(&PolicyRule{}, "policy_id = ?", policyID)
if result.Error != nil {
return result.Error
}

return tx.Clauses(clause.Locking{Strength: string(lockStrength)}).
Delete(&Policy{}, accountAndIDQueryCondition, accountID, policyID).Error
})
if err != nil {
log.WithContext(ctx).Errorf("failed to delete policy from store: %s", err)
return status.Errorf(status.Internal, "failed to delete policy from store")
}

if result.RowsAffected == 0 {
return status.NewPolicyNotFoundError(policyID)
}

return nil
}

Expand Down

0 comments on commit 875b8d6

Please sign in to comment.