Skip to content

Commit 76d30f8

Browse files
authored
Merge pull request #601 from insano10/postgres-enums
Add support to output enum definitions in the db doc README
2 parents b85fa2f + 20b1e62 commit 76d30f8

18 files changed

+184
-0
lines changed

config/templates.go

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ type MD struct {
1515
Index string `yaml:"index,omitempty"`
1616
Table string `yaml:"table,omitempty"`
1717
Viewpoint string `yaml:"viewpoint,omitempty"`
18+
Enum string `yaml:"enum,omitempty"`
1819
}
1920

2021
// Dot holds the paths to the dot template files.

drivers/postgres/postgres.go

+42
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,13 @@ ORDER BY tgrelid
329329
}
330330
s.Functions = functions
331331

332+
// Enums
333+
enums, err := p.getEnums()
334+
if err != nil {
335+
return err
336+
}
337+
s.Enums = enums
338+
332339
s.Tables = tables
333340

334341
// Relations
@@ -490,6 +497,41 @@ func (p *Postgres) getFunctionsByQuery(query string) ([]*schema.Function, error)
490497
return functions, nil
491498
}
492499

500+
func (p *Postgres) getEnums() ([]*schema.Enum, error) {
501+
enums := []*schema.Enum{}
502+
503+
enumsResult, err := p.db.Query(`SELECT n.nspname, t.typname AS enum_name, ARRAY_AGG(e.enumlabel) AS enum_values
504+
FROM pg_type t, pg_enum e, pg_catalog.pg_namespace n
505+
WHERE t.typcategory = 'E'
506+
AND t.oid = e.enumtypid
507+
AND n.oid = t.typnamespace
508+
GROUP BY n.nspname, t.typname `)
509+
510+
if err != nil {
511+
return nil, errors.WithStack(err)
512+
}
513+
defer enumsResult.Close()
514+
515+
for enumsResult.Next() {
516+
var (
517+
schemaName string
518+
enumName string
519+
enumValues []string
520+
)
521+
err := enumsResult.Scan(&schemaName, &enumName, pq.Array(&enumValues))
522+
if err != nil {
523+
return enums, errors.WithStack(err)
524+
}
525+
526+
enum := &schema.Enum{
527+
Name: fmt.Sprintf("%s.%s", schemaName, enumName),
528+
Values: enumValues,
529+
}
530+
enums = append(enums, enum)
531+
}
532+
return enums, nil
533+
}
534+
493535
func fullTableName(owner string, tableName string) string {
494536
return fmt.Sprintf("%s.%s", owner, tableName)
495537
}

output/md/md.go

+34
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"io"
88
"os"
99
"path/filepath"
10+
"sort"
1011
"strconv"
1112
"strings"
1213
"text/template"
@@ -521,11 +522,15 @@ func (m *Md) makeSchemaTemplateData(s *schema.Schema) map[string]interface{} {
521522
// Viewpoints
522523
viewpointsData := m.viewpointsData(s.Viewpoints, number, adjust, showOnlyFirstParagraph)
523524

525+
// Enums
526+
enumData := m.enumData(s.Enums)
527+
524528
return map[string]interface{}{
525529
"Schema": s,
526530
"Tables": tablesData,
527531
"Functions": functionsData,
528532
"Viewpoints": viewpointsData,
533+
"Enums": enumData,
529534
}
530535
}
531536

@@ -873,6 +878,35 @@ func (m *Md) functionsData(functions []*schema.Function, number, adjust, showOnl
873878
return data
874879
}
875880

