Files
query-orchestration/internal/serviceconfig/database/pool.go
T
Jay Brown 15adaebfcd Merged in feature/serviceconfig-integration (pull request #38)
DRAFT PR : WIP working through ideas for integration

* movearound

* attempttwo

* openapi

* further sanding

* fix

* start on tests

* runthroughsingleconfig

* somechanges

* reflectissue

* removeerrs

* mostlyremovepanic

* removeenv

* noncfgtests

* go

* repo

* fix service config test

* add PWD to all

* test fix

* fix lint

* todo for later

* passingunittests

* alltests

* testlogger

* testloggername

* clean
2025-01-31 13:43:55 +00:00

74 lines
1.3 KiB
Go

package database
import (
"context"
"errors"
"fmt"
"queryorchestration/internal/database/repository"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)
type Pool interface {
repository.DBTX
Begin(ctx context.Context) (pgx.Tx, error)
Ping(ctx context.Context) error
}
func (b *BaseConfig) GetDBPool() Pool {
return b.DBPool
}
func (b *BaseConfig) GetDBQueries() *repository.Queries {
return b.DBQueries
}
func (b *BaseConfig) SetDBPoolConfig() error {
config, err := pgxpool.ParseConfig(b.GetDBURI())
if err != nil {
return err
}
b.DBEnumTypes = []string{
"querytype",
}
config.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
for _, enumName := range b.DBEnumTypes {
dt, err := conn.LoadType(ctx, enumName)
if err != nil {
return fmt.Errorf("failed to load suitable enum type: %w", err)
}
conn.TypeMap().RegisterType(dt)
}
return nil
}
b.DBPoolConfig = config
return nil
}
func (b *BaseConfig) SetDBPool(ctx context.Context) error {
err := b.SetDBPoolConfig()
if err != nil {
return err
}
pool, err := pgxpool.NewWithConfig(ctx, b.DBPoolConfig)
if err != nil {
return err
}
err = pool.Ping(ctx)
if err != nil {
return errors.New("unable to ping database")
}
b.DBPool = pool
b.DBQueries = repository.New(b.DBPool)
return nil
}