-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from Quaver/db_sc_del_failed
Command to delete failed scores
- Loading branch information
Showing
2 changed files
with
54 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
53 changes: 53 additions & 0 deletions
53
cmd/console/commands/database_scores_batch_delete_failed.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
}, | ||
} |