881+
func (m *Md) enumData(enums []*schema.Enum) [][]string {
882+
data := [][]string{}
883+
884+
if len(enums) == 0 {
885+
return data
886+
}
887+
888+
header := []string{
889+
m.config.MergedDict.Lookup("Name"),
890+
m.config.MergedDict.Lookup("Values"),
891+
}
892+
headerLine := []string{"----", "-------"}
893+
data = append(data,
894+
header,
895+
headerLine,
896+
)
897+
898+
for _, e := range enums {
899+
sort.Strings(e.Values)
900+
d := []string{
901+
e.Name,
902+
strings.Join(e.Values, ", "),
903+
}
904+
data = append(data, d)
905+
}
906+
907+
return data
908+
}
909+
876910
func (m *Md) viewpointsData(viewpoints []*schema.Viewpoint, number, adjust, showOnlyFirstParagraph bool) [][]string {
877911
data := [][]string{}
878912
header := []string{

output/md/templates/index.md.tmpl

+9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
|{{ range $d := $t }} {{ $d | nl2br }} |{{ end }}
3232
{{- end -}}
3333
{{- end -}}
34+
35+
{{ if ne (len .Enums) 0 }}
36+
37+
## {{ "Enums" | lookup }}
38+
{{ range $t := .Enums }}
39+
|{{ range $d := $t }} {{ $d | nl2br }} |{{ end }}
40+
{{- end -}}
41+
{{- end -}}
42+
3443
{{- if .er }}
3544

3645
## {{ "Relations" | lookup }}

sample/adjust/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ Sample PostgreSQL database document.
4242
| public.update_updated | trigger | | FUNCTION |
4343
| public.reset_comment | void | IN comment_id integer | PROCEDURE |
4444

45+
## Enums
46+
47+
| Name | Values |
48+
| ---- | ------- |
49+
| public.post_types | draft, private, public |
50+
4551
## Relations
4652

4753
![er](schema.svg)

sample/postgres/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ Sample PostgreSQL database document.
4242
| public.update_updated | trigger | | FUNCTION |
4343
| public.reset_comment | void | IN comment_id integer | PROCEDURE |
4444

45+
## Enums
46+
47+
| Name | Values |
48+
| ---- | ------- |
49+
| public.post_types | draft, private, public |
50+
4551
## Relations
4652

4753
![er](schema.svg)

sample/postgres95/README.md

+6
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ Sample PostgreSQL database document.
4141
| public.uuid_generate_v5 | uuid | namespace uuid, name text | FUNCTION |
4242
| public.update_updated | trigger | | FUNCTION |
4343

44+
## Enums
45+
46+
| Name | Values |
47+
| ---- | ------- |
48+
| public.post_types | draft, private, public |
49+
4450
## Relations
4551

4652
![er](schema.svg)

schema/json.go

+13
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ func (s Schema) MarshalJSON() ([]byte, error) {
1919
Tables []*Table `json:"tables"`
2020
Relations []*Relation `json:"relations"`
2121
Functions []*Function `json:"functions"`
22+
Enums []*Enum `json:"enums,omitempty"`
2223
Driver *Driver `json:"driver"`
2324
Labels Labels `json:"labels,omitempty"`
2425
Viewpoints []*Viewpoint `json:"viewpoints,omitempty"`
@@ -29,6 +30,7 @@ func (s Schema) MarshalJSON() ([]byte, error) {
2930
Relations: s.Relations,
3031
Driver: s.Driver,
3132
Functions: s.Functions,
33+
Enums: s.Enums,
3234
Labels: s.Labels,
3335
Viewpoints: s.Viewpoints,
3436
})
@@ -49,6 +51,17 @@ func (d Function) MarshalJSON() ([]byte, error) {
4951
})
5052
}
5153

