66 lines
1.2 KiB
Go
66 lines
1.2 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"embed"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
//go:embed migrations/*.sql
|
|
var migrationsFS embed.FS
|
|
|
|
type DB struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func Connect(ctx context.Context, databaseURL string) (*DB, error) {
|
|
cfg, err := pgxpool.ParseConfig(databaseURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse database url: %w", err)
|
|
}
|
|
cfg.MaxConns = 10
|
|
cfg.MinConns = 1
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("connect postgres: %w", err)
|
|
}
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
|
|
defer cancel()
|
|
if err := pool.Ping(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, fmt.Errorf("ping postgres: %w", err)
|
|
}
|
|
|
|
d := &DB{pool: pool}
|
|
if err := d.migrate(ctx); err != nil {
|
|
pool.Close()
|
|
return nil, err
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func (d *DB) Close() {
|
|
d.pool.Close()
|
|
}
|
|
|
|
func (d *DB) migrate(ctx context.Context) error {
|
|
data, err := migrationsFS.ReadFile("migrations/001_init.sql")
|
|
if err != nil {
|
|
return fmt.Errorf("read migration: %w", err)
|
|
}
|
|
_, err = d.pool.Exec(ctx, string(data))
|
|
if err != nil {
|
|
return fmt.Errorf("apply migration: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (d *DB) Pool() *pgxpool.Pool {
|
|
return d.pool
|
|
}
|