diff --git a/backend/internal/repository/account_repo.go b/backend/internal/repository/account_repo.go index 5d7ff70d..525abf65 100644 --- a/backend/internal/repository/account_repo.go +++ b/backend/internal/repository/account_repo.go @@ -454,6 +454,7 @@ func (r *accountRepository) Delete(ctx context.Context, id int64) error { return err } } + r.deleteSchedulerAccountSnapshot(ctx, id) if err := enqueueSchedulerOutbox(ctx, r.sql, service.SchedulerOutboxEventAccountChanged, &id, nil, buildSchedulerGroupPayload(groupIDs)); err != nil { logger.LegacyPrintf("repository.account", "[SchedulerOutbox] enqueue account delete failed: account=%d err=%v", id, err) } @@ -754,6 +755,15 @@ func (r *accountRepository) syncSchedulerAccountSnapshot(ctx context.Context, ac } } +func (r *accountRepository) deleteSchedulerAccountSnapshot(ctx context.Context, accountID int64) { + if r == nil || r.schedulerCache == nil || accountID <= 0 { + return + } + if err := r.schedulerCache.DeleteAccount(ctx, accountID); err != nil { + logger.LegacyPrintf("repository.account", "[Scheduler] delete account snapshot failed: id=%d err=%v", accountID, err) + } +} + func (r *accountRepository) syncSchedulerAccountSnapshots(ctx context.Context, accountIDs []int64) { if r == nil || r.schedulerCache == nil || len(accountIDs) == 0 { return diff --git a/backend/internal/repository/account_repo_integration_test.go b/backend/internal/repository/account_repo_integration_test.go index 87ed49ca..9e15047c 100644 --- a/backend/internal/repository/account_repo_integration_test.go +++ b/backend/internal/repository/account_repo_integration_test.go @@ -23,6 +23,7 @@ type AccountRepoSuite struct { type schedulerCacheRecorder struct { setAccounts []*service.Account + deleteIDs []int64 accounts map[int64]*service.Account } @@ -53,6 +54,10 @@ func (s *schedulerCacheRecorder) SetAccount(ctx context.Context, account *servic } func (s *schedulerCacheRecorder) DeleteAccount(ctx context.Context, accountID int64) error { + s.deleteIDs = append(s.deleteIDs, accountID) + if s.accounts != nil { + delete(s.accounts, accountID) + } return nil } @@ -185,6 +190,27 @@ func (s *AccountRepoSuite) TestDelete() { s.Require().Error(err, "expected error after delete") } +func (s *AccountRepoSuite) TestDelete_RemovesSchedulerAccountSnapshot() { + account := mustCreateAccount(s.T(), s.client, &service.Account{Name: "to-delete-cache"}) + cacheRecorder := &schedulerCacheRecorder{ + accounts: map[int64]*service.Account{ + account.ID: { + ID: account.ID, + Name: account.Name, + Status: service.StatusActive, + Schedulable: true, + }, + }, + } + s.repo.schedulerCache = cacheRecorder + + err := s.repo.Delete(s.ctx, account.ID) + s.Require().NoError(err, "Delete") + + s.Require().Equal([]int64{account.ID}, cacheRecorder.deleteIDs) + s.Require().NotContains(cacheRecorder.accounts, account.ID) +} + func (s *AccountRepoSuite) TestDelete_WithGroupBindings() { group := mustCreateGroup(s.T(), s.client, &service.Group{Name: "g-del"}) account := mustCreateAccount(s.T(), s.client, &service.Account{Name: "acc-del"})