54+
// MarshalJSON return custom JSON byte
55+
func (e Enum) MarshalJSON() ([]byte, error) {
56+
return json.Marshal(&struct {
57+
Name string `json:"name"`
58+
Values []string `json:"values"`
59+
}{
60+
Name: e.Name,
61+
Values: e.Values,
62+
})
63+
}
64+
5265
// MarshalJSON return custom JSON byte
5366
func (d Driver) MarshalJSON() ([]byte, error) {
5467
if d.Meta == nil {

schema/schema.go

+6
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,11 @@ type Function struct {
180180
Type string `json:"type"`
181181
}
182182

183+
type Enum struct {
184+
Name string `json:"name"`
185+
Values []string `json:"values"`
186+
}
187+
183188
// Driver is the struct for tbls driver information
184189
type Driver struct {
185190
Name string `json:"name"`
@@ -194,6 +199,7 @@ type Schema struct {
194199
Tables []*Table `json:"tables"`
195200
Relations []*Relation `json:"relations"`
196201
Functions []*Function `json:"functions"`
202+
Enums []*Enum `json:"enums,omitempty"`
197203
Driver *Driver `json:"driver"`
198204
Labels Labels `json:"labels,omitempty"`
199205
Viewpoints Viewpoints `json:"viewpoints,omitempty"`

testdata/json_output_schema.golden

+10
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,16 @@
139139
}
140140
],
141141
"functions": null,
142+
"enums": [
143+
{
144+
"name": "enum",
145+
"values": [
146+
"one",
147+
"two",
148+
"three"
149+
]
150+
}
151+
],
142152
"driver": {
143153
"name": "testdriver",
144154
"database_version": "1.0.0",

testdata/md_test_README.md.adjust.golden

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
| [b](b.md) | 2 | table b | | `red` `green` |
1818
| [view](view.md) | 1 | view | VIEW | |
1919

20+
## Enums
21+
22+
| Name | Values |
23+
| ---- | ------- |
24+
| enum | one, three, two |
25+
2026
---
2127

2228
> Generated by [tbls](https://github.com/k1LoW/tbls)

testdata/md_test_README.md.first_para.golden

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
| [b](b.md) | 2 | table b | | `red` `green` |
1818
| [view](view.md) | 1 | view | VIEW | |
1919

20+
## Enums
21+
22+
| Name | Values |
23+
| ---- | ------- |
24+
| enum | one, three, two |
25+
2026
## Relations
2127

2228
![er](schema.png)

testdata/md_test_README.md.golden

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
| [b](b.md) | 2 | table b | | `red` `green` |
1818
| [view](view.md) | 1 | view | VIEW | |
1919

20+
## Enums
21+
22+
| Name | Values |
23+
| ---- | ------- |
24+
| enum | one, three, two |
25+
2026
## Relations
2127

2228
![er](schema.png)

testdata/md_test_README.md.mermaid.golden

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
| [b](b.md) | 2 | table b | | `red` `green` |
1818
| [view](view.md) | 1 | view | VIEW | |
1919

20+
## Enums
21+
22+
| Name | Values |
23+
| ---- | ------- |
24+
| enum | one, three, two |
25+
2026
## Relations
2127

2228
```mermaid

testdata/md_test_README.md.number.golden

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
| 2 | [b](b.md) | 2 | table b | | `red` `green` |
1818
| 3 | [view](view.md) | 1 | view | VIEW | |
1919

20+
## Enums
21+
22+
| Name | Values |
23+
| ---- | ------- |
24+
| enum | one, three, two |
25+
2026
---
2127

2228
> Generated by [tbls](https://github.com/k1LoW/tbls)

testdata/md_test_README.md.space_in_table_name.golden

+6
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
| [a b](a%20b.md) | 2 | table b | | `red` `green` |
1818
| [view](view.md) | 1 | view | VIEW | |
1919

20+
## Enums
21+
22+
| Name | Values |
23+
| ---- | ------- |
24+
| enum | one, three, two |
25+
2026
---
2127

2228
> Generated by [tbls](https://github.com/k1LoW/tbls)

testdata/yaml_output_schema.golden

+6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@ relations:
9292
def: FOREIGN KEY (b) REFERENCES a(a)
9393
virtual: false
9494
functions: []
95+
enums:
96+
- name: enum
97+
values:
98+
- one
99+
- two
100+
- three
95101
driver:
96102
name: testdriver
97103
databaseVersion: 1.0.0

testutil/schema.go

+9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ func NewSchema(t *testing.T) *schema.Schema {
1414
labelBlueName = "blue"
1515
labelRedName = "red"
1616
labelGreenName = "green"
17+
enumName = "enum"
1718
)
1819

1920
labelBlue := &schema.Label{
@@ -108,6 +109,11 @@ func NewSchema(t *testing.T) *schema.Schema {
108109
},
109110
}
110111

112+
enum := &schema.Enum{
113+
Name: enumName,
114+
Values: []string{"one", "two", "three"},
115+
}
116+
111117
r := &schema.Relation{
112118
Table: tb,
113119
Columns: []*schema.Column{cb},
@@ -128,6 +134,9 @@ func NewSchema(t *testing.T) *schema.Schema {
128134
tb,
129135
tView,
130136
},
137+
Enums: []*schema.Enum{
138+
enum,
139+
},
131140
Relations: []*schema.Relation{
132141
r,
133142
},

0 commit comments

Comments
 (0)