Skip to content

Commit

Permalink
Merge pull request #594 from mjpieters/postgres-user-searchpath
Browse files Browse the repository at this point in the history
Postgres: Support "$user" search path
  • Loading branch information
k1LoW authored Jun 19, 2024
2 parents c89160e + 3833253 commit baa4ea0
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 baa4ea0

Please sign in to comment.