Skip to content

Commit

Permalink
fix(test): 🐛 cascade delete result with games
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorPolyakov committed Aug 12, 2024
1 parent 5e2a32f commit 70457f2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"conventionalCommits.scopes": [
"config",
"api"
"api",
"test"
]
}
1 change: 1 addition & 0 deletions internal/app/migrations/psql/struct_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
33 changes: 33 additions & 0 deletions internal/app/migrations/psql/update0021_update0022.go
Original file line number Diff line number Diff line change
@@ -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
}
13 changes: 9 additions & 4 deletions test/server_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})

Expand Down

0 comments on commit 70457f2

Please sign in to comment.