Some checks failed
Deploy to NAS / deploy (push) Failing after 4s
Remove submodule tracking; backend is now a plain directory in the repo. Also update deploy workflow: remove --recurse-submodules. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
42 lines
910 B
Go
42 lines
910 B
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
_ "embed"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
//go:embed schema.sql
|
|
var schema string
|
|
|
|
func NewPool(ctx context.Context, dsn string) (*pgxpool.Pool, error) {
|
|
cfg, err := pgxpool.ParseConfig(dsn)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("parse dsn: %w", err)
|
|
}
|
|
cfg.MaxConns = 25
|
|
cfg.MinConns = 2
|
|
cfg.MaxConnLifetime = 15 * time.Minute
|
|
cfg.MaxConnIdleTime = 5 * time.Minute
|
|
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("create pool: %w", err)
|
|
}
|
|
if err := pool.Ping(ctx); err != nil {
|
|
return nil, fmt.Errorf("ping db: %w", err)
|
|
}
|
|
return pool, nil
|
|
}
|
|
|
|
// InitSchema applies the embedded schema.sql (idempotent via IF NOT EXISTS).
|
|
func InitSchema(ctx context.Context, pool *pgxpool.Pool) error {
|
|
if _, err := pool.Exec(ctx, schema); err != nil {
|
|
return fmt.Errorf("init schema: %w", err)
|
|
}
|
|
return nil
|
|
}
|