Skip to content

Commit

Permalink
rewrite PQconnectdb using connection setup in a loop
Browse files Browse the repository at this point in the history
  • Loading branch information
lesovsky committed Mar 27, 2019
1 parent 30936b4 commit e71ec38
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cmd/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
// ProgramVersion is the version of this program
ProgramVersion = "0.6"
// ProgramRelease is release number of this program
ProgramRelease = "1"
ProgramRelease = "1-devel"
// ProgramIssuesUrl is the public URL for posting issues, bug reports and asking questions
ProgramIssuesUrl = "https://github.com/lesovsky/pgcenter/issues"
)
Expand Down
46 changes: 11 additions & 35 deletions lib/utils/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,53 +76,29 @@ func assembleConnstr(c *Conninfo) string {

// PQconnectdb connects to Postgres, asks password if required.
func PQconnectdb(c *Conninfo, connstr string) (conn *sql.DB, err error) {
conn, err = sql.Open(dbDriver, connstr)
if err != nil {
return nil, err
}

if err = PQstatus(conn); err != nil {
// handle libpq errors if found
if pqerr, ok := err.(*pq.Error); ok {
// try connecting in the loop and handle all known errors, give up if can;t handle an error
for {
conn, err = sql.Open(dbDriver, connstr)
if err = PQstatus(conn); err != nil {
switch {
// Password required -- ask user and retry connection
case pqerr.Code == errCodeInvalidPassword:
case err == pq.ErrSSLNotSupported:
// By default pq-driver tries to connect with SSL. So if SSL is not enabled on the other side - fix our connection string and try to reconnect
connstr = connstr + " sslmode=disable"
case err.(*pq.Error).Code == errCodeInvalidPassword:
fmt.Printf("Password for user %s: ", c.User)
bytePassword, err := terminal.ReadPassword(0)
if err != nil {
return nil, err
}
connstr = fmt.Sprintf("%s password=%s ", connstr, string(bytePassword))
conn, err = sql.Open(dbDriver, connstr)
if err != nil {
return nil, err
}
if err = PQstatus(conn); err != nil {
return nil, err
}
fmt.Println()
default:
return nil, fmt.Errorf("error occurred during connection establishing: %s", err)
return nil, fmt.Errorf("failed connection establishing: %s", err)
}

} else {
return conn, nil
}

// handle other golang 'pq' driver-specific errors (not related to libpq)
switch err {
case pq.ErrSSLNotSupported:
// By default pq-driver tries to connect with SSL.
// So if SSL is not enabled on the other side - fix our connection string and try to reconnect
connstr = connstr + " sslmode=disable"
conn, err = sql.Open(dbDriver, connstr)
if err = PQstatus(conn); err != nil {
return nil, err
}
default:
return nil, fmt.Errorf("error occurred during connection establishing: %s", err)
}
}

return conn, nil
}

// Fills empty connection settings by normal values
Expand Down

0 comments on commit e71ec38

Please sign in to comment.