|
|
@ -13,7 +13,7 @@ import (
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
|
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
|
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
@ -32,10 +32,11 @@ func init() {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type Store struct {
|
|
|
|
type Store struct {
|
|
|
|
db *sql.DB
|
|
|
|
db *sql.DB
|
|
|
|
|
|
|
|
logger zerolog.Logger
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewStore(db *sql.DB) (*Store, error) {
|
|
|
|
func NewStore(db *sql.DB, logger zerolog.Logger) (*Store, error) {
|
|
|
|
d, err := iofs.New(migrations, "migrations")
|
|
|
|
d, err := iofs.New(migrations, "migrations")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open iofs migration source: %w", err)
|
|
|
|
return nil, fmt.Errorf("failed to open iofs migration source: %w", err)
|
|
|
@ -61,15 +62,16 @@ func NewStore(db *sql.DB) (*Store, error) {
|
|
|
|
return nil, fmt.Errorf("failed to get database migration version: %w", err)
|
|
|
|
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")
|
|
|
|
logger.Debug().Uint("current_version", ver).Bool("dirty", dirty).Msg("running database migrations")
|
|
|
|
|
|
|
|
|
|
|
|
return &Store{
|
|
|
|
return &Store{
|
|
|
|
db: db,
|
|
|
|
logger: logger,
|
|
|
|
|
|
|
|
db: db,
|
|
|
|
}, nil
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Store) Close() error {
|
|
|
|
func (s *Store) Close() error {
|
|
|
|
log.Debug().Msg("closing database")
|
|
|
|
s.logger.Debug().Msg("closing database")
|
|
|
|
return s.db.Close()
|
|
|
|
return s.db.Close()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -84,7 +86,7 @@ func (s *Store) Claim(ctx context.Context, userId, player, province string, clai
|
|
|
|
))
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().
|
|
|
|
s.logger.Debug().
|
|
|
|
Ctx(ctx).
|
|
|
|
Ctx(ctx).
|
|
|
|
Str("userid", userId).
|
|
|
|
Str("userid", userId).
|
|
|
|
Str("player", player).
|
|
|
|
Str("player", player).
|
|
|
@ -115,7 +117,7 @@ func (s *Store) Claim(ctx context.Context, userId, player, province string, clai
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(conflicts) > 0 {
|
|
|
|
if len(conflicts) > 0 {
|
|
|
|
log.Debug().Ctx(ctx).Int("len", len(conflicts)).Msg("found conflicts")
|
|
|
|
s.logger.Debug().Ctx(ctx).Int("len", len(conflicts)).Msg("found conflicts")
|
|
|
|
audit.err = errors.New("found conflicts")
|
|
|
|
audit.err = errors.New("found conflicts")
|
|
|
|
return 0, ErrConflict{Conflicts: conflicts}
|
|
|
|
return 0, ErrConflict{Conflicts: conflicts}
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -185,7 +187,7 @@ func (s *Store) ListAvailability(ctx context.Context, claimType ClaimType, searc
|
|
|
|
))
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Ctx(ctx).Stringer("claim_type", claimType).Strs("search_terms", search).Msg("listing available entries")
|
|
|
|
s.logger.Debug().Ctx(ctx).Stringer("claim_type", claimType).Strs("search_terms", search).Msg("listing available entries")
|
|
|
|
queryParams := []any{string(claimType)}
|
|
|
|
queryParams := []any{string(claimType)}
|
|
|
|
|
|
|
|
|
|
|
|
queryPattern := `SELECT distinct name
|
|
|
|
queryPattern := `SELECT distinct name
|
|
|
@ -237,7 +239,7 @@ func (s *Store) ListClaims(ctx context.Context) ([]Claim, error) {
|
|
|
|
))
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Ctx(ctx).Msg("listing all claims currently in database")
|
|
|
|
s.logger.Debug().Ctx(ctx).Msg("listing all claims currently in database")
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, `SELECT id, player, claim_type, val FROM claims`)
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, `SELECT id, player, claim_type, val FROM claims`)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.RecordError(err)
|
|
|
@ -300,7 +302,7 @@ func (s *Store) DescribeClaim(ctx context.Context, ID int) (ClaimDetail, error)
|
|
|
|
))
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Ctx(ctx).Int("id", ID).Msg("describing claim")
|
|
|
|
s.logger.Debug().Ctx(ctx).Int("id", ID).Msg("describing claim")
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, `SELECT id, player, claim_type, val FROM claims WHERE id = ?`)
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, `SELECT id, player, claim_type, val FROM claims WHERE id = ?`)
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.RecordError(err)
|
|
|
@ -374,7 +376,7 @@ func (s *Store) DeleteClaim(ctx context.Context, ID int, userId string) error {
|
|
|
|
))
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Ctx(ctx).Str("userid", userId).Int("id", ID).Msg("deleting claim")
|
|
|
|
s.logger.Debug().Ctx(ctx).Str("userid", userId).Int("id", ID).Msg("deleting claim")
|
|
|
|
audit := &AuditableEvent{
|
|
|
|
audit := &AuditableEvent{
|
|
|
|
userId: userId,
|
|
|
|
userId: userId,
|
|
|
|
eventType: EventUnclaim,
|
|
|
|
eventType: EventUnclaim,
|
|
|
@ -421,7 +423,7 @@ func (s *Store) CountClaims(ctx context.Context) (total, uniquePlayers int, err
|
|
|
|
))
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Ctx(ctx).Msg("counting all claims and unique users")
|
|
|
|
s.logger.Debug().Ctx(ctx).Msg("counting all claims and unique users")
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, "SELECT COUNT(1), COUNT(DISTINCT(userid)) FROM claims")
|
|
|
|
stmt, err := s.db.PrepareContext(ctx, "SELECT COUNT(1), COUNT(DISTINCT(userid)) FROM claims")
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
span.RecordError(err)
|
|
|
|
span.RecordError(err)
|
|
|
@ -449,7 +451,7 @@ func (s *Store) Flush(ctx context.Context, userId string) error {
|
|
|
|
))
|
|
|
|
))
|
|
|
|
defer span.End()
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
|
|
log.Debug().Ctx(ctx).Str("initiated_by", userId).Msg("flushing all currently help claims")
|
|
|
|
s.logger.Debug().Ctx(ctx).Str("initiated_by", userId).Msg("flushing all currently help claims")
|
|
|
|
audit := &AuditableEvent{
|
|
|
|
audit := &AuditableEvent{
|
|
|
|
userId: userId,
|
|
|
|
userId: userId,
|
|
|
|
eventType: EventFlush,
|
|
|
|
eventType: EventFlush,
|
|
|
|