Clean up sqlite policy rules after deletion

Signed-off-by: bcmmbaga <bethuelmbaga12@gmail.com>
This commit is contained in:
bcmmbaga
2024-11-27 15:40:37 +03:00
parent 41b4e3177a
commit 875b8d662c

View File

@@ -1449,15 +1449,19 @@ func (s *SqlStore) SavePolicy(ctx context.Context, lockStrength LockingStrength,
} }
func (s *SqlStore) DeletePolicy(ctx context.Context, lockStrength LockingStrength, accountID, policyID string) error { func (s *SqlStore) DeletePolicy(ctx context.Context, lockStrength LockingStrength, accountID, policyID string) error {
result := s.db.Clauses(clause.Locking{Strength: string(lockStrength)}). err := s.db.Transaction(func(tx *gorm.DB) error {
Delete(&Policy{}, accountAndIDQueryCondition, accountID, policyID) result := tx.Clauses(clause.Locking{Strength: string(lockStrength)}).
if err := result.Error; err != nil { Delete(&PolicyRule{}, "policy_id = ?", policyID)
log.WithContext(ctx).Errorf("failed to delete policy from store: %s", err) if result.Error != nil {
return status.Errorf(status.Internal, "failed to delete policy from store") return result.Error
} }
if result.RowsAffected == 0 { return tx.Clauses(clause.Locking{Strength: string(lockStrength)}).
return status.NewPolicyNotFoundError(policyID) 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")
} }
return nil return nil