This is an automated email from the ASF dual-hosted git repository. yasithdev pushed a commit to branch user-delete-in-use in repository https://gitbox.apache.org/repos/asf/airavata-custos.git
commit 1fc7247903b5473b3fed5bed8c3c2a7ef52d719b Author: Yasith Jayawardana <[email protected]> AuthorDate: Wed Nov 4 23:47:29 2026 -0500 Report a referenced user as in use on delete. --- pkg/service/user.go | 5 +++++ pkg/service/user_merge_integration_test.go | 11 +++++++++++ 2 files changed, 16 insertions(+) diff --git a/pkg/service/user.go b/pkg/service/user.go index c5ed04125..d131ef064 100644 --- a/pkg/service/user.go +++ b/pkg/service/user.go @@ -20,10 +20,12 @@ package service import ( "context" "database/sql" + "errors" "fmt" "github.com/apache/airavata-custos/pkg/events" "github.com/apache/airavata-custos/pkg/models" + "github.com/jackc/pgx/v5/pgconn" ) // CreateUser persists a new user. If user.ID is empty a new UUID is generated. @@ -243,6 +245,9 @@ func (s *Service) DeleteUser(ctx context.Context, id string) error { } return s.eventBus.Publish(ctx, tx, events.UserDeleteEvent, existing) }); err != nil { + if pgErr := (*pgconn.PgError)(nil); errors.As(err, &pgErr) && pgErr.Code == "23503" { + return fmt.Errorf("%w: user %q is referenced by records that must stay", ErrInUse, id) + } return fmt.Errorf("delete user: %w", err) } s.InvalidateAllIdentities() diff --git a/pkg/service/user_merge_integration_test.go b/pkg/service/user_merge_integration_test.go index 2aaf8fd90..33bd4a30f 100644 --- a/pkg/service/user_merge_integration_test.go +++ b/pkg/service/user_merge_integration_test.go @@ -22,6 +22,7 @@ package service import ( "context" "encoding/json" + "errors" "fmt" "strings" "testing" @@ -194,3 +195,13 @@ func TestDeleteUser_RemovesTheirAllocationMemberships(t *testing.T) { t.Fatalf("%d memberships left", len(left)) } } + +func TestDeleteUser_RefusesAUserStillReferenced(t *testing.T) { + database := setupTestDB(t) + svc := newTestService(database) + pi := seedUser(t, database, "[email protected]") + newClusterAllocation(t, svc, pi) + if err := svc.DeleteUser(ctx(), pi); !errors.Is(err, ErrInUse) { + t.Fatalf("delete a project PI: %v, want ErrInUse", err) + } +}
