Skip to content

Commit

Permalink
Postgres: Support "$user" search path
Browse files Browse the repository at this point in the history
This simply replaces the string `"$user"` in the Postgres search path
with the current username.
  • Loading branch information
mjpieters committed Jun 19, 2024
1 parent 20ece3a commit 3833253
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,27 @@ func (p *Postgres) Analyze(s *schema.Schema) error {
return errors.WithStack(err)
}
}
s.Driver.Meta.SearchPaths = strings.Split(searchPaths, ", ")
splitPaths := strings.Split(searchPaths, ", ")
// Replace "$user" with the current username
for idx, path := range splitPaths {
if path == `"$user"` {
var userName string
userNameRows, err := p.db.Query(`SELECT current_user`)
if err != nil {
return errors.WithStack(err)
}
defer userNameRows.Close()
for userNameRows.Next() {
err := userNameRows.Scan(&userName)
if err != nil {
return errors.WithStack(err)
}
}
splitPaths[idx] = userName
}
}

s.Driver.Meta.SearchPaths = splitPaths

fullTableNames := []string{}

Expand Down Expand Up @@ -649,7 +669,6 @@ func detectFullTableName(name string, searchPaths, fullTableNames []string) (str
for _, n := range fullTableNames {
if strings.HasSuffix(n, name) {
for _, p := range searchPaths {
// TODO: Support $user
if n == fmt.Sprintf("%s.%s", p, name) {
fns = append(fns, n)
}
Expand Down

0 comments on commit 3833253

Please sign in to comment.