Skip to content

Commit 1386c0f

Browse files
committed
Command to delete failed scores
1 parent 90aba0b commit 1386c0f

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

cmd/console/cmd.go

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ func init() {
4848
RootCmd.AddCommand(commands.SupervisorActivityCmd)
4949
RootCmd.AddCommand(commands.ClanRankMapCmd)
5050
RootCmd.AddCommand(commands.DenyOnHoldCmd)
51+
RootCmd.AddCommand(commands.DatabaseScoresBatchDeleteFailed)
5152

5253
// Migrations
5354
RootCmd.AddCommand(migrations.MigrationPlaylistMapsetCmd)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package commands
2+
3+
import (
4+
"github.com/Quaver/api2/db"
5+
"github.com/sirupsen/logrus"
6+
"github.com/spf13/cobra"
7+
)
8+
9+
var DatabaseScoresBatchDeleteFailed = &cobra.Command{
10+
Use: "database:scores:batch:delete",
11+
Short: "Deletes every failed score",
12+
Run: func(cmd *cobra.Command, args []string) {
13+
batchSize := 5000
14+
offset := 0
15+
16+
for {
17+
var scoreIDs []int
18+
19+
result := db.SQL.
20+
Model(&db.Score{}).
21+
Where("failed = 1").
22+
Limit(batchSize).
23+
Offset(offset).
24+
Pluck("id", &scoreIDs)
25+
26+
if result.Error != nil {
27+
logrus.Info("Something went wrong")
28+
break
29+
}
30+
31+
if len(scoreIDs) == 0 {
32+
if offset == 0 {
33+
logrus.Info("No more failed scores found!")
34+
} else {
35+
logrus.Info("There are no scores left to delete!")
36+
}
37+
38+
break
39+
}
40+
41+
deletedBatch := db.SQL.Where("id IN ?", scoreIDs).Delete(&db.Score{})
42+
43+
if deletedBatch.Error != nil {
44+
logrus.Info("Something went wrong when deleting scores")
45+
break
46+
}
47+
48+
logrus.Infof("Deleted %d failed scores", deletedBatch.RowsAffected)
49+
50+
offset += batchSize
51+
}
52+
},
53+
}

0 commit comments

Comments
 (0)