diff --git a/.vscode/settings.json b/.vscode/settings.json index 3ae11c7..5aebdea 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,7 @@ { "conventionalCommits.scopes": [ "config", - "api" + "api", + "test" ] } diff --git a/internal/app/migrations/psql/struct_updater.go b/internal/app/migrations/psql/struct_updater.go index 39ba81b..301d3d8 100644 --- a/internal/app/migrations/psql/struct_updater.go +++ b/internal/app/migrations/psql/struct_updater.go @@ -50,6 +50,7 @@ func RegisterAllUpdates() map[string][]DatabaseUpdateFunc { allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0019_update0020) allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0020_update0020testdata) allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0020_update0021) + allUpdates = RegisterDatabaseUpdate(allUpdates, DatabaseUpdate_update0021_update0022) return allUpdates } diff --git a/internal/app/migrations/psql/update0021_update0022.go b/internal/app/migrations/psql/update0021_update0022.go new file mode 100644 index 0000000..927460c --- /dev/null +++ b/internal/app/migrations/psql/update0021_update0022.go @@ -0,0 +1,33 @@ +package migration + +import ( + "database/sql" + "log/slog" + "runtime" +) + +func DatabaseUpdate_update0021_update0022(db *sql.DB, getInfo bool) (string, string, string, error) { + // WARNING!!! + // Do not change the update if it has already been installed by other developers or in production. + // To correct the database, create a new update and register it in the list of updates. + + fromUpdateId, toUpdateId := ParseNameFuncUpdate(runtime.Caller(0)) + description := "Add cascade delete for results when deleting games" + if getInfo { + return fromUpdateId, toUpdateId, description, nil + } + + query := ` + BEGIN; + ALTER TABLE results DROP CONSTRAINT IF EXISTS results_game_id_fkey; + ALTER TABLE results ADD CONSTRAINT results_game_id_fkey + FOREIGN KEY (game_id) REFERENCES games(id) ON DELETE CASCADE; + COMMIT; + ` + _, err := db.Exec(query) + if err != nil { + slog.Error("Problem with select, query: " + query + "\\n error:" + err.Error()) + return fromUpdateId, toUpdateId, description, err + } + return fromUpdateId, toUpdateId, description, nil +} diff --git a/test/server_integration_test.go b/test/server_integration_test.go index 067884b..f0d5b7f 100644 --- a/test/server_integration_test.go +++ b/test/server_integration_test.go @@ -592,10 +592,15 @@ func TestGameCRUD(t *testing.T) { t.Fatalf("expected at least one game") } - lastGame := games[len(games)-1] - gameID = lastGame["id"].(string) - if gameID == "" { - t.Fatalf("expected game ID in response") + gameExists := false + for _, game := range games { + if game["id"].(string) == gameID { + gameExists = true + break + } + } + if !gameExists { + t.Fatalf("expected game ID %v in the list of games, but it was not found", gameID) } })