Skip to content

Commit

Permalink
Fix expired demos query
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmacdonald committed Jun 14, 2024
1 parent ea4cd96 commit bb6e0b4
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 27 deletions.
27 changes: 3 additions & 24 deletions internal/demo/demo_respository.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions internal/demo/demo_usecase.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion internal/domain/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down

0 comments on commit bb6e0b4

Please sign in to comment.