stillbox/pkg/gordio/database/database.go

60 lines
1.2 KiB
Go
Raw Normal View History

2024-07-15 10:12:53 -04:00
package database
import (
"context"
"errors"
"strings"
"dynatron.me/x/stillbox/pkg/gordio/config"
sqlembed "dynatron.me/x/stillbox/sql"
"github.com/golang-migrate/migrate/v4"
_ "github.com/golang-migrate/migrate/v4/database/pgx/v5"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jackc/pgx/v5/pgxpool"
)
type Conn = *pgxpool.Pool
func NewClient(conf config.DB) (Conn, error) {
dir, err := iofs.New(sqlembed.Migrations, "postgres/migrations")
if err != nil {
return nil, err
}
m, err := migrate.NewWithSourceInstance("iofs", dir, strings.Replace(conf.Connect, "postgres://", "pgx5://", 1)) // yech
if err != nil {
return nil, err
}
err = m.Up()
if err != nil && !errors.Is(err, migrate.ErrNoChange) {
return nil, err
}
m.Close()
db, err := pgxpool.New(context.Background(), conf.Connect)
if err != nil {
return nil, err
}
return db, nil
}
type DBCtxKey string
const DBCTXKeyValue DBCtxKey = "dbctx"
func FromCtx(ctx context.Context) Conn {
c, ok := ctx.Value(DBCTXKeyValue).(Conn)
2024-07-15 10:12:53 -04:00
if !ok {
panic("no DB in context")
}
return c
}
func CtxWithDB(ctx context.Context, conn Conn) context.Context {
2024-07-15 10:12:53 -04:00
return context.WithValue(ctx, DBCTXKeyValue, conn)
}