Skip to content

Commit

Permalink
Merge pull request #71 from Quaver/db_sc_del_failed
Browse files Browse the repository at this point in the history
Command to delete failed scores
  • Loading branch information
Swan authored Aug 29, 2024
2 parents 90aba0b + 1386c0f commit 92551d2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions cmd/console/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func init() {
RootCmd.AddCommand(commands.SupervisorActivityCmd)
RootCmd.AddCommand(commands.ClanRankMapCmd)
RootCmd.AddCommand(commands.DenyOnHoldCmd)
RootCmd.AddCommand(commands.DatabaseScoresBatchDeleteFailed)

// Migrations
RootCmd.AddCommand(migrations.MigrationPlaylistMapsetCmd)
Expand Down
53 changes: 53 additions & 0 deletions cmd/console/commands/database_scores_batch_delete_failed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package commands

import (
"github.com/Quaver/api2/db"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)

var DatabaseScoresBatchDeleteFailed = &cobra.Command{
Use: "database:scores:batch:delete",
Short: "Deletes every failed score",
Run: func(cmd *cobra.Command, args []string) {
batchSize := 5000
offset := 0

for {
var scoreIDs []int

result := db.SQL.
Model(&db.Score{}).
Where("failed = 1").
Limit(batchSize).
Offset(offset).
Pluck("id", &scoreIDs)

if result.Error != nil {
logrus.Info("Something went wrong")
break
}

if len(scoreIDs) == 0 {
if offset == 0 {
logrus.Info("No more failed scores found!")
} else {
logrus.Info("There are no scores left to delete!")
}

break
}

deletedBatch := db.SQL.Where("id IN ?", scoreIDs).Delete(&db.Score{})

if deletedBatch.Error != nil {
logrus.Info("Something went wrong when deleting scores")
break
}

logrus.Infof("Deleted %d failed scores", deletedBatch.RowsAffected)

offset += batchSize
}
},
}

0 comments on commit 92551d2

Please sign in to comment.