Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: google cloud sql connections #446

Merged
merged 2 commits into from
Jan 24, 2024
Merged
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
22 changes: 16 additions & 6 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,34 @@ func NewGorm(connection string, config *gorm.Config) (*gorm.DB, error) {
return Gorm, nil
}

func NewDB(connection string) (*sql.DB, error) {
func getConnection(connection string) (string, error) {
pgxConfig, err := drivers.ParseURL(connection)
if err != nil {
return nil, err
return connection, err
} else if pgxConfig != nil {
connection = stdlib.RegisterConnConfig(pgxConfig)
return stdlib.RegisterConnConfig(pgxConfig), nil
}
return connection, nil
}

return sql.Open("pgx", connection)
func NewDB(connection string) (*sql.DB, error) {
conn, err := getConnection(connection)
if err != nil {
return nil, err
}
return sql.Open("pgx", conn)
}

func NewPgxPool(connection string) (*pgxpool.Pool, error) {
pgUrl, err := url.Parse(connection)
connection, err := getConnection(connection)
if err != nil {
return nil, err
}

logger.Infof("Connecting to %s", pgUrl.Redacted())
pgUrl, err := url.Parse(connection)
if err == nil {
logger.Infof("Connecting to %s", pgUrl.Redacted())
}

config, err := pgxpool.ParseConfig(connection)
if err != nil {
Expand Down
Loading