Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Add er.hideDef: for hiding relation definition #417

Merged
merged 1 commit into from
Jan 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,9 @@ er:
# Add table/column comment to ER diagram
# Default is false
comment: true
# Hide relation definition from ER diagram
# Default is false
hideDef: true
# Distance between tables that display relations in the ER
# Default is 1
distance: 2
Expand Down
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type ER struct {
Skip bool `yaml:"skip,omitempty"`
Format string `yaml:"format,omitempty"`
Comment bool `yaml:"comment,omitempty"`
HideDef bool `yaml:"hideDef,omitempty"`
Distance *int `yaml:"distance,omitempty"`
Font string `yaml:"font,omitempty"`
}
Expand Down
2 changes: 2 additions & 0 deletions output/dot/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (d *Dot) OutputSchema(wr io.Writer, s *schema.Schema) error {
err = tmpl.Execute(wr, map[string]interface{}{
"Schema": s,
"showComment": d.config.ER.Comment,
"showDef": !d.config.ER.HideDef,
})
if err != nil {
return errors.WithStack(err)
Expand All @@ -96,6 +97,7 @@ func (d *Dot) OutputTable(wr io.Writer, t *schema.Table) error {
"Tables": tables[1:],
"Relations": relations,
"showComment": d.config.ER.Comment,
"showDef": !d.config.ER.HideDef,
})
if err != nil {
return errors.WithStack(err)
Expand Down
5 changes: 4 additions & 1 deletion output/dot/dot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (

func TestOutputSchema(t *testing.T) {
tests := []struct {
hideDef bool
wantFile string
}{
{"dot_test_schema.dot"},
{false, "dot_test_schema.dot"},
{true, "dot_test_schema.dot.hidedef"},
}
for _, tt := range tests {
t.Run(tt.wantFile, func(t *testing.T) {
Expand All @@ -24,6 +26,7 @@ func TestOutputSchema(t *testing.T) {
if err != nil {
t.Error(err)
}
c.ER.HideDef = tt.hideDef
if err := c.LoadConfigFile(filepath.Join(testdataDir(), "out_test_tbls.yml")); err != nil {
t.Error(err)
}
Expand Down
2 changes: 2 additions & 0 deletions output/mermaid/mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func (m *Mermaid) OutputSchema(wr io.Writer, s *schema.Schema) error {
err = tmpl.Execute(wr, map[string]interface{}{
"Schema": s,
"showComment": m.config.ER.Comment,
"showDef": !m.config.ER.HideDef,
})
if err != nil {
return errors.WithStack(err)
Expand All @@ -95,6 +96,7 @@ func (m *Mermaid) OutputTable(wr io.Writer, t *schema.Table) error {
"Tables": tables[1:],
"Relations": relations,
"showComment": m.config.ER.Comment,
"showDef": !m.config.ER.HideDef,
})
if err != nil {
return errors.WithStack(err)
Expand Down
58 changes: 34 additions & 24 deletions output/mermaid/mermaid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,40 @@ import (
)

func TestOutputSchema(t *testing.T) {
s := testutil.NewSchema(t)
c, err := config.New()
if err != nil {
t.Error(err)
}
if err := c.LoadConfigFile(filepath.Join(testdataDir(), "out_test_tbls.yml")); err != nil {
t.Error(err)
}
if err := c.MergeAdditionalData(s); err != nil {
t.Error(err)
}
o := New(c)
got := &bytes.Buffer{}
err = o.OutputSchema(got, s)
if err != nil {
t.Error(err)
}
f := fmt.Sprintf("mermaid_test_schema")
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, testdataDir(), f, got)
return
}
if diff := golden.Diff(t, testdataDir(), f, got); diff != "" {
t.Error(diff)
tests := []struct {
hideDef bool
wantFile string
}{
{false, "mermaid_test_schema"},
{true, "mermaid_test_schema.hidedef"},
}
for _, tt := range tests {
t.Run(tt.wantFile, func(t *testing.T) {
s := testutil.NewSchema(t)
c, err := config.New()
if err != nil {
t.Error(err)
}
if err := c.LoadConfigFile(filepath.Join(testdataDir(), "out_test_tbls.yml")); err != nil {
t.Error(err)
}
if err := c.ModifySchema(s); err != nil {
t.Error(err)
}
c.ER.HideDef = tt.hideDef
o := New(c)
got := &bytes.Buffer{}
if err := o.OutputSchema(got, s); err != nil {
t.Error(err)
}
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, testdataDir(), tt.wantFile, got)
return
}
if diff := golden.Diff(t, testdataDir(), tt.wantFile, got); diff != "" {
t.Error(diff)
}
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion output/mermaid/templates/schema.mermaid.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
erDiagram
{{ $sc := .showComment -}}
{{- $sd := .showDef -}}
{{- range $j, $r := .Schema.Relations }}
"{{ $r.Table.Name }}" {{ $r.Cardinality | lcardi }}--{{ $r.ParentCardinality | rcardi }} "{{ $r.ParentTable.Name }}" : "{{ $r.Def | html }}"
"{{ $r.Table.Name }}" {{ $r.Cardinality | lcardi }}--{{ $r.ParentCardinality | rcardi }} "{{ $r.ParentTable.Name }}" : "{{ if $sd }}{{ $r.Def | html }}{{ end }}"
{{- end }}
{{ range $i, $t := .Schema.Tables }}
"{{ $t.Name }}" {
Expand Down
3 changes: 2 additions & 1 deletion output/mermaid/templates/table.mermaid.tmpl
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
erDiagram
{{ $sc := .showComment -}}
{{ $sd := .showDef -}}
{{- range $j, $r := .Relations }}
"{{ $r.Table.Name }}" {{ $r.Cardinality | lcardi }}--{{ $r.ParentCardinality | rcardi }} "{{ $r.ParentTable.Name }}" : "{{ $r.Def | html }}"
"{{ $r.Table.Name }}" {{ $r.Cardinality | lcardi }}--{{ $r.ParentCardinality | rcardi }} "{{ $r.ParentTable.Name }}" : "{{ if $sd }}{{ $r.Def | html }}{{ end }}"
{{- end }}

"{{ .Table.Name }}" {
Expand Down
2 changes: 2 additions & 0 deletions output/plantuml/plantuml.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (p *PlantUML) OutputSchema(wr io.Writer, s *schema.Schema) error {
err = tmpl.Execute(wr, map[string]interface{}{
"Schema": s,
"showComment": p.config.ER.Comment,
"showDef": !p.config.ER.HideDef,
})
if err != nil {
return errors.WithStack(err)
Expand Down Expand Up @@ -110,6 +111,7 @@ func (p *PlantUML) OutputTable(wr io.Writer, t *schema.Table) error {
"Tables": tables[1:],
"Relations": relations,
"showComment": p.config.ER.Comment,
"showDef": !p.config.ER.HideDef,
})
if err != nil {
return errors.WithStack(err)
Expand Down
59 changes: 35 additions & 24 deletions output/plantuml/plantuml_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,41 @@ import (
)

func TestOutputSchema(t *testing.T) {
s := testutil.NewSchema(t)
c, err := config.New()
if err != nil {
t.Error(err)
}
if err := c.LoadConfigFile(filepath.Join(testdataDir(), "out_test_tbls.yml")); err != nil {
t.Error(err)
}
if err := c.MergeAdditionalData(s); err != nil {
t.Error(err)
}
o := New(c)
got := &bytes.Buffer{}
err = o.OutputSchema(got, s)
if err != nil {
t.Error(err)
}
f := fmt.Sprintf("plantuml_test_schema.puml")
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, testdataDir(), f, got)
return
}
if diff := golden.Diff(t, testdataDir(), f, got); diff != "" {
t.Error(diff)
tests := []struct {
hideDef bool
wantFile string
}{
{false, "plantuml_test_schema.puml"},
{true, "plantuml_test_schema.puml.hidedef"},
}
for _, tt := range tests {
t.Run(tt.wantFile, func(t *testing.T) {
s := testutil.NewSchema(t)
c, err := config.New()
if err != nil {
t.Error(err)
}
if err := c.LoadConfigFile(filepath.Join(testdataDir(), "out_test_tbls.yml")); err != nil {
t.Error(err)
}
if err := c.ModifySchema(s); err != nil {
t.Error(err)
}
c.ER.HideDef = tt.hideDef
o := New(c)
got := &bytes.Buffer{}
err = o.OutputSchema(got, s)
if err != nil {
t.Error(err)
}
if os.Getenv("UPDATE_GOLDEN") != "" {
golden.Update(t, testdataDir(), tt.wantFile, got)
return
}
if diff := golden.Diff(t, testdataDir(), tt.wantFile, got); diff != "" {
t.Error(diff)
}
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion output/plantuml/templates/schema.puml.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@startuml
{{ $sc := .showComment -}}
{{- $sd := .showDef -}}
!define table(name, desc) entity name as "desc" << (T,#5DBCD2) >>
!define view(name, desc) entity name as "desc" << (V,#C6EDDB) >>
!define column(name, type, desc) name <font color="#666666">[type]</font><font color="#333333">desc</font>
Expand Down Expand Up @@ -27,7 +28,7 @@ view("{{ $t.Name }}", "{{ $t.Name }}{{ if $sc }}{{ if ne $t.Comment "" }}\n{{ $t

' relations
{{- range $j, $r := .Schema.Relations }}
"{{ $r.Table.Name }}" {{ $r.Cardinality | lcardi }}--{{ $r.ParentCardinality | rcardi }} "{{ $r.ParentTable.Name }}" : "{{ $r.Def | html }}"
"{{ $r.Table.Name }}" {{ $r.Cardinality | lcardi }}--{{ $r.ParentCardinality | rcardi }} "{{ $r.ParentTable.Name }}" : "{{ if $sd }}{{ $r.Def | html }}{{ end }}"
{{- end }}

@enduml
3 changes: 2 additions & 1 deletion output/plantuml/templates/table.puml.tmpl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
@startuml
{{ $sc := .showComment -}}
{{- $sd := .showDef -}}
!define table(name, desc) entity name as "desc" << (T,#5DBCD2) >>
!define view(name, desc) entity name as "desc" << (V,#C6EDDB) >>
!define column(name, type, desc) name <font color="#666666">[type]</font><font color="#333333">desc</font>
Expand Down Expand Up @@ -36,7 +37,7 @@ view("{{ $t.Name }}", "{{ $t.Name }}{{ if $sc }}{{ if ne $t.Comment "" }}\n{{ $t

' relations
{{- range $j, $r := .Relations }}
"{{ $r.Table.Name }}" {{ $r.Cardinality | lcardi }}--{{ $r.ParentCardinality | rcardi }} "{{ $r.ParentTable.Name }}" : "{{ $r.Def | html }}"
"{{ $r.Table.Name }}" {{ $r.Cardinality | lcardi }}--{{ $r.ParentCardinality | rcardi }} "{{ $r.ParentTable.Name }}" : "{{ if $sd }}{{ $r.Def | html }}{{ end }}"
{{- end }}

@enduml
2 changes: 1 addition & 1 deletion testdata/dot_template_test_a.dot.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ digraph "a" {
</table>>];

// Relations
"b":"b" -> "a":"a" [dir=back, arrowtail=tee, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td></td></tr></table>>];
"b":"b" -> "a":"a" [dir=back, arrowtail=tee, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td>FOREIGN KEY (b) REFERENCES a(a)</td></tr></table>>];
}
2 changes: 1 addition & 1 deletion testdata/dot_template_test_schema.dot.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ digraph "testschema" {
</table>>];

// Relations
"b":"b" -> "a":"a" [dir=back, arrowtail=tee, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td></td></tr></table>>];
"b":"b" -> "a":"a" [dir=back, arrowtail=tee, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td>FOREIGN KEY (b) REFERENCES a(a)</td></tr></table>>];
}
2 changes: 1 addition & 1 deletion testdata/dot_test_a.dot.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ digraph "a" {
</table>>];

// Relations
"b":"b" -> "a":"a" [dir=back, arrowtail=crow, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td></td></tr></table>>];
"b":"b" -> "a":"a" [dir=back, arrowtail=crow, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td>FOREIGN KEY (b) REFERENCES a(a)</td></tr></table>>];
}
2 changes: 1 addition & 1 deletion testdata/dot_test_schema.dot.golden
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ digraph "testschema" {
</table>>];

// Relations
"b":"b" -> "a":"a" [dir=back, arrowtail=crow, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td></td></tr></table>>];
"b":"b" -> "a":"a" [dir=back, arrowtail=crow, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td>FOREIGN KEY (b) REFERENCES a(a)</td></tr></table>>];
}
21 changes: 21 additions & 0 deletions testdata/dot_test_schema.dot.hidedef.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
digraph "testschema" {
// Config
graph [rankdir=TB, layout=dot, fontname="Arial"];
node [shape=record, fontsize=14, margin=0.6, fontname="Arial"];
edge [fontsize=10, labelfloat=false, splines=none, fontname="Arial"];

// Tables
"a" [shape=none, label=<<table border="0" cellborder="1" cellspacing="0" cellpadding="6">
<tr><td bgcolor="#EFEFEF"><font face="Arial Bold" point-size="18">a</font> <font color="#666666">[]</font></td></tr>
<tr><td port="a" align="left">a <font color="#666666">[INTEGER]</font></td></tr>
<tr><td port="a2" align="left">a2 <font color="#666666">[TEXT]</font></td></tr>
</table>>];
"b" [shape=none, label=<<table border="0" cellborder="1" cellspacing="0" cellpadding="6">
<tr><td bgcolor="#EFEFEF"><font face="Arial Bold" point-size="18">b</font> <font color="#666666">[]</font></td></tr>
<tr><td port="b" align="left">b <font color="#666666">[INTEGER]</font></td></tr>
<tr><td port="b2" align="left">b2 <font color="#666666">[TEXT]</font></td></tr>
</table>>];

// Relations
"b":"b" -> "a":"a" [dir=back, arrowtail=crow, taillabel=<<table cellpadding="5" border="0" cellborder="0"><tr><td></td></tr></table>>];
}
2 changes: 1 addition & 1 deletion testdata/json_output_schema.golden
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"a"
],
"parent_cardinality": "Exactly one",
"def": "",
"def": "FOREIGN KEY (b) REFERENCES a(a)",
"virtual": false
}
],
Expand Down
2 changes: 1 addition & 1 deletion testdata/mermaid_template_test_schema.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
erDiagram

"b" }|--|| "a" : ""
"b" }|--|| "a" : "FOREIGN KEY (b) REFERENCES a(a)"
2 changes: 1 addition & 1 deletion testdata/mermaid_test_a.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
erDiagram

"b" }|--|| "a" : ""
"b" }|--|| "a" : "FOREIGN KEY (b) REFERENCES a(a)"

"a" {
INTEGER a
Expand Down
6 changes: 3 additions & 3 deletions testdata/mermaid_test_schema.golden
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
erDiagram

"b" }|--|| "a" : ""
"b" }|--|| "a" : "FOREIGN KEY (b) REFERENCES a(a)"

"a" {
INTEGER a
INTEGER a PK
TEXT a2
}
"b" {
INTEGER b
INTEGER b FK
TEXT b2
}
12 changes: 12 additions & 0 deletions testdata/mermaid_test_schema.hidedef.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
erDiagram

"b" }|--|| "a" : ""

"a" {
INTEGER a PK
TEXT a2
}
"b" {
INTEGER b FK
TEXT b2
}
2 changes: 1 addition & 1 deletion testdata/plantuml_template_test_a.puml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ table("b", "b") {
}

' relations
"b" }-- "a" : ""
"b" }-- "a" : "FOREIGN KEY (b) REFERENCES a(a)"

@enduml
2 changes: 1 addition & 1 deletion testdata/plantuml_template_test_schema.puml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ table("b", "b") {
}

' relations
"b" }-- "a" : ""
"b" }-- "a" : "FOREIGN KEY (b) REFERENCES a(a)"

@enduml
2 changes: 1 addition & 1 deletion testdata/plantuml_test_a.puml.golden
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ table("b", "b") {
}

' relations
"b" }|--|| "a" : ""
"b" }|--|| "a" : "FOREIGN KEY (b) REFERENCES a(a)"

@enduml
Loading