From 122bf0e4cf5e29c44e825dbf2aef6cbf363eb82d Mon Sep 17 00:00:00 2001 From: William Perron Date: Fri, 5 Jan 2024 17:36:23 -0500 Subject: [PATCH] tmp --- conflicts.go | 80 ++++++++++++++----- conflicts_test.go | 44 +++++----- go.mod | 2 +- ...0105153349_create_claimables_view.down.sql | 1 + ...40105153349_create_claimables_view.up.sql} | 2 +- store.go | 11 +-- uptime_darwin.go | 3 +- 7 files changed, 92 insertions(+), 51 deletions(-) create mode 100644 migrations/20240105153349_create_claimables_view.down.sql rename migrations/{test.sql => 20240105153349_create_claimables_view.up.sql} (94%) diff --git a/conflicts.go b/conflicts.go index e0036f8..26ba578 100644 --- a/conflicts.go +++ b/conflicts.go @@ -2,6 +2,7 @@ package themis import ( "context" + "database/sql" "fmt" "github.com/rs/zerolog/log" @@ -19,34 +20,71 @@ func (c Conflict) String() string { return fmt.Sprintf("%s owned by #%d %s %s (%s)", c.Province, c.ClaimID, c.ClaimType, c.Claim, c.Player) } -const conflictQuery string = `SELECT name, player, claim_type, val, id FROM ( - SELECT provinces.name, claims.player, claims.claim_type, claims.val, claims.id - FROM claims - LEFT JOIN provinces ON claims.val = provinces.trade_node - WHERE claims.claim_type = 'trade' AND claims.userid IS NOT ? - AND provinces.%[1]s = ? - UNION - SELECT provinces.name, claims.player, claims.claim_type, claims.val, claims.id - FROM claims - LEFT JOIN provinces ON claims.val = provinces.region - WHERE claims.claim_type = 'region' AND claims.userid IS NOT ? - AND provinces.%[1]s = ? - UNION - SELECT provinces.name, claims.player, claims.claim_type, claims.val, claims.id - FROM claims - LEFT JOIN provinces ON claims.val = provinces.area - WHERE claims.claim_type = 'area' AND claims.userid IS NOT ? - AND provinces.%[1]s = ? -);` +// const conflictQuery string = `SELECT name, player, claim_type, val, id FROM ( +// SELECT provinces.name, claims.player, claims.claim_type, claims.val, claims.id +// FROM claims +// LEFT JOIN provinces ON claims.val = provinces.trade_node +// WHERE claims.claim_type = 'trade' AND claims.userid IS NOT ? +// AND provinces.%[1]s = ? +// UNION +// SELECT provinces.name, claims.player, claims.claim_type, claims.val, claims.id +// FROM claims +// LEFT JOIN provinces ON claims.val = provinces.region +// WHERE claims.claim_type = 'region' AND claims.userid IS NOT ? +// AND provinces.%[1]s = ? +// UNION +// SELECT provinces.name, claims.player, claims.claim_type, claims.val, claims.id +// FROM claims +// LEFT JOIN provinces ON claims.val = provinces.area +// WHERE claims.claim_type = 'area' AND claims.userid IS NOT ? +// AND provinces.%[1]s = ? +// );` + +const conflictQuery string = ` +WITH claiming AS ( + SELECT province FROM claimables + WHERE typ = ? + AND name = ? +) +SELECT claimables.province, claims.player, claims.claim_type, claims.val, claims.id +FROM claims +INNER JOIN claimables + ON claims.claim_type = claimables.typ + AND claims.val = claimables.name +INNER JOIN claiming + ON claiming.province = claimables.province +WHERE claims.userid IS NOT ?` func (s *Store) FindConflicts(ctx context.Context, userId, name string, claimType ClaimType) ([]Conflict, error) { log.Debug().Ctx(ctx).Stringer("claim_type", claimType).Str("userid", userId).Msg("searching for potential conflicts") - stmt, err := s.db.PrepareContext(ctx, fmt.Sprintf(conflictQuery, claimTypeToColumn[claimType])) + stmt, err := s.db.PrepareContext(ctx, conflictQuery) if err != nil { return nil, fmt.Errorf("failed to prepare conflicts query: %w", err) } - rows, err := stmt.QueryContext(ctx, userId, name, userId, name, userId, name) + // ========================================================================= + rows, err := s.db.QueryContext(ctx, `select claims.val, claims.claim_type, claims.player, claimables.province +from claims +inner join claimables on claims.val = claimables.name and claims.claim_type = claimables.typ; + `) + if err != nil { + panic(err) + } + + res2 := make([]string, 0) + for rows.Next() { + var name, typ, play string + var pro sql.NullString + if err := rows.Scan(&name, &typ, &play, &pro); err != nil { + panic(err) + } + res2 = append(res2, fmt.Sprintf("%s, %s, %s, %s", name, typ, play, pro.String)) + } + + log.Debug().Strs("list", res2).Msg("existing claims") + // ========================================================================= + + rows, err = stmt.QueryContext(ctx, claimTypeToColumn[claimType], name, userId) if err != nil { return nil, fmt.Errorf("failed to get conflicting provinces: %w", err) } diff --git a/conflicts_test.go b/conflicts_test.go index 2ecab23..d79e501 100644 --- a/conflicts_test.go +++ b/conflicts_test.go @@ -28,17 +28,17 @@ func TestStore_FindConflicts(t *testing.T) { want []Conflict wantErr bool }{ - { - name: "same-player", - args: args{ - context.TODO(), - "000000000000000001", - "France", - CLAIM_TYPE_REGION, - }, - want: []Conflict{}, - wantErr: false, - }, + // { + // name: "same-player", + // args: args{ + // context.TODO(), + // "000000000000000001", + // "France", + // CLAIM_TYPE_REGION, + // }, + // want: []Conflict{}, + // wantErr: false, + // }, { name: "overlapping", args: args{ @@ -54,17 +54,17 @@ func TestStore_FindConflicts(t *testing.T) { }, wantErr: false, }, - { - name: "no-overlap", - args: args{ - context.TODO(), - "000000000000000002", - "Scandinavia", - CLAIM_TYPE_REGION, - }, - want: []Conflict{}, - wantErr: false, - }, + // { + // name: "no-overlap", + // args: args{ + // context.TODO(), + // "000000000000000002", + // "Scandinavia", + // CLAIM_TYPE_REGION, + // }, + // want: []Conflict{}, + // wantErr: false, + // }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/go.mod b/go.mod index e6522d9..2de0931 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module go.wperron.io/themis -go 1.21 +go 1.19 require ( github.com/bwmarrin/discordgo v0.26.1 diff --git a/migrations/20240105153349_create_claimables_view.down.sql b/migrations/20240105153349_create_claimables_view.down.sql new file mode 100644 index 0000000..9bfe924 --- /dev/null +++ b/migrations/20240105153349_create_claimables_view.down.sql @@ -0,0 +1 @@ +drop claimables; diff --git a/migrations/test.sql b/migrations/20240105153349_create_claimables_view.up.sql similarity index 94% rename from migrations/test.sql rename to migrations/20240105153349_create_claimables_view.up.sql index 863c3db..8535166 100644 --- a/migrations/test.sql +++ b/migrations/20240105153349_create_claimables_view.up.sql @@ -1,4 +1,4 @@ -create view if not exists prov_by_type as +create view if not exists claimables as with trades as (select distinct trade_node from provinces where trade_node != ''), areas as (select distinct area from provinces where area != ''), diff --git a/store.go b/store.go index 9802858..2539afd 100644 --- a/store.go +++ b/store.go @@ -45,16 +45,17 @@ func NewStore(conn string) (*Store, error) { return nil, fmt.Errorf("failed to initialize db migrate: %w", err) } + err = m.Up() + if err != nil && !errors.Is(err, migrate.ErrNoChange) { + return nil, fmt.Errorf("failed to roll up migrations: %w", err) + } + ver, dirty, err := m.Version() - if err != nil { + if err != nil && err != migrate.ErrNilVersion { return nil, fmt.Errorf("failed to get database migration version: %w", err) } log.Debug().Uint("current_version", ver).Bool("dirty", dirty).Msg("running database migrations") - err = m.Up() - if err != nil && !errors.Is(err, migrate.ErrNoChange) { - return nil, fmt.Errorf("failed to roll up migrations: %w", err) - } return &Store{ db: db, diff --git a/uptime_darwin.go b/uptime_darwin.go index 29fbd0e..3665cd3 100644 --- a/uptime_darwin.go +++ b/uptime_darwin.go @@ -3,10 +3,11 @@ package themis import ( + "context" "time" ) // Uptime returns the time elapsed since the start of the current process ID. -func Uptime() (time.Duration, error) { +func Uptime(ctx context.Context) (time.Duration, error) { return 0, nil }