Skip to content

Commit

Permalink
Merge pull request #3 from tristanfisher/connect_timeout_fix
Browse files Browse the repository at this point in the history
specify connect seconds, remove unused struct field. v2.
  • Loading branch information
tristanfisher authored Apr 9, 2023
2 parents 98221b0 + f9ce9d4 commit fea8d06
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 52 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/tristanfisher/ivory
module github.com/tristanfisher/ivory/v2

go 1.20

Expand Down
28 changes: 14 additions & 14 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,16 +181,16 @@ func New(ctx context.Context, opts *DatabaseOptions, sqlText []string, createDat
// DatabaseOptions is used for connection parameters and setting up our handle
// used to generate a DSN() for connection
type DatabaseOptions struct {
Host string `dsnFormat:"host='%s'"`
Port int `dsnFormat:"port=%d"`
Database string `dsnFormat:"dbname='%s'"`
Schema string `dsnFormat:"search_path='%s'"`
User string `dsnFormat:"user='%s'"`
Password string `dsnFormat:"password='%s'"`
SslMode string `dsnFormat:"sslmode=%s"`
ConnectTimeout time.Duration `dsnFormat:"connect_timeout=%d"`
MaxOpenConns int
MaxIdleConns int
Host string `dsnFormat:"host='%s'"`
Port int `dsnFormat:"port=%d"`
Database string `dsnFormat:"dbname='%s'"`
Schema string `dsnFormat:"search_path='%s'"`
User string `dsnFormat:"user='%s'"`
Password string `dsnFormat:"password='%s'"`
SslMode string `dsnFormat:"sslmode=%s"`
ConnectTimeoutSeconds int `dsnFormat:"connect_timeout=%d"`
MaxOpenConns int
MaxIdleConns int

// internal for DSN string generation
reflectType reflect.Type
Expand Down Expand Up @@ -236,7 +236,7 @@ func (do *DatabaseOptions) GetDSNPart(fieldName string) (string, error) {
// name, type in addition to the dsn format string in the struct tags
func (do *DatabaseOptions) DSN() (string, error) {
// slice so we can trivially join and not deal with double spaces
// for string-comparison internal tests (whitespace trimming is not trivially easy with spaces, special chars valid in postgres fields)
// for string-comparison internal tests (whitespace trimming isConnectTimeoutSeconds not trivially easy with spaces, special chars valid in postgres fields)
dsnPortions := make([]string, 0)

if len(do.Host) > 0 {
Expand Down Expand Up @@ -299,12 +299,12 @@ func (do *DatabaseOptions) DSN() (string, error) {
dsnPortions = append(dsnPortions, fmt.Sprintf(partFmt, do.SslMode))
}

if do.ConnectTimeout > 0 {
partFmt, err := do.GetDSNPart("ConnectTimeout")
if do.ConnectTimeoutSeconds > 0 {
partFmt, err := do.GetDSNPart("ConnectTimeoutSeconds")
if err != nil {
return "", err
}
dsnPortions = append(dsnPortions, fmt.Sprintf(partFmt, do.ConnectTimeout))
dsnPortions = append(dsnPortions, fmt.Sprintf(partFmt, do.ConnectTimeoutSeconds))
}

return strings.Join(dsnPortions, " "), nil
Expand Down
74 changes: 37 additions & 37 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ func Test_mightHaveTransaction(t *testing.T) {

func TestDatabaseOptions_DSN(t *testing.T) {
type fields struct {
Host string
Port int
Database string
Schema string
User string
Password string
SslMode string
ConnectTimeout time.Duration
MaxOpenConns int
MaxIdleConns int
DefaultQueryTimeout time.Duration
reflectType reflect.Type
Host string
Port int
Database string
Schema string
User string
Password string
SslMode string
ConnectTimeoutSeconds int
MaxOpenConns int
MaxIdleConns int

reflectType reflect.Type
}
tests := []struct {
name string
Expand Down Expand Up @@ -135,32 +135,32 @@ func TestDatabaseOptions_DSN(t *testing.T) {
{
name: "allFields",
fields: fields{
Host: "localhost",
Port: 1024,
Database: "exampleDB",
Schema: "exampleSchema",
User: "user",
Password: "password",
SslMode: "disable",
ConnectTimeout: 10,
Host: "localhost",
Port: 1024,
Database: "exampleDB",
Schema: "exampleSchema",
User: "user",
Password: "password",
SslMode: "disable",
ConnectTimeoutSeconds: 10,
},
want: "host='localhost' port=1024 dbname='exampleDB' search_path='exampleSchema' user='user' password='password' sslmode=disable connect_timeout=10",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
do := &DatabaseOptions{
Host: tt.fields.Host,
Port: tt.fields.Port,
Database: tt.fields.Database,
Schema: tt.fields.Schema,
User: tt.fields.User,
Password: tt.fields.Password,
SslMode: tt.fields.SslMode,
ConnectTimeout: tt.fields.ConnectTimeout,
MaxOpenConns: tt.fields.MaxOpenConns,
MaxIdleConns: tt.fields.MaxIdleConns,
reflectType: tt.fields.reflectType,
Host: tt.fields.Host,
Port: tt.fields.Port,
Database: tt.fields.Database,
Schema: tt.fields.Schema,
User: tt.fields.User,
Password: tt.fields.Password,
SslMode: tt.fields.SslMode,
ConnectTimeoutSeconds: tt.fields.ConnectTimeoutSeconds,
MaxOpenConns: tt.fields.MaxOpenConns,
MaxIdleConns: tt.fields.MaxIdleConns,
reflectType: tt.fields.reflectType,
}

got, err := do.DSN()
Expand Down Expand Up @@ -410,12 +410,12 @@ func TestFindLikelyAbandonedDBs(t *testing.T) {

// bind a new connection not specific to a database
noDBOpts := &DatabaseOptions{
Host: "localhost",
Port: 5555,
ConnectTimeout: 10,
SslMode: "disable",
User: "postgres",
Password: "rootUserSeriousPassword1",
Host: "localhost",
Port: 5555,
ConnectTimeoutSeconds: 10,
SslMode: "disable",
User: "postgres",
Password: "rootUserSeriousPassword1",
}
dbHandle, err := Connect(ctx, noDBOpts)
if err != nil {
Expand Down

0 comments on commit fea8d06

Please sign in to comment.