From bb6e0b4b0d73415ff79f4589a3a2c275ccc1158c Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Fri, 14 Jun 2024 03:08:17 -0600 Subject: [PATCH 1/2] Fix expired demos query --- internal/demo/demo_respository.go | 27 +++------------------------ internal/demo/demo_usecase.go | 4 ++-- internal/domain/demo.go | 1 - 3 files changed, 5 insertions(+), 27 deletions(-) diff --git a/internal/demo/demo_respository.go b/internal/demo/demo_respository.go index f8a717f8..64db4362 100644 --- a/internal/demo/demo_respository.go +++ b/internal/demo/demo_respository.go @@ -23,8 +23,9 @@ func (r *demoRepository) ExpiredDemos(ctx context.Context, limit uint64) ([]doma Builder(). Select("d.demo_id", "d.title", "d.asset_id"). From("demo d"). - Where(sq.Eq{"d.archive": false}). - OrderBy("d.demo_id DESC"). + LeftJoin("report r on d.demo_id = r.demo_id"). + Where("coalesce(r.demo_id > 0, false) = false"). + OrderBy("d.demo_id"). Offset(limit)) if errRow != nil { return nil, r.db.DBErr(errRow) @@ -154,28 +155,6 @@ func (r *demoRepository) GetDemos(ctx context.Context) ([]domain.DemoFile, error } func (r *demoRepository) SaveDemo(ctx context.Context, demoFile *domain.DemoFile) error { - // Find open reports and if any are returned, mark the demo as archived so that it does not get auto - // deleted during cleanup. - // Reports can happen mid-game which is why this is checked when the demo is saved and not during the report where - // we have no completed demo instance/id yet. - reportRow, reportRowErr := r.db.QueryRowBuilder(ctx, r.db. - Builder(). - Select("count(report_id)"). - From("report"). - Where(sq.Eq{"demo_id": demoFile.DemoID})) - if reportRowErr != nil { - return errors.Join(reportRowErr, domain.ErrReportCountQuery) - } - - var count int - if errScan := reportRow.Scan(&count); errScan != nil && !errors.Is(errScan, domain.ErrNoResult) { - return r.db.DBErr(errScan) - } - - if count > 0 { - demoFile.Archive = true - } - var err error if demoFile.DemoID > 0 { err = r.updateDemo(ctx, demoFile) diff --git a/internal/demo/demo_usecase.go b/internal/demo/demo_usecase.go index 88a433d6..aaf6f515 100644 --- a/internal/demo/demo_usecase.go +++ b/internal/demo/demo_usecase.go @@ -38,7 +38,7 @@ func NewDemoUsecase(bucket domain.Bucket, repository domain.DemoRepository, asse } } -func (d demoUsecase) OldestDemo(ctx context.Context) (domain.DemoInfo, error) { +func (d demoUsecase) oldest(ctx context.Context) (domain.DemoInfo, error) { demos, errDemos := d.repository.ExpiredDemos(ctx, 1) if errDemos != nil { return domain.DemoInfo{}, errDemos @@ -86,7 +86,7 @@ func (d demoUsecase) truncateBySpace(ctx context.Context, root string, maxAllowe return count, size, nil } - oldestDemo, errOldest := d.OldestDemo(ctx) + oldestDemo, errOldest := d.oldest(ctx) if errOldest != nil { if errors.Is(errOldest, domain.ErrNoResult) { return count, size, nil diff --git a/internal/domain/demo.go b/internal/domain/demo.go index 95e0ccc0..c24589bd 100644 --- a/internal/domain/demo.go +++ b/internal/domain/demo.go @@ -16,7 +16,6 @@ type DemoUsecase interface { GetDemoByName(ctx context.Context, demoName string, demoFile *DemoFile) error GetDemos(ctx context.Context) ([]DemoFile, error) CreateFromAsset(ctx context.Context, asset Asset, serverID int) (*DemoFile, error) - OldestDemo(ctx context.Context) (DemoInfo, error) TriggerCleanup() } From c0081b7bef791517965494d9f79169fd9774e739 Mon Sep 17 00:00:00 2001 From: Leigh MacDonald Date: Fri, 14 Jun 2024 03:12:10 -0600 Subject: [PATCH 2/2] Simplify condition --- internal/demo/demo_respository.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/demo/demo_respository.go b/internal/demo/demo_respository.go index 64db4362..439059e6 100644 --- a/internal/demo/demo_respository.go +++ b/internal/demo/demo_respository.go @@ -24,7 +24,7 @@ func (r *demoRepository) ExpiredDemos(ctx context.Context, limit uint64) ([]doma Select("d.demo_id", "d.title", "d.asset_id"). From("demo d"). LeftJoin("report r on d.demo_id = r.demo_id"). - Where("coalesce(r.demo_id > 0, false) = false"). + Where("r.demo_id > 0"). OrderBy("d.demo_id"). Offset(limit)) if errRow != nil {