Skip to content

Commit 3099343

Browse files
at least use contextx
Signed-off-by: Sarah Funkhouser <147884153+golanglemonade@users.noreply.github.com>
1 parent 011080b commit 3099343

4 files changed

Lines changed: 40 additions & 36 deletions

File tree

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,28 @@
11
package contextx
22

3-
import "context"
3+
import (
4+
"context"
45

5-
// SkipCustomEnumDeleteKey is the context key used to skip the "in use" errors check during enum deletion.
6-
// This is used during organization cascade deletion where the deletion order is handled by EdgeCleanup.
7-
// else the custom deletion by default will check if the enum is being used by another other object.
8-
// But with this, we can just skip the check because when the org itself is deleted, it cascades to delete the
9-
// custom enums too
10-
type SkipCustomEnumDeleteKey string
11-
12-
const (
13-
// SkipCustomEnumInUseCheck is the context value that triggers skipping the "in use" check/error during enum deletion.
14-
SkipCustomEnumInUseCheck SkipCustomEnumDeleteKey = "custom_enum_cascade_delete_operation"
6+
utilsctx "github.com/theopenlane/utils/contextx"
157
)
168

9+
// skipEnumInUseCheckKey skips the "in use" check during custom enum deletion.
10+
// This is used during organization cascade deletion where the deletion order is handled by
11+
// EdgeCleanup, the custom enum deletion would otherwise check whether the enum is still used by
12+
// another object. When the organization itself is deleted the cascade removes those objects too,
13+
// so the check has nothing useful to say
14+
var skipEnumInUseCheckKey = utilsctx.NewKey[bool]()
15+
1716
// WithSkipEnumInUseCheck returns a new context with the skip flag set for custom enums deletion.
1817
// This should be used when deleting CustomTypeEnums as part of a cascade delete
19-
// where the deletion order is handled by EdgeCleanup.
18+
// where the deletion order is handled by EdgeCleanup
2019
func WithSkipEnumInUseCheck(ctx context.Context) context.Context {
21-
return context.WithValue(ctx, SkipCustomEnumInUseCheck, true)
20+
return skipEnumInUseCheckKey.Set(ctx, true)
21+
}
22+
23+
// SkipEnumInUseCheckEnabled reports whether the custom enum "in use" check should be skipped
24+
func SkipEnumInUseCheckEnabled(ctx context.Context) bool {
25+
skip, _ := skipEnumInUseCheckKey.Get(ctx)
26+
27+
return skip
2228
}
Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,24 @@
11
package contextx
22

3-
import "context"
3+
import (
4+
"context"
45

5-
// PurgeHistoryKey is the context key used to opt a cascade delete into purging the
6-
// history rows of every record it removes
7-
type PurgeHistoryKey string
8-
9-
const (
10-
// PurgeHistory is the context value that triggers the history purge during a cascade delete
11-
PurgeHistory PurgeHistoryKey = "cascade_delete_purge_history"
6+
utilsctx "github.com/theopenlane/utils/contextx"
127
)
138

9+
// purgeHistoryKey marks a cascade delete as purging history rather than recording it
10+
var purgeHistoryKey = utilsctx.NewKey[bool]()
11+
1412
// WithPurgeHistory returns a new context that opts the cascade delete into purging history rows.
1513
// EdgeCleanup deletes the history for each record it removes only when this is set, so callers that
1614
// want to keep the audit trail are unaffected
1715
func WithPurgeHistory(ctx context.Context) context.Context {
18-
return context.WithValue(ctx, PurgeHistory, true)
16+
return purgeHistoryKey.Set(ctx, true)
1917
}
2018

2119
// PurgeHistoryEnabled reports whether the cascade delete should purge history rows
2220
func PurgeHistoryEnabled(ctx context.Context) bool {
23-
purge, ok := ctx.Value(PurgeHistory).(bool)
21+
purge, _ := purgeHistoryKey.Get(ctx)
2422

25-
return ok && purge
23+
return purge
2624
}
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package contextx
22

3-
import "context"
3+
import (
4+
"context"
45

5-
// TupleCleanupKey is the context key used to force relationship tuple cleanup on delete
6-
type TupleCleanupKey string
7-
8-
const (
9-
// TupleCleanup is the context value that forces tuple cleanup even for internal requests
10-
TupleCleanup TupleCleanupKey = "cascade_delete_tuple_cleanup"
6+
utilsctx "github.com/theopenlane/utils/contextx"
117
)
128

9+
// tupleCleanupKey forces relationship tuple cleanup on delete even for internal requests
10+
var tupleCleanupKey = utilsctx.NewKey[bool]()
11+
1312
// WithTupleCleanup returns a new context that forces the delete permissions hook to run even though
1413
// the request is an internal one. The organization cascade delete runs as an internal caller so it
15-
// can bypass privacy rules, but the records it removes still need their tuples cleaned out of FGA
14+
// can bypass privacy rules, but the records it removes still need their tuples cleaned out of FGA,
15+
// otherwise every cascaded object leaves its relationships behind pointing at rows that are gone
1616
func WithTupleCleanup(ctx context.Context) context.Context {
17-
return context.WithValue(ctx, TupleCleanup, true)
17+
return tupleCleanupKey.Set(ctx, true)
1818
}
1919

2020
// TupleCleanupEnabled reports whether tuple cleanup should run despite an internal request
2121
func TupleCleanupEnabled(ctx context.Context) bool {
22-
cleanup, ok := ctx.Value(TupleCleanup).(bool)
22+
cleanup, _ := tupleCleanupKey.Get(ctx)
2323

24-
return ok && cleanup
24+
return cleanup
2525
}

internal/ent/hooks/customenums.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ func HookCustomTypeEnumDelete() ent.Hook {
281281

282282
// skip the "in use" error/check when deleting via organization cascade
283283
// the organization edge cleanup needs to cascade deletes
284-
if ctx.Value(contextx.SkipCustomEnumInUseCheck) == true {
284+
if contextx.SkipEnumInUseCheckEnabled(ctx) {
285285
return next.Mutate(ctx, m)
286286
}
287287

0 commit comments

Comments
 (0)