Skip to content

Commit f65664e

Browse files
[CMCSMACD-6133] Support search_path in postgres+rds-iam scheme (#21)
Also, use "config" param when adding a search path to a regular postgres DSN because that is supported by both psql and the Go pq library, whereas search_path is only supported by the latter.
1 parent 9a1ff04 commit f65664e

1 file changed

Lines changed: 20 additions & 10 deletions

File tree

pgutils/connector.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -127,23 +127,23 @@ func MustConnectDB(conn driver.Connector) *sqlx.DB {
127127
return db
128128
}
129129

130-
// addSearchPathToURL returns a copy of u with search_path set in the query string.
131-
// It returns an error if search_path is already present.
130+
// addSearchPathToURL returns a copy of u with search_path set in the options parameter
131+
// of the query string. It returns an error if the search_path or options parameter is
132+
// already present.
132133
func addSearchPathToURL(rawURL string, searchPath string) (string, error) {
133134
u, err := url.Parse(rawURL)
134135
if err != nil {
135136
return "", fmt.Errorf("url string failed to parse while adding search path: %w", err)
136137
}
137138

138-
if searchPath == "" {
139-
return u.String(), nil
140-
}
141-
142139
q := u.Query()
143-
if v := q.Get("search_path"); v != "" {
140+
if v, ok := q["search_path"]; ok {
144141
return "", fmt.Errorf("search_path already set to %q", v)
145142
}
146-
q.Set("search_path", searchPath)
143+
if v, ok := q["options"]; ok {
144+
return "", fmt.Errorf("options already set to %q", v)
145+
}
146+
q.Set("options", fmt.Sprintf("-csearch_path=%s", searchPath))
147147
u.RawQuery = q.Encode()
148148
return u.String(), nil
149149
}
@@ -247,6 +247,7 @@ func newIAMConnectionStringProviderFromURL(ctx context.Context, u *url.URL, onTo
247247
supportedParams := map[string]struct{}{
248248
"assume_role_arn": {},
249249
"assume_role_session_name": {},
250+
"search_path": {},
250251
}
251252
for k := range q {
252253
if _, ok := supportedParams[k]; !ok {
@@ -278,7 +279,7 @@ func newIAMConnectionStringProviderFromURL(ctx context.Context, u *url.URL, onTo
278279
creds = aws.NewCredentialsCache(assumeProvider)
279280
}
280281

281-
return &rdsIAMConnectionStringProvider{
282+
var p ConnectionStringProvider = &rdsIAMConnectionStringProvider{
282283
Region: awsCfg.Region,
283284
RDSEndpoint: net.JoinHostPort(host, port),
284285
User: user,
@@ -287,5 +288,14 @@ func newIAMConnectionStringProviderFromURL(ctx context.Context, u *url.URL, onTo
287288
AssumeRoleARN: assumeRoleARN,
288289
AssumeRoleSessionName: sessionName,
289290
OnTokenSign: onTokenSign,
290-
}, nil
291+
}
292+
293+
if searchPath, ok := q["search_path"]; ok {
294+
if len(searchPath) > 1 {
295+
return nil, fmt.Errorf("Multiple search_path values specified")
296+
}
297+
p = WithSchemaSearchPath(p, searchPath[0])
298+
}
299+
300+
return p, nil
291301
}

0 commit comments

Comments
 (0)