From 0f734a24100383171a866fccc194dea578141d74 Mon Sep 17 00:00:00 2001 From: kim <89579420+NyaaaWhatsUpDoc@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:15:11 +0000 Subject: [chore] move PopulateAccountStats() nil check often performed into function itself (#3158) * move PopulateAccountStats() nil check often performed into function itself * fix test to take in mind we don't repopulate account stats if not-nil --- internal/db/bundb/account.go | 13 ++++++++----- internal/db/bundb/account_test.go | 4 ++++ 2 files changed, 12 insertions(+), 5 deletions(-) (limited to 'internal/db') diff --git a/internal/db/bundb/account.go b/internal/db/bundb/account.go index 90bf3eb65..94fd054bf 100644 --- a/internal/db/bundb/account.go +++ b/internal/db/bundb/account.go @@ -712,11 +712,9 @@ func (a *accountDB) PopulateAccount(ctx context.Context, account *gtsmodel.Accou } } - if account.Stats == nil { - // Get / Create stats for this account. - if err := a.state.DB.PopulateAccountStats(ctx, account); err != nil { - errs.Appendf("error populating account stats: %w", err) - } + // Get / Create stats for this account (handles case of already set). + if err := a.state.DB.PopulateAccountStats(ctx, account); err != nil { + errs.Appendf("error populating account stats: %w", err) } return errs.Combine() @@ -1160,6 +1158,11 @@ func (a *accountDB) UpdateAccountSettings( } func (a *accountDB) PopulateAccountStats(ctx context.Context, account *gtsmodel.Account) error { + if account.Stats != nil { + // Already populated! + return nil + } + // Fetch stats from db cache with loader callback. stats, err := a.state.Caches.DB.AccountStats.LoadOne( "AccountID", diff --git a/internal/db/bundb/account_test.go b/internal/db/bundb/account_test.go index 116ea19f0..28c9274f7 100644 --- a/internal/db/bundb/account_test.go +++ b/internal/db/bundb/account_test.go @@ -743,6 +743,10 @@ func (suite *AccountTestSuite) TestAccountStatsAll() { suite.FailNow(err.Error()) } + // Nil out account stats to allow + // db to refetch + regenerate them. + account.Stats = nil + // Get stats for a third time, they // should get regenerated now, but // only for local accounts. -- cgit v1.3