diff --git a/.golangci.yml b/.golangci.yml
index 007af930f..ff919bbe3 100644
--- a/.golangci.yml
+++ b/.golangci.yml
@@ -1,3 +1,5 @@
+run:
+ timeout: 5m
linters:
fast: false
enable:
@@ -7,7 +9,7 @@ linters-settings:
errcheck:
check-type-assertions: true
staticcheck:
- go: 1.16
+ go: "1.21"
misspell:
locale: US
ignore-words: []
diff --git a/output/md/md.go b/output/md/md.go
index 362dcfd7e..3d5b09ce8 100644
--- a/output/md/md.go
+++ b/output/md/md.go
@@ -129,6 +129,11 @@ func (m *Md) OutputViewpoint(wr io.Writer, i int, v *schema.Viewpoint) error {
// Output generate markdown files.
func Output(s *schema.Schema, c *config.Config, force bool) (e error) {
+ s, err := s.SetViewpointsToTables()
+ if err != nil {
+ return errors.WithStack(err)
+ }
+
docPath := c.DocPath
fullPath, err := filepath.Abs(docPath)
@@ -533,6 +538,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
number := m.config.Format.Number
adjust := m.config.Format.Adjust
hideColumns := m.config.Format.HideColumnsWithoutValues
+ showOnlyFirstParagraph := m.config.Format.ShowOnlyFirstParagraph
// Columns
columnsData := [][]string{}
@@ -588,6 +594,28 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
columnsData = append(columnsData, data)
}
+ // Viewpoints
+ viewpointsData := [][]string{
+ []string{
+ m.config.MergedDict.Lookup("Name"),
+ m.config.MergedDict.Lookup("Definition"),
+ },
+ []string{"----", "----------"},
+ }
+
+ for _, v := range t.Viewpoints {
+ desc := v.Desc
+ if showOnlyFirstParagraph {
+ desc = output.ShowOnlyFirstParagraph(desc)
+ }
+ data := []string{
+ fmt.Sprintf("[%s](viewpoint-%d.md)", v.Name, v.Index),
+ desc,
+ }
+
+ viewpointsData = append(viewpointsData, data)
+ }
+
// Constraints
constraintsData := [][]string{
[]string{
@@ -698,6 +726,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
return map[string]interface{}{
"Table": t,
"Columns": adjustTable(columnsData),
+ "Viewpoints": adjustTable(viewpointsData),
"Constraints": adjustTable(constraintsData),
"Indexes": adjustTable(indexesData),
"Triggers": adjustTable(triggersData),
@@ -708,6 +737,7 @@ func (m *Md) makeTableTemplateData(t *schema.Table) map[string]interface{} {
return map[string]interface{}{
"Table": t,
"Columns": columnsData,
+ "Viewpoints": viewpointsData,
"Constraints": constraintsData,
"Indexes": indexesData,
"Triggers": triggersData,
diff --git a/output/md/md_test.go b/output/md/md_test.go
index e58c38b90..2030c0e95 100644
--- a/output/md/md_test.go
+++ b/output/md/md_test.go
@@ -3,6 +3,7 @@ package md
import (
"os"
"path/filepath"
+ "slices"
"testing"
"github.com/k1LoW/tbls/config"
@@ -62,6 +63,11 @@ func TestOutput(t *testing.T) {
t.Fatal(err)
}
tb.Name = tt.tableBName
+ for _, v := range s.Viewpoints {
+ if vti := slices.Index(v.Tables, "b"); vti != -1 {
+ v.Tables[vti] = tt.tableBName
+ }
+ }
c, err := config.New()
if err != nil {
t.Error(err)
diff --git a/output/md/templates/table.md.tmpl b/output/md/templates/table.md.tmpl
index 25be0ebcd..1fd95de1e 100644
--- a/output/md/templates/table.md.tmpl
+++ b/output/md/templates/table.md.tmpl
@@ -34,7 +34,14 @@
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
{{- end }}
-{{ $len := len .Constraints }}{{ if ne $len 2 -}}
+{{ $len := len .Viewpoints }}{{ if ne $len 2 -}}
+## {{ "Viewpoints" | lookup }}
+{{ range $l := .Viewpoints }}
+|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
+{{- end }}
+
+{{ end -}}
+{{ $len := len .Constraints -}}{{ if ne $len 2 -}}
## {{ "Constraints" | lookup }}
{{ range $l := .Constraints }}
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
diff --git a/schema/schema.go b/schema/schema.go
index 6097e76da..7f9a352b7 100644
--- a/schema/schema.go
+++ b/schema/schema.go
@@ -131,19 +131,26 @@ type Column struct {
HideForER bool `json:"-"`
}
+type TableViewpoint struct {
+ Index int `json:"index"`
+ Name string `json:"name"`
+ Desc string `json:"desc"`
+}
+
// Table is the struct for database table
type Table struct {
- Name string `json:"name"`
- Type string `json:"type"`
- Comment string `json:"comment"`
- Columns []*Column `json:"columns"`
- Indexes []*Index `json:"indexes"`
- Constraints []*Constraint `json:"constraints"`
- Triggers []*Trigger `json:"triggers"`
- Def string `json:"def"`
- Labels Labels `json:"labels,omitempty"`
- ReferencedTables []*Table `json:"referenced_tables,omitempty" yaml:"referencedTables,omitempty"`
- External bool `json:"-"` // Table external to the schema
+ Name string `json:"name"`
+ Type string `json:"type"`
+ Comment string `json:"comment"`
+ Columns []*Column `json:"columns"`
+ Viewpoints []*TableViewpoint `json:"viewpoints"`
+ Indexes []*Index `json:"indexes"`
+ Constraints []*Constraint `json:"constraints"`
+ Triggers []*Trigger `json:"triggers"`
+ Def string `json:"def"`
+ Labels Labels `json:"labels,omitempty"`
+ ReferencedTables []*Table `json:"referenced_tables,omitempty" yaml:"referencedTables,omitempty"`
+ External bool `json:"-"` // Table external to the schema
}
// Relation is the struct for table relation
@@ -192,6 +199,24 @@ type Schema struct {
Viewpoints Viewpoints `json:"viewpoints,omitempty"`
}
+func (s *Schema) SetViewpointsToTables() (*Schema, error) {
+ for vi, v := range s.Viewpoints {
+ // Add viewpoints to table
+ for _, t := range v.Tables {
+ table, err := s.FindTableByName(t)
+ if err != nil {
+ return s, err
+ }
+ table.Viewpoints = append(table.Viewpoints, &TableViewpoint{
+ Index: vi,
+ Name: v.Name,
+ Desc: v.Desc,
+ })
+ }
+ }
+ return s, nil
+}
+
func (s *Schema) NormalizeTableName(name string) string {
if s.Driver != nil && s.Driver.Meta != nil && s.Driver.Meta.CurrentSchema != "" && (s.Driver.Name == "postgres" || s.Driver.Name == "redshift") && !strings.Contains(name, ".") {
return fmt.Sprintf("%s.%s", s.Driver.Meta.CurrentSchema, name)
diff --git a/schema/schema_test.go b/schema/schema_test.go
index 6678e6dfe..bc023f368 100644
--- a/schema/schema_test.go
+++ b/schema/schema_test.go
@@ -11,6 +11,54 @@ import (
"github.com/google/go-cmp/cmp"
)
+func TestSetViewpointsToTables(t *testing.T) {
+ viewpointAName := "va"
+ viewpointBName := "vb"
+
+ tests := []struct {
+ viewpointATables []string
+ viewpointBTables []string
+ wantTableAViewpoints []*TableViewpoint
+ }{
+ {[]string{"a"}, []string{"b"}, []*TableViewpoint{{Name: viewpointAName}}},
+ {[]string{"a", "b"}, []string{"a"}, []*TableViewpoint{{
+ Index: 0,
+ Name: viewpointAName,
+ }, {
+ Index: 1,
+ Name: viewpointBName,
+ }}},
+ }
+
+ for _, tt := range tests {
+ t.Run(fmt.Sprintf("%v", tt.viewpointATables), func(t *testing.T) {
+ fmt.Println(tt.viewpointATables)
+ s := newTestSchema(t)
+ s.Viewpoints = []*Viewpoint{
+ {
+ Name: viewpointAName,
+ Tables: tt.viewpointATables,
+ },
+ {
+ Name: viewpointBName,
+ Tables: tt.viewpointBTables,
+ },
+ }
+ result, err := s.SetViewpointsToTables()
+ if err != nil {
+ t.Error(err)
+ }
+ gotTable, _ := result.FindTableByName("a")
+ got := gotTable.Viewpoints
+ want := tt.wantTableAViewpoints
+
+ if diff := cmp.Diff(got, want, nil); diff != "" {
+ t.Errorf("%s", diff)
+ }
+ })
+ }
+}
+
func TestNormalizeTableName(t *testing.T) {
tests := []struct {
s *Schema
@@ -212,11 +260,11 @@ func TestSchema_Sort(t *testing.T) {
},
Functions: []*Function{
&Function{
- Name: "b",
+ Name: "b",
Arguments: "arg b",
},
&Function{
- Name: "b",
+ Name: "b",
Arguments: "arg a",
},
},
diff --git a/testdata/md_template_test_a.md.golden b/testdata/md_template_test_a.md.golden
index 63a8fe1c4..10f668f9c 100644
--- a/testdata/md_template_test_a.md.golden
+++ b/testdata/md_template_test_a.md.golden
@@ -19,6 +19,13 @@ THIS IS TABLE A
| a | INTEGER | | false | [b](b.md) | | COLUMN A |
| a2 | TEXT | | false | | | column a2 |
+## Viewpoints
+
+| Name | Definition |
+| ---- | ---------- |
+| [table a b](viewpoint-0.md) | select table a and b |
+| [table a label red](viewpoint-3.md) | select table a and label red
- table a
- label red |
+
## Constraints
| Name | Type | Definition | Comment |
diff --git a/testdata/md_test_a.md.first_para.golden b/testdata/md_test_a.md.first_para.golden
index 84d6a0734..a680945b7 100644
--- a/testdata/md_test_a.md.first_para.golden
+++ b/testdata/md_test_a.md.first_para.golden
@@ -15,6 +15,13 @@ TABLE A
| a | INTEGER | | false | [b](b.md) | | COLUMN A |
| a2 | TEXT | | false | | | column a2 |
+## Viewpoints
+
+| Name | Definition |
+| ---- | ---------- |
+| [table a b](viewpoint-0.md) | select table a and b |
+| [table a label red](viewpoint-3.md) | select table a and label red |
+
## Constraints
| Name | Type | Definition | Comment |
diff --git a/testdata/md_test_a.md.golden b/testdata/md_test_a.md.golden
index f86eb5f5d..5696e690e 100644
--- a/testdata/md_test_a.md.golden
+++ b/testdata/md_test_a.md.golden
@@ -15,6 +15,13 @@ TABLE A
| a | INTEGER | | false | [b](b.md) | | COLUMN A |
| a2 | TEXT | | false | | | column a2 |
+## Viewpoints
+
+| Name | Definition |
+| ---- | ---------- |
+| [table a b](viewpoint-0.md) | select table a and b |
+| [table a label red](viewpoint-3.md) | select table a and label red
- table a
- label red |
+
## Constraints
| Name | Type | Definition | Comment |
diff --git a/testdata/md_test_a.md.mermaid.golden b/testdata/md_test_a.md.mermaid.golden
index 30eae61d6..453fc53f1 100644
--- a/testdata/md_test_a.md.mermaid.golden
+++ b/testdata/md_test_a.md.mermaid.golden
@@ -15,6 +15,13 @@ TABLE A
| a | INTEGER | | false | [b](b.md) | | COLUMN A |
| a2 | TEXT | | false | | | column a2 |
+## Viewpoints
+
+| Name | Definition |
+| ---- | ---------- |
+| [table a b](viewpoint-0.md) | select table a and b |
+| [table a label red](viewpoint-3.md) | select table a and label red
- table a
- label red |
+
## Constraints
| Name | Type | Definition | Comment |
diff --git a/testdata/templates/table.md.tmpl b/testdata/templates/table.md.tmpl
index 362913991..e54c4ca6f 100644
--- a/testdata/templates/table.md.tmpl
+++ b/testdata/templates/table.md.tmpl
@@ -31,7 +31,14 @@
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
{{- end }}
-{{ $len := len .Constraints }}{{ if ne $len 2 -}}
+{{ $len := len .Viewpoints }}{{ if ne $len 2 -}}
+## {{ "Viewpoints" | lookup }}
+{{ range $l := .Viewpoints }}
+|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}
+{{- end }}
+
+{{ end -}}
+{{ $len := len .Constraints -}}{{ if ne $len 2 -}}
## {{ "Constraints" | lookup }}
{{ range $l := .Constraints }}
|{{ range $d := $l }} {{ $d | nl2br }} |{{ end }}