diff options
author | Mike Crute <mike@crute.us> | 2023-12-20 14:10:30 -0800 |
---|---|---|
committer | Mike Crute <mike@crute.us> | 2023-12-20 14:10:30 -0800 |
commit | 6241d18c5d4faee3c0806a3151edde15772c6eac (patch) | |
tree | c471fb87b385f9fb7f45cbf6fd79a9f81abec9c0 | |
parent | f0c1818b9148e56f8e1c0c3fd8a6c4070d9a80d3 (diff) | |
download | golib-6241d18c5d4faee3c0806a3151edde15772c6eac.tar.bz2 golib-6241d18c5d4faee3c0806a3151edde15772c6eac.tar.xz golib-6241d18c5d4faee3c0806a3151edde15772c6eac.zip |
db/postgres: support goormdb/postgres/v0.2.0
-rw-r--r-- | db/postgres/client.go | 56 | ||||
-rw-r--r-- | db/postgres/go.mod | 2 | ||||
-rw-r--r-- | db/postgres/go.sum | 2 |
3 files changed, 56 insertions, 4 deletions
diff --git a/db/postgres/client.go b/db/postgres/client.go index 49f8b2e..f8c6123 100644 --- a/db/postgres/client.go +++ b/db/postgres/client.go | |||
@@ -2,6 +2,7 @@ package postgres | |||
2 | 2 | ||
3 | import ( | 3 | import ( |
4 | "context" | 4 | "context" |
5 | "database/sql" | ||
5 | "net" | 6 | "net" |
6 | "net/url" | 7 | "net/url" |
7 | "strings" | 8 | "strings" |
@@ -9,9 +10,11 @@ import ( | |||
9 | "code.crute.us/mcrute/golib/secrets" | 10 | "code.crute.us/mcrute/golib/secrets" |
10 | 11 | ||
11 | "github.com/jackc/pgx/v5" | 12 | "github.com/jackc/pgx/v5" |
13 | "github.com/jackc/pgx/v5/pgxpool" | ||
14 | "github.com/jackc/pgx/v5/stdlib" | ||
12 | ) | 15 | ) |
13 | 16 | ||
14 | func Connect(ctx context.Context, uri string, sc secrets.Client) (*pgx.Conn, error) { | 17 | func prepareUri(ctx context.Context, uri string, sc secrets.Client) (string, string, error) { |
15 | // Prefix uri with postgres:// unless it already includes one of the | 18 | // Prefix uri with postgres:// unless it already includes one of the |
16 | // standard prefixes. Otherwise if scheme is omitted then url parsing | 19 | // standard prefixes. Otherwise if scheme is omitted then url parsing |
17 | // will fail to capture the username for secret lookup. | 20 | // will fail to capture the username for secret lookup. |
@@ -21,7 +24,7 @@ func Connect(ctx context.Context, uri string, sc secrets.Client) (*pgx.Conn, err | |||
21 | 24 | ||
22 | u, err := url.Parse(uri) | 25 | u, err := url.Parse(uri) |
23 | if err != nil { | 26 | if err != nil { |
24 | return nil, err | 27 | return "", "", err |
25 | } | 28 | } |
26 | 29 | ||
27 | // The username provided by the user (there should be no | 30 | // The username provided by the user (there should be no |
@@ -31,7 +34,7 @@ func Connect(ctx context.Context, uri string, sc secrets.Client) (*pgx.Conn, err | |||
31 | // connect. | 34 | // connect. |
32 | cred, _, err := sc.DatabaseCredential(ctx, u.User.Username()) | 35 | cred, _, err := sc.DatabaseCredential(ctx, u.User.Username()) |
33 | if err != nil { | 36 | if err != nil { |
34 | return nil, err | 37 | return "", "", err |
35 | } | 38 | } |
36 | u.User = url.UserPassword(cred.Username, cred.Password) | 39 | u.User = url.UserPassword(cred.Username, cred.Password) |
37 | 40 | ||
@@ -40,5 +43,50 @@ func Connect(ctx context.Context, uri string, sc secrets.Client) (*pgx.Conn, err | |||
40 | u.Host = net.JoinHostPort(u.Host, "5432") | 43 | u.Host = net.JoinHostPort(u.Host, "5432") |
41 | } | 44 | } |
42 | 45 | ||
43 | return pgx.Connect(ctx, u.String()) | 46 | return u.String(), u.Query().Get("TimeZone"), nil |
47 | } | ||
48 | |||
49 | func Connect(ctx context.Context, uri string, sc secrets.Client) (*pgx.Conn, error) { | ||
50 | dsn, tz, err := prepareUri(ctx, uri, sc) | ||
51 | if err != nil { | ||
52 | return nil, err | ||
53 | } | ||
54 | |||
55 | cfg, err := pgx.ParseConfig(dsn) | ||
56 | if err != nil { | ||
57 | return nil, err | ||
58 | } | ||
59 | |||
60 | if tz != "" { | ||
61 | cfg.RuntimeParams["timezone"] = tz | ||
62 | } | ||
63 | |||
64 | return pgx.ConnectConfig(ctx, cfg) | ||
65 | } | ||
66 | |||
67 | func ConnectPool(ctx context.Context, uri string, sc secrets.Client) (*pgxpool.Pool, error) { | ||
68 | dsn, tz, err := prepareUri(ctx, uri, sc) | ||
69 | if err != nil { | ||
70 | return nil, err | ||
71 | } | ||
72 | |||
73 | cfg, err := pgxpool.ParseConfig(dsn) | ||
74 | if err != nil { | ||
75 | return nil, err | ||
76 | } | ||
77 | |||
78 | if tz != "" { | ||
79 | cfg.ConnConfig.RuntimeParams["timezone"] = tz | ||
80 | } | ||
81 | |||
82 | return pgxpool.NewWithConfig(ctx, cfg) | ||
83 | } | ||
84 | |||
85 | func ConnectStdlibPool(ctx context.Context, uri string, sc secrets.Client) (*sql.DB, error) { | ||
86 | pool, err := ConnectPool(ctx, uri, sc) | ||
87 | if err != nil { | ||
88 | return nil, err | ||
89 | } | ||
90 | |||
91 | return stdlib.OpenDBFromPool(pool), nil | ||
44 | } | 92 | } |
diff --git a/db/postgres/go.mod b/db/postgres/go.mod index ae0e3d0..ac3bee8 100644 --- a/db/postgres/go.mod +++ b/db/postgres/go.mod | |||
@@ -37,6 +37,7 @@ require ( | |||
37 | github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect | 37 | github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb // indirect |
38 | github.com/jackc/pgpassfile v1.0.0 // indirect | 38 | github.com/jackc/pgpassfile v1.0.0 // indirect |
39 | github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect | 39 | github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect |
40 | github.com/jackc/puddle/v2 v2.2.1 // indirect | ||
40 | github.com/mattn/go-colorable v0.1.6 // indirect | 41 | github.com/mattn/go-colorable v0.1.6 // indirect |
41 | github.com/mattn/go-isatty v0.0.12 // indirect | 42 | github.com/mattn/go-isatty v0.0.12 // indirect |
42 | github.com/mitchellh/copystructure v1.0.0 // indirect | 43 | github.com/mitchellh/copystructure v1.0.0 // indirect |
@@ -51,6 +52,7 @@ require ( | |||
51 | go.uber.org/atomic v1.9.0 // indirect | 52 | go.uber.org/atomic v1.9.0 // indirect |
52 | golang.org/x/crypto v0.9.0 // indirect | 53 | golang.org/x/crypto v0.9.0 // indirect |
53 | golang.org/x/net v0.10.0 // indirect | 54 | golang.org/x/net v0.10.0 // indirect |
55 | golang.org/x/sync v0.1.0 // indirect | ||
54 | golang.org/x/sys v0.8.0 // indirect | 56 | golang.org/x/sys v0.8.0 // indirect |
55 | golang.org/x/text v0.9.0 // indirect | 57 | golang.org/x/text v0.9.0 // indirect |
56 | golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect | 58 | golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1 // indirect |
diff --git a/db/postgres/go.sum b/db/postgres/go.sum index 78d421b..950a9b7 100644 --- a/db/postgres/go.sum +++ b/db/postgres/go.sum | |||
@@ -152,6 +152,7 @@ github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZ | |||
152 | github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw= | 152 | github.com/jackc/pgx/v5 v5.5.0 h1:NxstgwndsTRy7eq9/kqYc/BZh5w2hHJV86wjvO+1xPw= |
153 | github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= | 153 | github.com/jackc/pgx/v5 v5.5.0/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= |
154 | github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= | 154 | github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= |
155 | github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= | ||
155 | github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= | 156 | github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI= |
156 | github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE= | 157 | github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE= |
157 | github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74= | 158 | github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74= |
@@ -273,6 +274,7 @@ golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJ | |||
273 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | 274 | golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
274 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | 275 | golang.org/x/sync v0.0.0-20190911185100-cd5d95a43a6e/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= |
275 | golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= | 276 | golang.org/x/sync v0.1.0 h1:wsuoTGHzEhffawBOhz5CYhcrV4IdKZbEyZjBMuTp12o= |
277 | golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= | ||
276 | golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | 278 | golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
277 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | 279 | golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |
278 | golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= | 280 | golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= |