first commit

This commit is contained in:
2026-02-26 17:52:48 +09:00
commit dabf1f3ba9
49 changed files with 14883 additions and 0 deletions

51
internal/db/postgres.go Normal file
View File

@@ -0,0 +1,51 @@
package db
import (
"context"
"fmt"
"github.com/jackc/pgx/v5/pgxpool"
"a301_game_server/config"
"a301_game_server/pkg/logger"
)
// Pool wraps a pgx connection pool.
type Pool struct {
*pgxpool.Pool
}
// NewPool creates a connection pool to PostgreSQL.
func NewPool(ctx context.Context, cfg *config.DatabaseConfig) (*Pool, error) {
poolCfg, err := pgxpool.ParseConfig(cfg.DSN())
if err != nil {
return nil, fmt.Errorf("parse dsn: %w", err)
}
poolCfg.MaxConns = cfg.MaxConns
poolCfg.MinConns = cfg.MinConns
pool, err := pgxpool.NewWithConfig(ctx, poolCfg)
if err != nil {
return nil, fmt.Errorf("create pool: %w", err)
}
if err := pool.Ping(ctx); err != nil {
pool.Close()
return nil, fmt.Errorf("ping database: %w", err)
}
logger.Info("database connected", "host", cfg.Host, "db", cfg.DBName)
return &Pool{pool}, nil
}
// RunMigrations executes all schema migrations.
func (p *Pool) RunMigrations(ctx context.Context) error {
for i, m := range migrations {
if _, err := p.Exec(ctx, m); err != nil {
return fmt.Errorf("migration %d failed: %w", i, err)
}
}
logger.Info("database migrations completed", "count", len(migrations))
return nil
}