Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions direct_database_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"database/sql/driver"
"fmt"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/stdlib"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
Expand Down Expand Up @@ -66,9 +66,6 @@ func (p *alloydbDirect) Open(name string) (driver.Conn, error) {
config.Password = tok.AccessToken

dbURI := stdlib.RegisterConnConfig(config)
if err != nil {
return nil, err
}
return stdlib.GetDefaultDriver().Open(dbURI)
}

Expand Down
6 changes: 3 additions & 3 deletions direct_pgxpool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
"context"
"fmt"

"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"golang.org/x/oauth2/google"
)

Expand Down Expand Up @@ -70,5 +70,5 @@ func connectDirectPGXPoolAutoIAMAuthN(
c.Password = tok.AccessToken
return nil
}
return pgxpool.ConnectConfig(ctx, config)
return pgxpool.NewWithConfig(ctx, config)
}
12 changes: 6 additions & 6 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
// "net"
//
// "cloud.google.com/go/alloydbconn"
// "github.com/jackc/pgx/v4/pgxpool"
// "github.com/jackc/pgx/v5/pgxpool"
// )
//
// func connect() {
Expand All @@ -55,26 +55,26 @@
// }
//
// // Interact with the driver directly as you normally would
// conn, err := pgxpool.ConnectConfig(context.Background(), config)
// conn, err := pgxpool.NewWithConfig(context.Background(), config)
// if err != nil {
// log.Fatalf("failed to connect: %v", connErr)
// }
// defer conn.Close()
// }
//
// To use [database/sql], call pgxv4.RegisterDriver with any necessary Dialer
// To use [database/sql], call pgxv5.RegisterDriver with any necessary Dialer
// configuration.
//
// import (
// "database/sql"
//
// "cloud.google.com/go/alloydbconn"
// "cloud.google.com/go/alloydbconn/driver/pgxv4"
// "cloud.google.com/go/alloydbconn/driver/pgxv5"
// )
//
// func connect() {
// // adjust options as needed
// cleanup, err := pgxv4.RegisterDriver("alloydb")
// cleanup, err := pgxv5.RegisterDriver("alloydb")
// if err != nil {
// // ... handle error
// }
Expand All @@ -89,5 +89,5 @@
//
// [database/sql]: https://pkg.go.dev/database/sql
// [pgx]: https://github.com/jackc/pgx
// [pgxpool]: https://pkg.go.dev/github.com/jackc/pgx/v4/pgxpool
// [pgxpool]: https://pkg.go.dev/github.com/jackc/pgx/v5/pgxpool
package alloydbconn
77 changes: 5 additions & 72 deletions driver/pgxv4/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,83 +12,16 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package pgxv4 provides an AlloyDB driver that uses pgx v4 and works with the
// database/sql package.
// This package is deprecated. Use pgxv5 directly instead.
package pgxv4

import (
"context"
"database/sql"
"database/sql/driver"
"net"
"sync"

"cloud.google.com/go/alloydbconn"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"cloud.google.com/go/alloydbconn/driver/pgxv5"
)

// RegisterDriver registers a Postgres driver that uses the alloydbconn.Dialer
// configured with the provided options. The choice of name is entirely up to
// the caller and may be used to distinguish between multiple registrations of
// differently configured Dialers. The driver uses pgx/v4 internally.
Comment thread
nancynh marked this conversation as resolved.
// RegisterDriver returns a cleanup function that should be called one the
// database connection is no longer needed.
// RegisterDriver calls through to pgxv5.RegisterDriver. Prefer the pgxv5
// interface instead of using this one
func RegisterDriver(name string, opts ...alloydbconn.Option) (func() error, error) {
d, err := alloydbconn.NewDialer(context.Background(), opts...)
if err != nil {
return func() error { return nil }, err
}
sql.Register(name, &pgDriver{
d: d,
dbURIs: make(map[string]string),
})
return func() error { return d.Close() }, nil
}

type pgDriver struct {
d *alloydbconn.Dialer
mu sync.RWMutex
// dbURIs is a map of DSN to DB URI for registered connection names.
dbURIs map[string]string
}

// Open accepts a keyword/value formatted connection string and returns a
// connection to the database using alloydbconn.Dialer. The AlloyDB instance URI
// should be specified in the host field. For example:
//
// "host=projects/<PROJECT>/locations/<REGION>/clusters/<CLUSTER>/instances/<INSTANCE> user=myuser password=mypass"
func (p *pgDriver) Open(name string) (driver.Conn, error) {
dbURI, err := p.dbURI(name)
if err != nil {
return nil, err
}
return stdlib.GetDefaultDriver().Open(dbURI)

}

// dbURI registers a driver using the provided DSN. If the name has already
// been registered, dbURI returns the existing registration.
func (p *pgDriver) dbURI(name string) (string, error) {
p.mu.Lock()
defer p.mu.Unlock()
dbURI, ok := p.dbURIs[name]
if ok {
return dbURI, nil
}

config, err := pgx.ParseConfig(name)
if err != nil {
return "", err
}
instConnName := config.Config.Host // Extract instance connection name
config.Config.Host = "localhost" // Replace it with a default value
config.DialFunc = func(ctx context.Context, _, _ string) (net.Conn, error) {
return p.d.Dial(ctx, instConnName)
}

dbURI = stdlib.RegisterConnConfig(config)
p.dbURIs[name] = dbURI

return dbURI, nil
return pgxv5.RegisterDriver(name, opts...)
}
2 changes: 1 addition & 1 deletion e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"cloud.google.com/go/alloydbconn"
"cloud.google.com/go/alloydbconn/driver/pgxv4"
"cloud.google.com/go/alloydbconn/driver/pgxv5"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
)

Expand Down
7 changes: 0 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ require (
github.com/GoogleCloudPlatform/opentelemetry-operations-go/exporter/metric v0.55.0
github.com/google/go-cmp v0.7.0
github.com/google/uuid v1.6.0
github.com/jackc/pgx/v4 v4.18.3
github.com/jackc/pgx/v5 v5.8.0
go.opencensus.io v0.24.0
go.opentelemetry.io/otel v1.42.0
Expand Down Expand Up @@ -39,14 +38,8 @@ require (
github.com/google/s2a-go v0.1.9 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.14 // indirect
github.com/googleapis/gax-go/v2 v2.17.0 // indirect
github.com/jackc/chunkreader/v2 v2.0.1 // indirect
github.com/jackc/pgconn v1.14.3 // indirect
github.com/jackc/pgio v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgproto3/v2 v2.3.3 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgtype v1.14.0 // indirect
github.com/jackc/puddle v1.3.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.63.0 // indirect
Expand Down
Loading
Loading