Skip to content

Commit

Permalink
Scope enums by their enclosing schema
Browse files Browse the repository at this point in the history
  • Loading branch information
insano10 committed Oct 5, 2024
1 parent d898345 commit 20b1e62
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -500,24 +500,34 @@ func (p *Postgres) getFunctionsByQuery(query string) ([]*schema.Function, error)
func (p *Postgres) getEnums() ([]*schema.Enum, error) {
enums := []*schema.Enum{}

enumsResult, err := p.db.Query(`SELECT t.typname AS enum_name, ARRAY_AGG(e.enumlabel) AS enum_values
FROM pg_type t, pg_enum e
WHERE t.typcategory = 'E'
AND t.oid = e.enumtypid
GROUP BY t.typname `)
enumsResult, err := p.db.Query(`SELECT n.nspname, t.typname AS enum_name, ARRAY_AGG(e.enumlabel) AS enum_values
FROM pg_type t, pg_enum e, pg_catalog.pg_namespace n
WHERE t.typcategory = 'E'
AND t.oid = e.enumtypid
AND n.oid = t.typnamespace
GROUP BY n.nspname, t.typname `)

if err != nil {
return nil, errors.WithStack(err)
}
defer enumsResult.Close()

for enumsResult.Next() {
var enum schema.Enum
err := enumsResult.Scan(&enum.Name, pq.Array(&enum.Values))
var (
schemaName string
enumName string
enumValues []string
)
err := enumsResult.Scan(&schemaName, &enumName, pq.Array(&enumValues))
if err != nil {
return enums, errors.WithStack(err)
}
enums = append(enums, &enum)

enum := &schema.Enum{
Name: fmt.Sprintf("%s.%s", schemaName, enumName),
Values: enumValues,
}
enums = append(enums, enum)
}
return enums, nil
}
Expand Down

0 comments on commit 20b1e62

Please sign in to comment.