-
Notifications
You must be signed in to change notification settings - Fork 0
feat(database): add roster and analytics schema #27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MagicTheDev
wants to merge
10
commits into
main
Choose a base branch
from
feat/personal-armies-contract
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
dcba23e
feat: add personal army library schema
MagicTheDev 8ab2642
feat(database): add roster and analytics migrations
MagicTheDev 935d26d
fix(database): constrain daily pet and siege counts
MagicTheDev ff23f25
feat(database): reserve roster publication nonces
MagicTheDev 3e4c66b
fix(database): enforce exclusive legend daily usage
MagicTheDev 9f57a89
fix(database): bound exclusive legend triples
MagicTheDev a07172d
fix(database): bound item triples and index army references
MagicTheDev 465ef44
fix(database): validate CWL town hall distribution
MagicTheDev a671f09
fix(database): require selected siege IDs
MagicTheDev c5fc3ec
fix(database): validate CWL hit-rate observations
MagicTheDev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
128 changes: 128 additions & 0 deletions
128
database/migrations/cwl_participation_constraints_test.go
This file contains hidden or 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,128 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/jackc/pgx/v5" | ||
| ) | ||
|
|
||
| func TestCWLParticipationTownHalls(t *testing.T) { | ||
| if os.Getenv("CLASHKING_DISPOSABLE_TIMESCALE") != "1" || os.Getenv("TEST_DATABASE_URL") == "" { | ||
| t.Skip("requires disposable authoritative Goose schema") | ||
| } | ||
| conn, err := pgx.Connect(t.Context(), os.Getenv("TEST_DATABASE_URL")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer conn.Close(context.Background()) | ||
| for _, tc := range []struct { | ||
| name, value string | ||
| want bool | ||
| }{ | ||
| {"empty distribution", `[]`, true}, | ||
| {"ordered distribution", `[{"level":18,"count":4},{"level":17,"count":2}]`, true}, | ||
| {"null entry", `[null]`, false}, | ||
| {"string level", `[{"level":"17","count":2}]`, false}, | ||
| {"negative count", `[{"level":17,"count":-1}]`, false}, | ||
| {"duplicate level", `[{"level":17,"count":1},{"level":17,"count":2}]`, false}, | ||
| {"ascending levels", `[{"level":17,"count":1},{"level":18,"count":2}]`, false}, | ||
| {"unknown key", `[{"level":17,"count":1,"other":true}]`, false}, | ||
| {"out of range level", `[{"level":21,"count":1}]`, false}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var got bool | ||
| if err := conn.QueryRow(t.Context(), "SELECT public.cwl_participation_town_halls_valid($1::jsonb)", tc.value).Scan(&got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != tc.want { | ||
| t.Fatalf("validator returned %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| for _, tc := range []struct { | ||
| name, value string | ||
| wantErr bool | ||
| }{ | ||
| {"valid row", `[{"level":18,"count":4}]`, false}, | ||
| {"malformed row", `[{"level":"18","count":4}]`, true}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| tx, err := conn.Begin(t.Context()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer tx.Rollback(context.Background()) | ||
| _, err = tx.Exec(t.Context(), `INSERT INTO public.cwl_participation | ||
| (season,cwl_league_id,war_size,group_count,clan_count,registered_player_count,townhall_counts,finalized_wars,archived_wars) | ||
| VALUES ('2026-09',48000001,15,1,1,1,$1::jsonb,0,0)`, tc.value) | ||
| if (err != nil) != tc.wantErr { | ||
| t.Fatalf("insert error = %v, want error = %v", err, tc.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCWLParticipationHitRates(t *testing.T) { | ||
| if os.Getenv("CLASHKING_DISPOSABLE_TIMESCALE") != "1" || os.Getenv("TEST_DATABASE_URL") == "" { | ||
| t.Skip("requires disposable authoritative Goose schema") | ||
| } | ||
| conn, err := pgx.Connect(t.Context(), os.Getenv("TEST_DATABASE_URL")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer conn.Close(context.Background()) | ||
| for _, tc := range []struct { | ||
| name, value string | ||
| want bool | ||
| }{ | ||
| {"empty observations", `[]`, true}, | ||
| {"descending observations", `[{"level":18,"attacks":4,"three_stars":2},{"level":17,"attacks":3,"three_stars":1}]`, true}, | ||
| {"null entry", `[null]`, false}, | ||
| {"missing fields", `[{}]`, false}, | ||
| {"string level", `[{"level":"18","attacks":1,"three_stars":0}]`, false}, | ||
| {"negative attempts", `[{"level":18,"attacks":-1,"three_stars":0}]`, false}, | ||
| {"triples over attempts", `[{"level":18,"attacks":1,"three_stars":2}]`, false}, | ||
| {"duplicate level", `[{"level":18,"attacks":1,"three_stars":0},{"level":18,"attacks":1,"three_stars":0}]`, false}, | ||
| {"ascending levels", `[{"level":17,"attacks":1,"three_stars":0},{"level":18,"attacks":1,"three_stars":0}]`, false}, | ||
| {"unknown key", `[{"level":18,"attacks":1,"three_stars":0,"other":true}]`, false}, | ||
| {"out of range level", `[{"level":21,"attacks":1,"three_stars":0}]`, false}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var got bool | ||
| if err := conn.QueryRow(t.Context(), "SELECT public.cwl_participation_hitrates_valid($1::jsonb)", tc.value).Scan(&got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != tc.want { | ||
| t.Fatalf("validator returned %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| for _, tc := range []struct { | ||
| name, value string | ||
| wantErr bool | ||
| }{ | ||
| {"SQL null observations", "", false}, | ||
| {"valid row", `[{"level":18,"attacks":1,"three_stars":1}]`, false}, | ||
| {"malformed row", `[{}]`, true}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| tx, err := conn.Begin(t.Context()) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer tx.Rollback(context.Background()) | ||
| var input any = tc.value | ||
| if tc.name == "SQL null observations" { | ||
| input = nil | ||
| } | ||
| _, err = tx.Exec(t.Context(), `INSERT INTO public.cwl_participation | ||
| (season,cwl_league_id,war_size,group_count,clan_count,registered_player_count,townhall_counts,same_th_hitrates,finalized_wars,archived_wars) | ||
| VALUES ('2026-09',48000001,15,1,1,1,'[]',$1::jsonb,0,0)`, input) | ||
| if (err != nil) != tc.wantErr { | ||
| t.Fatalf("insert error = %v, want error = %v", err, tc.wantErr) | ||
| } | ||
| }) | ||
| } | ||
| } |
104 changes: 104 additions & 0 deletions
104
database/migrations/daily_analytics_constraints_test.go
This file contains hidden or 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,104 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "os" | ||
| "testing" | ||
|
|
||
| "github.com/jackc/pgx/v5" | ||
| ) | ||
|
|
||
| func TestDailyAnalyticsUsageConstraints(t *testing.T) { | ||
| if os.Getenv("CLASHKING_DISPOSABLE_TIMESCALE") != "1" || os.Getenv("TEST_DATABASE_URL") == "" { | ||
| t.Skip("requires disposable authoritative Goose schema") | ||
| } | ||
| conn, err := pgx.Connect(t.Context(), os.Getenv("TEST_DATABASE_URL")) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| defer conn.Close(context.Background()) | ||
| for _, tc := range []struct { | ||
| name, value string | ||
| want bool | ||
| }{ | ||
| {"valid selected siege", `[{"id":1,"uses":1,"triples":0}]`, true}, | ||
| {"zero selected siege", `[{"id":0,"uses":1,"triples":0}]`, false}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var got bool | ||
| if err := conn.QueryRow(t.Context(), "SELECT public.legend_selected_siege_ids_valid($1::jsonb)", tc.value).Scan(&got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != tc.want { | ||
| t.Fatalf("validator returned %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| for _, tc := range []struct { | ||
| name, function, value string | ||
| want bool | ||
| }{ | ||
| {"valid pet combinations", "pet_combo_usage_triples_within_attack_count", `[{"petIds":[1,2],"uses":2,"triples":1},{"petIds":[2,3],"uses":3,"triples":2}]`, true}, | ||
| {"duplicate pet combination", "pet_combo_usage_triples_within_attack_count", `[{"petIds":[1,2],"uses":2,"triples":1},{"petIds":[1,2],"uses":2,"triples":1}]`, false}, | ||
| {"unordered pet combinations", "pet_combo_usage_triples_within_attack_count", `[{"petIds":[2,3],"uses":1,"triples":0},{"petIds":[1,2],"uses":1,"triples":0}]`, false}, | ||
| {"overcounted pet combinations", "pet_combo_usage_triples_within_attack_count", `[{"petIds":[1],"uses":3,"triples":0},{"petIds":[2],"uses":3,"triples":0}]`, false}, | ||
| {"valid siege usage", "army_setup_siege_usage_within_attack_count", `[{"id":1,"attacks":2},{"id":2,"attacks":3}]`, true}, | ||
| {"malformed siege usage", "army_setup_siege_usage_within_attack_count", `[{"id":1,"attacks":"2"}]`, false}, | ||
| {"duplicate siege ID", "army_setup_siege_usage_within_attack_count", `[{"id":1,"attacks":2},{"id":1,"attacks":1}]`, false}, | ||
| {"unordered siege IDs", "army_setup_siege_usage_within_attack_count", `[{"id":2,"attacks":1},{"id":1,"attacks":1}]`, false}, | ||
| {"overcounted siege usage", "army_setup_siege_usage_within_attack_count", `[{"id":1,"attacks":3},{"id":2,"attacks":3}]`, false}, | ||
| {"valid Legend siege usage", "legend_siege_usage_within_attack_count", `[{"id":1,"uses":2,"triples":1},{"id":2,"uses":3,"triples":0}]`, true}, | ||
| {"overcounted Legend siege usage", "legend_siege_usage_within_attack_count", `[{"id":1,"uses":3,"triples":0},{"id":2,"uses":3,"triples":0}]`, false}, | ||
| {"valid equipment pairs for multiple heroes", "equipment_pair_usage_triples_within_attack_count", `[{"heroId":1,"equipmentIds":[1,2],"uses":3,"triples":0},{"heroId":1,"equipmentIds":[1,3],"uses":2,"triples":0},{"heroId":2,"equipmentIds":[1,2],"uses":5,"triples":0}]`, true}, | ||
| {"overcounted equipment pairs for one hero", "equipment_pair_usage_triples_within_attack_count", `[{"heroId":1,"equipmentIds":[1,2],"uses":3,"triples":0},{"heroId":1,"equipmentIds":[1,3],"uses":3,"triples":0}]`, false}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var got bool | ||
| query := "SELECT public." + tc.function + "($1::jsonb, 5::bigint)" | ||
| if err := conn.QueryRow(t.Context(), query, tc.value).Scan(&got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != tc.want { | ||
| t.Fatalf("validator returned %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| for _, tc := range []struct { | ||
| name, value string | ||
| perHero bool | ||
| want bool | ||
| }{ | ||
| {"pet combos bounded by three stars", `[{"petIds":[1],"uses":1,"triples":1},{"petIds":[2],"uses":1,"triples":1}]`, false, true}, | ||
| {"pet combos exceed three stars", `[{"petIds":[1],"uses":3,"triples":3}]`, false, false}, | ||
| {"siege uses exceed three stars", `[{"id":1,"uses":1,"triples":1},{"id":2,"uses":1,"triples":2}]`, false, false}, | ||
| {"equipment pairs bounded per hero", `[{"heroId":1,"equipmentIds":[1,2],"uses":2,"triples":2},{"heroId":2,"equipmentIds":[1,2],"uses":2,"triples":2}]`, true, true}, | ||
| {"equipment pairs exceed three stars for hero", `[{"heroId":1,"equipmentIds":[1,2],"uses":2,"triples":2},{"heroId":1,"equipmentIds":[1,3],"uses":1,"triples":1}]`, true, false}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var got bool | ||
| if err := conn.QueryRow(t.Context(), "SELECT public.legend_exclusive_triples_within_star_count($1::jsonb, 2::bigint, $2::boolean)", tc.value, tc.perHero).Scan(&got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != tc.want { | ||
| t.Fatalf("validator returned %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| for _, tc := range []struct { | ||
| name, value string | ||
| want bool | ||
| }{ | ||
| {"valid item triples for co-occurring items", `[{"id":1,"uses":3,"triples":2},{"id":2,"uses":3,"triples":2}]`, true}, | ||
| {"item triples exceed three stars", `[{"id":1,"uses":3,"triples":3}]`, false}, | ||
| } { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| var got bool | ||
| if err := conn.QueryRow(t.Context(), "SELECT public.legend_item_triples_within_star_count($1::jsonb, 2::bigint)", tc.value).Scan(&got); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if got != tc.want { | ||
| t.Fatalf("validator returned %v, want %v", got, tc.want) | ||
| } | ||
| }) | ||
| } | ||
| } | ||
This file contains hidden or 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,36 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestLegendDailyMetadataMigrationIsAdditive(t *testing.T) { | ||
| raw, err := os.ReadFile("../timescale/030_legend_daily_metadata.sql") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| sql := string(raw) | ||
| for _, expected := range []string{ | ||
| "'top_200','top_100'", | ||
| "ADD COLUMN troop_stats jsonb NOT NULL DEFAULT '[]'::jsonb", | ||
| "ADD COLUMN spell_stats jsonb NOT NULL DEFAULT '[]'::jsonb", | ||
| "ADD COLUMN siege_stats jsonb NOT NULL DEFAULT '[]'::jsonb", | ||
| "ADD COLUMN equipment_pair_stats jsonb NOT NULL DEFAULT '[]'::jsonb", | ||
| "equipment_pair_usage_triples_within_attack_count", | ||
| } { | ||
| if !strings.Contains(sql, expected) { | ||
| t.Fatalf("migration is missing %q", expected) | ||
| } | ||
| } | ||
| petRaw, err := os.ReadFile("../timescale/031_legend_daily_pet_combos.sql") | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| for _, expected := range []string{"ADD COLUMN pet_combo_stats jsonb NOT NULL DEFAULT '[]'::jsonb", "pet_combo_usage_triples_within_attack_count"} { | ||
| if !strings.Contains(string(petRaw), expected) { | ||
| t.Fatalf("migration 031 is missing %q", expected) | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the checked workflows,
schema-upgrade.ymlinvokes only the baseline-006, baseline-013, and baseline-018 profiles, whilevalidate-repository.shrunsgo testwithout a disposable database, so this new analytics test always skips in CI;TestPersonalArmyStorageMigrationlikewise always skips because it requires baseline-021. Add explicit retained-api and baseline-021 workflow invocations so the new constraint logic and populated 021-to-022 upgrade are actually exercised.Useful? React with 👍 / 👎.