|
|
|
@ -13,7 +13,7 @@ import (
|
|
|
|
|
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
|
|
|
|
"github.com/golang-migrate/migrate/v4/source/iofs"
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
|
"github.com/rs/zerolog"
|
|
|
|
|
"go.opentelemetry.io/otel"
|
|
|
|
|
"go.opentelemetry.io/otel/attribute"
|
|
|
|
|
"go.opentelemetry.io/otel/codes"
|
|
|
|
@ -32,10 +32,11 @@ func init() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
if err != nil {
|
|
|
|
|
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)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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{
|
|
|
|
|
db: db,
|
|
|
|
|
logger: logger,
|
|
|
|
|
db: db,
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *Store) Close() error {
|
|
|
|
|
log.Debug().Msg("closing database")
|
|
|
|
|
s.logger.Debug().Msg("closing database")
|
|
|
|
|
return s.db.Close()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -84,7 +86,7 @@ func (s *Store) Claim(ctx context.Context, userId, player, province string, clai
|
|
|
|
|
))
|
|
|
|
|
defer span.End()
|
|
|
|
|
|
|
|
|
|
log.Debug().
|
|
|
|
|
s.logger.Debug().
|
|
|
|
|
Ctx(ctx).
|
|
|
|
|
Str("userid", userId).
|
|
|
|
|
Str("player", player).
|
|
|
|
@ -115,7 +117,7 @@ func (s *Store) Claim(ctx context.Context, userId, player, province string, clai
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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")
|
|
|
|
|
return 0, ErrConflict{Conflicts: conflicts}
|
|
|
|
|
}
|
|
|
|
@ -185,7 +187,7 @@ func (s *Store) ListAvailability(ctx context.Context, claimType ClaimType, searc
|
|
|
|
|
))
|
|
|
|
|
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)}
|
|
|
|
|
|
|
|
|
|
queryPattern := `SELECT distinct name
|
|
|
|
@ -237,7 +239,7 @@ func (s *Store) ListClaims(ctx context.Context) ([]Claim, error) {
|
|
|
|
|
))
|
|
|
|
|
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`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
span.RecordError(err)
|
|
|
|
@ -300,7 +302,7 @@ func (s *Store) DescribeClaim(ctx context.Context, ID int) (ClaimDetail, error)
|
|
|
|
|
))
|
|
|
|
|
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 = ?`)
|
|
|
|
|
if err != nil {
|
|
|
|
|
span.RecordError(err)
|
|
|
|
@ -374,7 +376,7 @@ func (s *Store) DeleteClaim(ctx context.Context, ID int, userId string) error {
|
|
|
|
|
))
|
|
|
|
|
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{
|
|
|
|
|
userId: userId,
|
|
|
|
|
eventType: EventUnclaim,
|
|
|
|
@ -421,7 +423,7 @@ func (s *Store) CountClaims(ctx context.Context) (total, uniquePlayers int, err
|
|
|
|
|
))
|
|
|
|
|
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")
|
|
|
|
|
if err != nil {
|
|
|
|
|
span.RecordError(err)
|
|
|
|
@ -449,7 +451,7 @@ func (s *Store) Flush(ctx context.Context, userId string) error {
|
|
|
|
|
))
|
|
|
|
|
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{
|
|
|
|
|
userId: userId,
|
|
|
|
|
eventType: EventFlush,
|
|
|
|
|