-
Are you sure you want to clear all match, ranking, and alliance selection data?
+
Are you sure you want to clear all practice match data?
+
The database will automatically be backed up.
diff --git a/web/alliance_selection.go b/web/alliance_selection.go
index f1bd8402..10ba94f4 100644
--- a/web/alliance_selection.go
+++ b/web/alliance_selection.go
@@ -147,21 +147,11 @@ func (web *Web) allianceSelectionResetHandler(w http.ResponseWriter, r *http.Req
}
// Delete any playoff matches that were already created (but not played since they would fail the above check).
- matches, err := web.arena.Database.GetMatchesByType(model.Playoff, true)
+ err := web.deleteMatchDataForType(model.Playoff)
if err != nil {
handleWebErr(w, err)
return
}
- for _, match := range matches {
- if err = web.arena.Database.DeleteMatch(match.Id); err != nil {
- handleWebErr(w, err)
- return
- }
- }
- if err = web.arena.Database.DeleteScheduledBreaksByMatchType(model.Playoff); err != nil {
- handleWebErr(w, err)
- return
- }
// Delete the saved alliances.
if err = web.arena.Database.TruncateAlliances(); err != nil {
diff --git a/web/setup_settings.go b/web/setup_settings.go
index c203e7c2..4147da1e 100644
--- a/web/setup_settings.go
+++ b/web/setup_settings.go
@@ -203,46 +203,53 @@ func (web *Web) restoreDbHandler(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/setup/settings", 303)
}
-// Deletes all data except for the team list.
+// Deletes all match data including and beyond the given tournament stage.
func (web *Web) clearDbHandler(w http.ResponseWriter, r *http.Request) {
if !web.userIsAdmin(w, r) {
return
}
- // Back up the database.
- err := web.arena.Database.Backup(web.arena.EventSettings.Name, "pre_clear")
- if err != nil {
- handleWebErr(w, err)
+ matchType, err := model.MatchTypeFromString(r.PathValue("type"))
+ if err != nil || matchType == model.Test {
+ web.renderSettings(w, r, "Invalid tournament stage to clear.")
return
- }
- err = web.arena.Database.TruncateMatches()
- if err != nil {
- handleWebErr(w, err)
- return
- }
- err = web.arena.Database.TruncateMatchResults()
- if err != nil {
- handleWebErr(w, err)
- return
- }
- err = web.arena.Database.TruncateRankings()
- if err != nil {
- handleWebErr(w, err)
- return
}
- err = web.arena.Database.TruncateAlliances()
+
+ // Back up the database.
+ err = web.arena.Database.Backup(web.arena.EventSettings.Name, "pre_clear")
if err != nil {
handleWebErr(w, err)
return
}
- err = web.arena.Database.TruncateScheduledBreaks()
- if err != nil {
- handleWebErr(w, err)
- return
+
+ switch matchType {
+ case model.Practice:
+ if err = web.deleteMatchDataForType(model.Practice); err != nil {
+ handleWebErr(w, err)
+ return
+ }
+ case model.Qualification:
+ if err = web.deleteMatchDataForType(model.Qualification); err != nil {
+ handleWebErr(w, err)
+ return
+ }
+ if err = web.arena.Database.TruncateRankings(); err != nil {
+ handleWebErr(w, err)
+ return
+ }
+ case model.Playoff:
+ if err = web.deleteMatchDataForType(model.Playoff); err != nil {
+ handleWebErr(w, err)
+ return
+ }
+ if err = web.arena.Database.TruncateAlliances(); err != nil {
+ handleWebErr(w, err)
+ return
+ }
+ web.arena.AllianceSelectionAlliances = []model.Alliance{}
+ cachedRankedTeams = []*RankedTeam{}
}
- web.arena.AllianceSelectionAlliances = []model.Alliance{}
- cachedRankedTeams = []*RankedTeam{}
http.Redirect(w, r, "/setup/settings", 303)
}
@@ -368,3 +375,35 @@ func (web *Web) renderSettings(w http.ResponseWriter, r *http.Request, errorMess
return
}
}
+
+// Deletes all match data (matches, results, and scheduled breaks) for the given match type.
+func (web *Web) deleteMatchDataForType(matchType model.MatchType) error {
+ matches, err := web.arena.Database.GetMatchesByType(matchType, true)
+ if err != nil {
+ return err
+ }
+ for _, match := range matches {
+ // Loop to delete all match results for the match before deleting the match itself.
+ matchResult, err := web.arena.Database.GetMatchResultForMatch(match.Id)
+ if err != nil {
+ return err
+ }
+ for matchResult != nil {
+ if err = web.arena.Database.DeleteMatchResult(matchResult.Id); err != nil {
+ return err
+ }
+ matchResult, err = web.arena.Database.GetMatchResultForMatch(match.Id)
+ if err != nil {
+ return err
+ }
+ }
+
+ if err = web.arena.Database.DeleteMatch(match.Id); err != nil {
+ return err
+ }
+ }
+ if err = web.arena.Database.DeleteScheduledBreaksByMatchType(matchType); err != nil {
+ return err
+ }
+ return nil
+}
diff --git a/web/setup_settings_test.go b/web/setup_settings_test.go
index f558495e..8d040e94 100644
--- a/web/setup_settings_test.go
+++ b/web/setup_settings_test.go
@@ -62,27 +62,108 @@ func TestSetupSettingsInvalidValues(t *testing.T) {
}
func TestSetupSettingsClearDb(t *testing.T) {
+ createData := func(web *Web) {
+ assert.Nil(t, web.arena.Database.CreateTeam(&model.Team{Id: 254}))
+ assert.Nil(t, web.arena.Database.CreateMatch(&model.Match{Type: model.Practice}))
+ assert.Nil(t, web.arena.Database.CreateMatch(&model.Match{Type: model.Qualification}))
+ assert.Nil(t, web.arena.Database.CreateMatch(&model.Match{Type: model.Playoff}))
+ assert.Nil(t, web.arena.Database.CreateMatchResult(&model.MatchResult{MatchId: 1, PlayNumber: 1}))
+ assert.Nil(t, web.arena.Database.CreateMatchResult(&model.MatchResult{MatchId: 1, PlayNumber: 2}))
+ assert.Nil(t, web.arena.Database.CreateMatchResult(&model.MatchResult{MatchId: 2, PlayNumber: 1}))
+ assert.Nil(t, web.arena.Database.CreateMatchResult(&model.MatchResult{MatchId: 3, PlayNumber: 1}))
+ assert.Nil(t, web.arena.Database.CreateRanking(&game.Ranking{TeamId: 254}))
+ assert.Nil(t, web.arena.Database.CreateAlliance(&model.Alliance{Id: 1}))
+ web.arena.AllianceSelectionAlliances = append(web.arena.AllianceSelectionAlliances, model.Alliance{Id: 1})
+ }
+
+ // Test clearing practice data.
web := setupTestWeb(t)
-
- assert.Nil(t, web.arena.Database.CreateTeam(&model.Team{Id: 254}))
- assert.Nil(t, web.arena.Database.CreateMatch(&model.Match{Type: model.Qualification}))
- assert.Nil(t, web.arena.Database.CreateMatchResult(new(model.MatchResult)))
- assert.Nil(t, web.arena.Database.CreateRanking(&game.Ranking{TeamId: 254}))
- assert.Nil(t, web.arena.Database.CreateAlliance(&model.Alliance{Id: 1}))
- recorder := web.postHttpResponse("/setup/db/clear", "")
+ createData(web)
+ recorder := web.postHttpResponse("/setup/db/clear/practice", "")
assert.Equal(t, 303, recorder.Code)
-
teams, _ := web.arena.Database.GetAllTeams()
assert.NotEmpty(t, teams)
- matches, _ := web.arena.Database.GetMatchesByType(model.Qualification, true)
+ matches, _ := web.arena.Database.GetMatchesByType(model.Practice, true)
assert.Empty(t, matches)
+ matchResult, _ := web.arena.Database.GetMatchResultForMatch(1)
+ assert.Nil(t, matchResult)
+ matches, _ = web.arena.Database.GetMatchesByType(model.Qualification, true)
+ assert.NotEmpty(t, matches)
+ matchResult, _ = web.arena.Database.GetMatchResultForMatch(2)
+ assert.NotNil(t, matchResult)
+ matches, _ = web.arena.Database.GetMatchesByType(model.Playoff, true)
+ assert.NotEmpty(t, matches)
+ matchResult, _ = web.arena.Database.GetMatchResultForMatch(3)
+ assert.NotNil(t, matchResult)
rankings, _ := web.arena.Database.GetAllRankings()
+ assert.NotEmpty(t, rankings)
+ tournament.CalculateRankings(web.arena.Database, false)
+ assert.NotEmpty(t, rankings)
+ alliances, _ := web.arena.Database.GetAllAlliances()
+ assert.NotEmpty(t, alliances)
+ assert.NotEmpty(t, web.arena.AllianceSelectionAlliances)
+
+ // Test clearing qualification data.
+ web = setupTestWeb(t)
+ createData(web)
+ recorder = web.postHttpResponse("/setup/db/clear/qualification", "")
+ assert.Equal(t, 303, recorder.Code)
+ teams, _ = web.arena.Database.GetAllTeams()
+ assert.NotEmpty(t, teams)
+ matches, _ = web.arena.Database.GetMatchesByType(model.Practice, true)
+ assert.NotEmpty(t, matches)
+ matchResult, _ = web.arena.Database.GetMatchResultForMatch(1)
+ assert.NotNil(t, matchResult)
+ matches, _ = web.arena.Database.GetMatchesByType(model.Qualification, true)
+ assert.Empty(t, matches)
+ matchResult, _ = web.arena.Database.GetMatchResultForMatch(2)
+ assert.Nil(t, matchResult)
+ matches, _ = web.arena.Database.GetMatchesByType(model.Playoff, true)
+ assert.NotEmpty(t, matches)
+ matchResult, _ = web.arena.Database.GetMatchResultForMatch(3)
+ assert.NotNil(t, matchResult)
+ rankings, _ = web.arena.Database.GetAllRankings()
assert.Empty(t, rankings)
tournament.CalculateRankings(web.arena.Database, false)
assert.Empty(t, rankings)
- alliances, _ := web.arena.Database.GetAllAlliances()
+ alliances, _ = web.arena.Database.GetAllAlliances()
+ assert.NotEmpty(t, alliances)
+ assert.NotEmpty(t, web.arena.AllianceSelectionAlliances)
+
+ // Test clearing playoff data.
+ web = setupTestWeb(t)
+ createData(web)
+ recorder = web.postHttpResponse("/setup/db/clear/playoff", "")
+ assert.Equal(t, 303, recorder.Code)
+ teams, _ = web.arena.Database.GetAllTeams()
+ assert.NotEmpty(t, teams)
+ matches, _ = web.arena.Database.GetMatchesByType(model.Practice, true)
+ assert.NotEmpty(t, matches)
+ matchResult, _ = web.arena.Database.GetMatchResultForMatch(1)
+ assert.NotNil(t, matchResult)
+ matches, _ = web.arena.Database.GetMatchesByType(model.Qualification, true)
+ assert.NotEmpty(t, matches)
+ matchResult, _ = web.arena.Database.GetMatchResultForMatch(2)
+ assert.NotNil(t, matchResult)
+ matches, _ = web.arena.Database.GetMatchesByType(model.Playoff, true)
+ assert.Empty(t, matches)
+ matchResult, _ = web.arena.Database.GetMatchResultForMatch(3)
+ assert.Nil(t, matchResult)
+ rankings, _ = web.arena.Database.GetAllRankings()
+ assert.NotEmpty(t, rankings)
+ tournament.CalculateRankings(web.arena.Database, false)
+ assert.NotEmpty(t, rankings)
+ alliances, _ = web.arena.Database.GetAllAlliances()
assert.Empty(t, alliances)
assert.Empty(t, web.arena.AllianceSelectionAlliances)
+
+ // Test with invalid match types.
+ recorder = web.postHttpResponse("/setup/db/clear/all", "")
+ assert.Equal(t, 200, recorder.Code)
+ assert.Contains(t, recorder.Body.String(), "Invalid tournament stage to clear")
+ recorder = web.postHttpResponse("/setup/db/clear/test", "")
+ assert.Equal(t, 200, recorder.Code)
+ assert.Contains(t, recorder.Body.String(), "Invalid tournament stage to clear")
}
func TestSetupSettingsBackupRestoreDb(t *testing.T) {
diff --git a/web/web.go b/web/web.go
index 966cc120..565a54d9 100644
--- a/web/web.go
+++ b/web/web.go
@@ -198,7 +198,7 @@ func (web *Web) newHandler() http.Handler {
mux.HandleFunc("POST /setup/awards", web.awardsPostHandler)
mux.HandleFunc("GET /setup/breaks", web.breaksGetHandler)
mux.HandleFunc("POST /setup/breaks", web.breaksPostHandler)
- mux.HandleFunc("POST /setup/db/clear", web.clearDbHandler)
+ mux.HandleFunc("POST /setup/db/clear/{type}", web.clearDbHandler)
mux.HandleFunc("POST /setup/db/restore", web.restoreDbHandler)
mux.HandleFunc("GET /setup/db/save", web.saveDbHandler)
mux.HandleFunc("GET /setup/displays", web.displaysGetHandler)