Skip to content

Commit 1707863

Browse files
fix: fix sqldbtest; files tossing
1 parent 0be9991 commit 1707863

29 files changed

+344
-313
lines changed

docker-compose.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ services:
33
image: postgres:16
44
restart: unless-stopped
55
environment:
6-
POSTGRES_PASSWORD: test
7-
POSTGRES_USER: test
8-
POSTGRES_DB: test
6+
POSTGRES_PASSWORD: local
7+
POSTGRES_USER: local
8+
POSTGRES_DB: local
99
ports:
1010
- "5432:5432"
11-

go.mod

-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@ require (
1111
github.com/jackc/pgx/v5 v5.6.0
1212
github.com/jackc/puddle/v2 v2.2.1
1313
github.com/joho/godotenv v1.5.1
14-
github.com/samber/lo v1.47.0
15-
github.com/spf13/cobra v1.8.1
1614
github.com/stretchr/testify v1.9.0
17-
go.inout.gg/shield v0.0.0-20240827214956-d2b7a49e7e6f
1815
golang.org/x/crypto v0.26.0
1916
golang.org/x/sync v0.8.0
2017
)
@@ -24,14 +21,12 @@ require (
2421
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
2522
github.com/go-playground/locales v0.14.1 // indirect
2623
github.com/go-playground/universal-translator v0.18.1 // indirect
27-
github.com/inconshreveable/mousetrap v1.1.0 // indirect
2824
github.com/jackc/pgpassfile v1.0.0 // indirect
2925
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
3026
github.com/kr/text v0.2.0 // indirect
3127
github.com/leodido/go-urn v1.4.0 // indirect
3228
github.com/pmezard/go-difflib v1.0.0 // indirect
3329
github.com/rogpeppe/go-internal v1.12.0 // indirect
34-
github.com/spf13/pflag v1.0.5 // indirect
3530
golang.org/x/net v0.27.0 // indirect
3631
golang.org/x/sys v0.23.0 // indirect
3732
golang.org/x/text v0.17.0 // indirect

go.sum

-12
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ github.com/a-h/templ v0.2.747 h1:D0dQ2lxC3W7Dxl6fxQ/1zZHBQslSkTSvl5FxP/CfdKg=
22
github.com/a-h/templ v0.2.747/go.mod h1:69ObQIbrcuwPCU32ohNaWce3Cb7qM5GMiqN1K+2yop4=
33
github.com/caarlos0/env/v11 v11.1.0 h1:a5qZqieE9ZfzdvbbdhTalRrHT5vu/4V1/ad1Ka6frhI=
44
github.com/caarlos0/env/v11 v11.1.0/go.mod h1:LwgkYk1kDvfGpHthrWWLof3Ny7PezzFwS4QrsJdHTMo=
5-
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
65
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
76
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
87
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
@@ -24,8 +23,6 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
2423
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
2524
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
2625
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
27-
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
28-
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
2926
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
3027
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
3128
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
@@ -46,20 +43,11 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
4643
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4744
github.com/rogpeppe/go-internal v1.12.0 h1:exVL4IDcn6na9z1rAb56Vxr+CgyK3nn3O+epU5NdKM8=
4845
github.com/rogpeppe/go-internal v1.12.0/go.mod h1:E+RYuTGaKKdloAfM02xzb0FW3Paa99yedzYV+kq4uf4=
49-
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
50-
github.com/samber/lo v1.47.0 h1:z7RynLwP5nbyRscyvcD043DWYoOcYRv3mV8lBeqOCLc=
51-
github.com/samber/lo v1.47.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU=
52-
github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
53-
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
54-
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
55-
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
5646
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
5747
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
5848
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
5949
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
6050
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
61-
go.inout.gg/shield v0.0.0-20240827214956-d2b7a49e7e6f h1:ZmMipAuTb7XaFoHZXG4DNZWoqwd7UY4/omqSzTY84hY=
62-
go.inout.gg/shield v0.0.0-20240827214956-d2b7a49e7e6f/go.mod h1:Pr1OahlV0l4aEF7X5MpmXdzyV2s0fz747kn8wfPWkhA=
6351
golang.org/x/crypto v0.26.0 h1:RrRspgV4mU+YwB4FYnuBoKsUapNIL5cohGAmSH3azsw=
6452
golang.org/x/crypto v0.26.0/go.mod h1:GY7jblb9wI+FOo5y8/S2oY4zWP07AkOJ4+jxCqdqn54=
6553
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=

pointer/pointer_test.go

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package pointer
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestToValue(t *testing.T) {
10+
t.Run("non-nil pointer", func(t *testing.T) {
11+
value := 42
12+
ptr := &value
13+
defaultValue := 0
14+
15+
result := ToValue(ptr, defaultValue)
16+
17+
assert.Equal(t, value, result, "ToValue should return the pointed-to value for non-nil pointers")
18+
})
19+
20+
t.Run("nil pointer", func(t *testing.T) {
21+
var ptr *int
22+
defaultValue := 10
23+
24+
result := ToValue(ptr, defaultValue)
25+
26+
assert.Equal(t, defaultValue, result, "ToValue should return the default value for nil pointers")
27+
})
28+
}
29+
30+
func TestFromValue(t *testing.T) {
31+
t.Run("integer value", func(t *testing.T) {
32+
value := 42
33+
34+
result := FromValue(value)
35+
36+
assert.NotNil(t, result, "FromValue should return a non-nil pointer")
37+
assert.Equal(t, value, *result, "FromValue should return a pointer to the correct value")
38+
})
39+
40+
t.Run("string value", func(t *testing.T) {
41+
value := "hello"
42+
43+
result := FromValue(value)
44+
45+
assert.NotNil(t, result, "FromValue should return a non-nil pointer")
46+
assert.Equal(t, value, *result, "FromValue should return a pointer to the correct value")
47+
})
48+
49+
t.Run("struct value", func(t *testing.T) {
50+
type TestStruct struct {
51+
Field string
52+
}
53+
value := TestStruct{Field: "test"}
54+
55+
result := FromValue(value)
56+
57+
assert.NotNil(t, result, "FromValue should return a non-nil pointer")
58+
assert.Equal(t, value, *result, "FromValue should return a pointer to the correct value")
59+
})
60+
}

random/hex.go

-28
This file was deleted.

sql/db/context.go

-30
This file was deleted.

sql/db/middleware.go

Whitespace-only changes.

sql/db/option.go

-15
This file was deleted.

sql/db/dbutil/util.go sqldb/error.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package dbutil
1+
package sqldb
22

33
import (
44
"errors"
@@ -14,11 +14,12 @@ const (
1414

1515
// IsUniqueViolationError returns true if the error is a unique violation error.
1616
func IsUniqueViolationError(err error) bool {
17-
if pgxErr, ok := err.(*pgconn.PgError); !ok || pgxErr.Code != ErrCodeUniqueViolation {
18-
return false
17+
pgxErr := &pgconn.PgError{}
18+
if errors.As(err, &pgxErr) {
19+
return pgxErr.Code == ErrCodeUniqueViolation
1920
}
2021

21-
return true
22+
return false
2223
}
2324

2425
// IsNotFoundError returns true if the error is a pgx no rows error.
File renamed without changes.
File renamed without changes.

sql/db/pgxuuid/pgxuuid_test.go sqldb/internal/pgxuuid/pgxuuid_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@ import (
88

99
"github.com/google/uuid"
1010
"github.com/stretchr/testify/require"
11-
"go.inout.gg/foundations/sql/db/dbtest"
11+
"go.inout.gg/foundations/sqldb/sqldbtest"
1212
)
1313

1414
func TestCodecDecodeValue(t *testing.T) {
1515
ctx := context.Background()
16-
db := dbtest.Must(ctx, t)
16+
db := sqldbtest.Must(ctx, t)
1717

1818
pool := db.Pool()
1919
original, err := uuid.NewV7()
@@ -50,7 +50,7 @@ func TestCodecDecodeValue(t *testing.T) {
5050

5151
func TestArray(t *testing.T) {
5252
ctx := context.Background()
53-
db := dbtest.Must(ctx, t)
53+
db := sqldbtest.Must(ctx, t)
5454
p := db.Pool()
5555

5656
inputSlice := []uuid.UUID{}

sqldb/middleware.go

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package sqldb
2+
3+
import (
4+
"context"
5+
"errors"
6+
"net/http"
7+
8+
"github.com/jackc/pgx/v5/pgxpool"
9+
"go.inout.gg/foundations/http/httpmiddleware"
10+
)
11+
12+
type ctxKey struct{}
13+
14+
var kCtxKey = ctxKey{}
15+
16+
var ErrDBPoolNotFound = errors.New("foundations/sqldb: failed to retrieve db pool from context.")
17+
18+
// WithContext returns a new context with the given pool.
19+
func WithContext(ctx context.Context, pool *pgxpool.Pool) context.Context {
20+
return context.WithValue(ctx, kCtxKey, pool)
21+
}
22+
23+
// FromContext returns the pool associated with the given context.
24+
func FromContext(ctx context.Context) (*pgxpool.Pool, error) {
25+
if pool, ok := ctx.Value(kCtxKey).(*pgxpool.Pool); ok {
26+
return pool, nil
27+
}
28+
29+
return nil, ErrDBPoolNotFound
30+
}
31+
32+
// FromRequest returns the pool associated with the given http request.
33+
func FromRequest(req *http.Request) (*pgxpool.Pool, error) {
34+
return FromContext(req.Context())
35+
}
36+
37+
// Middleware returns a middleware that injects the given pool into the request context.
38+
func Middleware(db *pgxpool.Pool) httpmiddleware.MiddlewareFunc {
39+
return func(next http.Handler) http.Handler {
40+
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
41+
next.ServeHTTP(w, req.WithContext(WithContext(req.Context(), db)))
42+
})
43+
}
44+
}

sqldb/pool.go

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package sqldb
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/jackc/pgx/v5"
8+
"github.com/jackc/pgx/v5/pgxpool"
9+
"go.inout.gg/foundations/must"
10+
"go.inout.gg/foundations/sqldb/internal/pgxuuid"
11+
)
12+
13+
// WithTracer sets the query tracer for the database pool.
14+
func WithTracer(t pgx.QueryTracer) func(c *pgxpool.Config) {
15+
return func(c *pgxpool.Config) { c.ConnConfig.Tracer = t }
16+
}
17+
18+
// WithUUID adds native support for converting between Postgres UUID and google/uuid.
19+
func WithUUID() func(c *pgxpool.Config) {
20+
return func(c *pgxpool.Config) {
21+
origAfterConnect := c.AfterConnect
22+
c.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error {
23+
pgxuuid.Register(conn.TypeMap())
24+
if origAfterConnect != nil {
25+
return origAfterConnect(ctx, conn)
26+
}
27+
return nil
28+
}
29+
}
30+
}
31+
32+
// MustPool creates a new connection pool and panics on error.
33+
func MustPool(ctx context.Context, connString string, cfgs ...func(*pgxpool.Config)) *pgxpool.Pool {
34+
return must.Must(NewPool(ctx, connString, cfgs...))
35+
}
36+
37+
// NewPool creates a new connection pool using the provided connection string.
38+
//
39+
// Optional cfgs like WithUUID or WithTracer can be provided.
40+
func NewPool(ctx context.Context, connString string, cfgs ...func(*pgxpool.Config)) (pool *pgxpool.Pool, err error) {
41+
config, err := pgxpool.ParseConfig(connString)
42+
if err != nil {
43+
return nil, fmt.Errorf("foundations/sqldb: failed to parse database connection string: %w", err)
44+
}
45+
for _, f := range cfgs {
46+
f(config)
47+
}
48+
49+
pool, err = pgxpool.NewWithConfig(ctx, config)
50+
if err != nil {
51+
return nil, fmt.Errorf("foundations/sqldb: failed to create a new database pool: %w", err)
52+
}
53+
defer func() {
54+
if err != nil {
55+
if pool != nil {
56+
pool.Close()
57+
}
58+
}
59+
}()
60+
61+
if err = pool.Ping(ctx); err != nil {
62+
return nil, fmt.Errorf("foundations/sqldb: failed to connect to the database at %s: %w", connString, err)
63+
}
64+
65+
return pool, nil
66+
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)