Skip to content

Commit

Permalink
[scd] inline cleanup of implicit subscription into CRDB call when rem…
Browse files Browse the repository at this point in the history
…oving OIR
  • Loading branch information
Shastick committed Jul 29, 2024
1 parent 4f9b508 commit daf4e31
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 17 deletions.
14 changes: 0 additions & 14 deletions pkg/scd/operational_intents_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ func (a *Server) DeleteOperationalIntentReference(ctx context.Context, req *rest
"OperationalIntent owned by %s, but %s attempted to delete", old.Manager, *req.Auth.ClientID)
}

removeImplicitSubscription, err := subscriptionCanBeRemoved(ctx, r, old.SubscriptionID)
if err != nil {
return stacktrace.Propagate(err, "Could not determine if Subscription can be removed")
}

// Find Subscriptions that may overlap the OperationalIntent's Volume4D
allsubs, err := r.SearchSubscriptions(ctx, &dssmodels.Volume4D{
StartTime: old.StartTime,
Expand Down Expand Up @@ -130,15 +125,6 @@ func (a *Server) DeleteOperationalIntentReference(ctx context.Context, req *rest
return stacktrace.Propagate(err, "Unable to delete OperationalIntent from repo")
}

// removeImplicitSubscription is only true if the OIR had a subscription defined
if removeImplicitSubscription {
// Automatically remove a now-unused implicit Subscription
err = r.DeleteSubscription(ctx, *old.SubscriptionID)
if err != nil {
return stacktrace.Propagate(err, "Unable to delete associated implicit Subscription")
}
}

// Return response to client
response = &restapi.ChangeOperationalIntentReferenceResponse{
OperationalIntentReference: *old.ToRest(),
Expand Down
40 changes: 37 additions & 3 deletions pkg/scd/store/cockroach/operational_intents.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,23 +146,57 @@ func (s *repo) GetOperationalIntent(ctx context.Context, id dssmodels.ID) (*scdm
func (s *repo) DeleteOperationalIntent(ctx context.Context, id dssmodels.ID) error {
var (
deleteOperationQuery = `
DELETE FROM
WITH deleted_oir AS (
DELETE FROM
scd_operations
WHERE
id = $1
RETURNING
id, subscription_id
),
exists_and_is_implicit AS (
SELECT subscription_id
FROM deleted_oir
JOIN scd_subscriptions ON scd_subscriptions.id = deleted_oir.subscription_id
WHERE scd_subscriptions.implicit = true
),
dependent_oirs AS ( -- NOTE: this sub-query will still return the OIR being deleted (!)
SELECT id
FROM scd_operations
WHERE subscription_id = (SELECT subscription_id FROM deleted_oir)
),
deleted_subscription_id AS (
DELETE FROM scd_subscriptions
WHERE id = (SELECT subscription_id FROM exists_and_is_implicit)
AND (SELECT COUNT(*) FROM dependent_oirs) = 1 -- NOTE: see above, the OIR being removed is still counted here, hence a value of 1
RETURNING id
)
SELECT -- NOTE: the following subqueries let us return details about which deletion occurred
(SELECT id FROM deleted_oir) AS deleted_oir_id,
(SELECT id FROM deleted_subscription_id) AS deleted_subscription_id
`
)

uid, err := id.PgUUID()
if err != nil {
return stacktrace.Propagate(err, "Failed to convert id to PgUUID")
}
res, err := s.q.Exec(ctx, deleteOperationQuery, uid)
res, err := s.q.Query(ctx, deleteOperationQuery, uid)
if err != nil {
return stacktrace.Propagate(err, "Error in query: %s", deleteOperationQuery)
}
defer res.Close()

if res.RowsAffected() == 0 {
// Check that the deleted OIR ID was returned:
var opID *dssmodels.ID
if res.Next() {
err = res.Scan(&opID, nil)
if err != nil {
return stacktrace.Propagate(err, "Error scanning deleted Operation ID")
}
}

if opID == nil || *opID != id {
return stacktrace.NewError("Could not delete Operation that does not exist")
}

Expand Down

0 comments on commit daf4e31

Please sign in to comment.