Skip to content

Commit

Permalink
Construct DNS for Postgres in more deterministic way
Browse files Browse the repository at this point in the history
  • Loading branch information
vasayxtx committed Feb 17, 2025
1 parent d114b71 commit fae7e0f
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 84 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ issues:
linters:
- dogsled
- dupl
- errcheck
- funlen
- goconst
- gocritic
Expand Down
11 changes: 9 additions & 2 deletions dsn.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ package dbkit

import (
"fmt"
"sort"
"strings"

"net/url"

Expand Down Expand Up @@ -60,8 +62,13 @@ func MakePostgresDSN(cfg *PostgresConfig) string {
if cfg.SearchPath != "" {
connURI.RawQuery += fmt.Sprintf("&search_path=%s", url.QueryEscape(cfg.SearchPath))
}
for k, v := range cfg.AdditionalParameters {
connURI.RawQuery += fmt.Sprintf("&%s=%s", k, url.QueryEscape(v))
if len(cfg.AdditionalParameters) != 0 {
queryParts := make([]string, 0, len(cfg.AdditionalParameters))
for k, v := range cfg.AdditionalParameters {
queryParts = append(queryParts, fmt.Sprintf("%s=%s", k, url.QueryEscape(v)))
}
sort.Strings(queryParts) // Sort to make DSN deterministic.
connURI.RawQuery += "&" + strings.Join(queryParts, "&")
}
return connURI.String()
}
Expand Down
42 changes: 42 additions & 0 deletions dsn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,48 @@ func TestMakeMySQLDSN(t *testing.T) {
require.Equal(t, wantDSN, gotDSN)
}

func TestMakePostgresDSN(t *testing.T) {
tests := []struct {
Name string
Cfg *PostgresConfig
WantDSN string
}{
{
Name: "search_path is used",
Cfg: &PostgresConfig{
Host: "pghost",
Port: 5433,
User: "pgadmin",
Password: "pgpassword",
Database: "pgdb",
SSLMode: PostgresSSLModeRequire,
SearchPath: "pgsearch",
AdditionalParameters: map[string]string{"param1": "foo", "param2": "bar"},
},
WantDSN: "postgres://pgadmin:pgpassword@pghost:5433/pgdb?sslmode=require&search_path=pgsearch&param1=foo&param2=bar",
},
{
Name: "base",
Cfg: &PostgresConfig{
Host: "pghost",
Port: 5433,
User: "pgadmin",
Password: "pgpassword",
Database: "pgdb",
SSLMode: PostgresSSLModeRequire,
AdditionalParameters: map[string]string{"param1": "Lorem ipsum"},
},
WantDSN: "postgres://pgadmin:pgpassword@pghost:5433/pgdb?sslmode=require&param1=Lorem+ipsum",
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.Name, func(t *testing.T) {
require.Equal(t, MakePostgresDSN(tt.Cfg), tt.WantDSN)
})
}
}

func TestMakePgSQLDSN(t *testing.T) {
cfg := &PostgresConfig{
Host: "myhost",
Expand Down
42 changes: 0 additions & 42 deletions pgx/pgx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,48 +21,6 @@ import (
"github.com/acronis/go-dbkit/internal/testing"
)

func TestMakePostgresDSN(t *gotesting.T) {
tests := []struct {
Name string
Cfg *dbkit.PostgresConfig
WantDSN string
}{
{
Name: "search_path is used",
Cfg: &dbkit.PostgresConfig{
Host: "pghost",
Port: 5433,
User: "pgadmin",
Password: "pgpassword",
Database: "pgdb",
SSLMode: dbkit.PostgresSSLModeRequire,
SearchPath: "pgsearch",
AdditionalParameters: map[string]string{"param1": "foo", "param2": "bar"},
},
WantDSN: "postgres://pgadmin:pgpassword@pghost:5433/pgdb?sslmode=require&search_path=pgsearch&param1=foo&param2=bar",
},
{
Name: "base",
Cfg: &dbkit.PostgresConfig{
Host: "pghost",
Port: 5433,
User: "pgadmin",
Password: "pgpassword",
Database: "pgdb",
SSLMode: dbkit.PostgresSSLModeRequire,
AdditionalParameters: map[string]string{"param1": "Lorem ipsum"},
},
WantDSN: "postgres://pgadmin:pgpassword@pghost:5433/pgdb?sslmode=require&param1=Lorem+ipsum",
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.Name, func(t *gotesting.T) {
require.Equal(t, dbkit.MakePostgresDSN(tt.Cfg), tt.WantDSN)
})
}
}

func TestPostgresIsRetryable(t *gotesting.T) {
isRetryable := dbkit.GetIsRetryable(&pg.Driver{})
require.NotNil(t, isRetryable)
Expand Down
40 changes: 0 additions & 40 deletions postgres/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,6 @@ import (
"github.com/acronis/go-dbkit"
)

func TestMakePostgresDSN(t *testing.T) {
tests := []struct {
Name string
Cfg *dbkit.PostgresConfig
WantDSN string
}{
{
Name: "search_path is used",
Cfg: &dbkit.PostgresConfig{
Host: "pghost",
Port: 5433,
User: "pgadmin",
Password: "pgpassword",
Database: "pgdb",
SSLMode: dbkit.PostgresSSLModeRequire,
SearchPath: "pgsearch",
},
WantDSN: "postgres://pgadmin:pgpassword@pghost:5433/pgdb?sslmode=require&search_path=pgsearch",
},
{
Name: "base",
Cfg: &dbkit.PostgresConfig{
Host: "pghost",
Port: 5433,
User: "pgadmin",
Password: "pgpassword",
Database: "pgdb",
SSLMode: dbkit.PostgresSSLModeRequire,
},
WantDSN: "postgres://pgadmin:pgpassword@pghost:5433/pgdb?sslmode=require",
},
}
for i := range tests {
tt := tests[i]
t.Run(tt.Name, func(t *testing.T) {
require.Equal(t, dbkit.MakePostgresDSN(tt.Cfg), tt.WantDSN)
})
}
}

func TestPostgresIsRetryable(t *testing.T) {
isRetryable := dbkit.GetIsRetryable(&pg.Driver{})
require.NotNil(t, isRetryable)
Expand Down

0 comments on commit fae7e0f

Please sign in to comment.