Skip to content

Commit

Permalink
Add support to output Postgres enum definitions in the db doc README
Browse files Browse the repository at this point in the history
  • Loading branch information
insano10 committed Sep 23, 2024
1 parent a1cebf6 commit af18d5e
Show file tree
Hide file tree
Showing 15 changed files with 156 additions and 0 deletions.
1 change: 1 addition & 0 deletions config/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ type MD struct {
Index string `yaml:"index,omitempty"`
Table string `yaml:"table,omitempty"`
Viewpoint string `yaml:"viewpoint,omitempty"`
Enum string `yaml:"enum,omitempty"`
}

// Dot holds the paths to the dot template files.
Expand Down
32 changes: 32 additions & 0 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,13 @@ ORDER BY tgrelid
}
s.Functions = functions

// Enums
enums, err := p.getEnums()
if err != nil {
return err
}
s.Enums = enums

s.Tables = tables

// Relations
Expand Down Expand Up @@ -490,6 +497,31 @@ func (p *Postgres) getFunctionsByQuery(query string) ([]*schema.Function, error)
return functions, nil
}

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 `)

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))
if err != nil {
return enums, errors.WithStack(err)
}
enums = append(enums, &enum)
}
return enums, nil
}

func fullTableName(owner string, tableName string) string {
return fmt.Sprintf("%s.%s", owner, tableName)
}
Expand Down
34 changes: 34 additions & 0 deletions output/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -521,11 +522,15 @@ func (m *Md) makeSchemaTemplateData(s *schema.Schema) map[string]interface{} {
// Viewpoints
viewpointsData := m.viewpointsData(s.Viewpoints, number, adjust, showOnlyFirstParagraph)

// Enums
enumData := m.enumData(s.Enums)

return map[string]interface{}{
"Schema": s,
"Tables": tablesData,
"Functions": functionsData,
"Viewpoints": viewpointsData,
"Enums": enumData,
}
}

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

func (m *Md) enumData(enums []*schema.Enum) [][]string {
data := [][]string{}

if len(enums) == 0 {
return data
}

header := []string{
m.config.MergedDict.Lookup("Name"),
m.config.MergedDict.Lookup("Values"),
}
headerLine := []string{"----", "-------"}
data = append(data,
header,
headerLine,
)

for _, e := range enums {
sort.Strings(e.Values)
d := []string{
e.Name,
strings.Join(e.Values, ", "),
}
data = append(data, d)
}

return data
}

func (m *Md) viewpointsData(viewpoints []*schema.Viewpoint, number, adjust, showOnlyFirstParagraph bool) [][]string {
data := [][]string{}
header := []string{
Expand Down
9 changes: 9 additions & 0 deletions output/md/templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@
|{{ range $d := $t }} {{ $d | nl2br }} |{{ end }}
{{- end -}}
{{- end -}}

{{ if ne (len .Enums) 0 }}

## {{ "Enums" | lookup }}
{{ range $t := .Enums }}
|{{ range $d := $t }} {{ $d | nl2br }} |{{ end }}
{{- end -}}
{{- end -}}

{{- if .er }}

## {{ "Relations" | lookup }}
Expand Down
13 changes: 13 additions & 0 deletions schema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func (s Schema) MarshalJSON() ([]byte, error) {
Tables []*Table `json:"tables"`
Relations []*Relation `json:"relations"`
Functions []*Function `json:"functions"`
Enums []*Enum `json:"enums,omitempty"`
Driver *Driver `json:"driver"`
Labels Labels `json:"labels,omitempty"`
Viewpoints []*Viewpoint `json:"viewpoints,omitempty"`
Expand All @@ -29,6 +30,7 @@ func (s Schema) MarshalJSON() ([]byte, error) {
Relations: s.Relations,
Driver: s.Driver,
Functions: s.Functions,
Enums: s.Enums,
Labels: s.Labels,
Viewpoints: s.Viewpoints,
})
Expand All @@ -49,6 +51,17 @@ func (d Function) MarshalJSON() ([]byte, error) {
})
}

// MarshalJSON return custom JSON byte
func (e Enum) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Name string `json:"name"`
Values []string `json:"values"`
}{
Name: e.Name,
Values: e.Values,
})
}

// MarshalJSON return custom JSON byte
func (d Driver) MarshalJSON() ([]byte, error) {
if d.Meta == nil {
Expand Down
6 changes: 6 additions & 0 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,11 @@ type Function struct {
Type string `json:"type"`
}

type Enum struct {
Name string `json:"name"`
Values []string `json:"values"`
}

// Driver is the struct for tbls driver information
type Driver struct {
Name string `json:"name"`
Expand All @@ -194,6 +199,7 @@ type Schema struct {
Tables []*Table `json:"tables"`
Relations []*Relation `json:"relations"`
Functions []*Function `json:"functions"`
Enums []*Enum `json:"enums,omitempty"`
Driver *Driver `json:"driver"`
Labels Labels `json:"labels,omitempty"`
Viewpoints Viewpoints `json:"viewpoints,omitempty"`
Expand Down
10 changes: 10 additions & 0 deletions testdata/json_output_schema.golden
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,16 @@
}
],
"functions": null,
"enums": [
{
"name": "enum",
"values": [
"one",
"two",
"three"
]
}
],
"driver": {
"name": "testdriver",
"database_version": "1.0.0",
Expand Down
6 changes: 6 additions & 0 deletions testdata/md_test_README.md.adjust.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
| [b](b.md) | 2 | table b | | `red` `green` |
| [view](view.md) | 1 | view | VIEW | |

## Enums

| Name | Values |
| ---- | ------- |
| enum | one, three, two |

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
6 changes: 6 additions & 0 deletions testdata/md_test_README.md.first_para.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
| [b](b.md) | 2 | table b | | `red` `green` |
| [view](view.md) | 1 | view | VIEW | |

## Enums

| Name | Values |
| ---- | ------- |
| enum | one, three, two |

## Relations

![er](schema.png)
Expand Down
6 changes: 6 additions & 0 deletions testdata/md_test_README.md.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
| [b](b.md) | 2 | table b | | `red` `green` |
| [view](view.md) | 1 | view | VIEW | |

## Enums

| Name | Values |
| ---- | ------- |
| enum | one, three, two |

## Relations

![er](schema.png)
Expand Down
6 changes: 6 additions & 0 deletions testdata/md_test_README.md.mermaid.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
| [b](b.md) | 2 | table b | | `red` `green` |
| [view](view.md) | 1 | view | VIEW | |

## Enums

| Name | Values |
| ---- | ------- |
| enum | one, three, two |

## Relations

```mermaid
Expand Down
6 changes: 6 additions & 0 deletions testdata/md_test_README.md.number.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
| 2 | [b](b.md) | 2 | table b | | `red` `green` |
| 3 | [view](view.md) | 1 | view | VIEW | |

