diff --git a/postgresql/connect.go b/postgresql/connect.go index 45bf9e00..8032b06b 100644 --- a/postgresql/connect.go +++ b/postgresql/connect.go @@ -4,22 +4,13 @@ import ( "context" "fmt" - "github.com/jackc/pgx/v5" "github.com/jackc/pgx/v5/pgxpool" ) func PgConnect(databaseUrl string) (*pgxpool.Pool, error) { - dbconfig, err := pgxpool.ParseConfig(databaseUrl) + pgxPool, err := pgxpool.New(context.Background(), databaseUrl) if err != nil { - return nil, err - } - dbconfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { - RegisterUuid7(conn.TypeMap()) - return nil - } - pgxPool, err := pgxpool.NewWithConfig(context.Background(), dbconfig) - if err != nil { - return nil, fmt.Errorf("pgxpool.NewWithConfig(%v): %w", databaseUrl, err) + return nil, fmt.Errorf("pgxpool.New(%v): %w", databaseUrl, err) } return pgxPool, nil } diff --git a/postgresql/lsps2_store.go b/postgresql/lsps2_store.go index 89457b7d..7e702615 100644 --- a/postgresql/lsps2_store.go +++ b/postgresql/lsps2_store.go @@ -112,7 +112,7 @@ func (s *Lsps2Store) GetBuyRegistration(ctx context.Context, scid lightning.Shor WHERE r.scid = $1`, int64(uint64(scid)), ) - var db_id uuid7.UUID + var db_id [16]byte var db_lsp_id string var db_peer_id string var db_scid int64 diff --git a/postgresql/uuid7.go b/postgresql/uuid7.go deleted file mode 100644 index 4a478948..00000000 --- a/postgresql/uuid7.go +++ /dev/null @@ -1,96 +0,0 @@ -package postgresql - -import ( - "fmt" - - "github.com/GoWebProd/uuid7" - "github.com/jackc/pgx/v5/pgtype" -) - -type UUID uuid7.UUID - -func (u *UUID) ScanUUID(v pgtype.UUID) error { - if !v.Valid { - return fmt.Errorf("cannot scan NULL into *uuid.UUID") - } - - *u = v.Bytes - return nil -} - -func (u UUID) UUIDValue() (pgtype.UUID, error) { - return pgtype.UUID{Bytes: [16]byte(u), Valid: true}, nil -} - -func TryWrapUUIDEncodePlan(value interface{}) (plan pgtype.WrappedEncodePlanNextSetter, nextValue interface{}, ok bool) { - switch value := value.(type) { - case uuid7.UUID: - return &wrapUUIDEncodePlan{}, UUID(value), true - } - - return nil, nil, false -} - -type wrapUUIDEncodePlan struct { - next pgtype.EncodePlan -} - -func (plan *wrapUUIDEncodePlan) SetNext(next pgtype.EncodePlan) { plan.next = next } - -func (plan *wrapUUIDEncodePlan) Encode(value interface{}, buf []byte) (newBuf []byte, err error) { - return plan.next.Encode(UUID(value.(uuid7.UUID)), buf) -} - -func TryWrapUUIDScanPlan(target interface{}) (plan pgtype.WrappedScanPlanNextSetter, nextDst interface{}, ok bool) { - switch target := target.(type) { - case *uuid7.UUID: - return &wrapUUIDScanPlan{}, (*UUID)(target), true - } - - return nil, nil, false -} - -type wrapUUIDScanPlan struct { - next pgtype.ScanPlan -} - -func (plan *wrapUUIDScanPlan) SetNext(next pgtype.ScanPlan) { plan.next = next } - -func (plan *wrapUUIDScanPlan) Scan(src []byte, dst interface{}) error { - return plan.next.Scan(src, (*UUID)(dst.(*uuid7.UUID))) -} - -type UUIDCodec struct { - pgtype.UUIDCodec -} - -func (UUIDCodec) DecodeValue(tm *pgtype.Map, oid uint32, format int16, src []byte) (interface{}, error) { - if src == nil { - return nil, nil - } - - var target uuid7.UUID - scanPlan := tm.PlanScan(oid, format, &target) - if scanPlan == nil { - return nil, fmt.Errorf("PlanScan did not find a plan") - } - - err := scanPlan.Scan(src, &target) - if err != nil { - return nil, err - } - - return target, nil -} - -// RegisterUuid7 registers the github.com/GoWebProd/uuid7 integration with a pgtype.Map. -func RegisterUuid7(tm *pgtype.Map) { - tm.TryWrapEncodePlanFuncs = append([]pgtype.TryWrapEncodePlanFunc{TryWrapUUIDEncodePlan}, tm.TryWrapEncodePlanFuncs...) - tm.TryWrapScanPlanFuncs = append([]pgtype.TryWrapScanPlanFunc{TryWrapUUIDScanPlan}, tm.TryWrapScanPlanFuncs...) - - tm.RegisterType(&pgtype.Type{ - Name: "uuid", - OID: pgtype.UUIDOID, - Codec: UUIDCodec{}, - }) -}