## Enums

| Name | Values |
| ---- | ------- |
| enum | one, three, two |

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
6 changes: 6 additions & 0 deletions testdata/md_test_README.md.space_in_table_name.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,12 @@
| [a b](a%20b.md) | 2 | table b | | `red` `green` |
| [view](view.md) | 1 | view | VIEW | |

## Enums

| Name | Values |
| ---- | ------- |
| enum | one, three, two |

---

> Generated by [tbls](https://github.com/k1LoW/tbls)
6 changes: 6 additions & 0 deletions testdata/yaml_output_schema.golden
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ relations:
def: FOREIGN KEY (b) REFERENCES a(a)
virtual: false
functions: []
enums:
- name: enum
values:
- one
- two
- three
driver:
name: testdriver
databaseVersion: 1.0.0
Expand Down
9 changes: 9 additions & 0 deletions testutil/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func NewSchema(t *testing.T) *schema.Schema {
labelBlueName = "blue"
labelRedName = "red"
labelGreenName = "green"
enumName = "enum"
)

labelBlue := &schema.Label{
Expand Down Expand Up @@ -108,6 +109,11 @@ func NewSchema(t *testing.T) *schema.Schema {
},
}

enum := &schema.Enum{
Name: enumName,
Values: []string{"one", "two", "three"},
}

r := &schema.Relation{
Table: tb,
Columns: []*schema.Column{cb},
Expand All @@ -128,6 +134,9 @@ func NewSchema(t *testing.T) *schema.Schema {
tb,
tView,
},
Enums: []*schema.Enum{
enum,
},
Relations: []*schema.Relation{
r,
},
Expand Down

0 comments on commit af18d5e

Please sign in to comment.