From 97dd4bd9c7421426118cb9af3f50512d7a96b7c6 Mon Sep 17 00:00:00 2001 From: Denis Shevchenko Date: Wed, 5 Aug 2026 10:20:45 +0300 Subject: [PATCH] fix(mermaid): render column definitions as comments --- target/mermaid/mermaid.go | 19 +++++++++++++++++- target/mermaid/mermaid_test.go | 6 ++---- target/mermaid/schema.tmpl | 2 +- target/mermaid/testdata/schema.mmd | 32 +++++++++++++++--------------- 4 files changed, 37 insertions(+), 22 deletions(-) diff --git a/target/mermaid/mermaid.go b/target/mermaid/mermaid.go index 97e2643..2b83029 100644 --- a/target/mermaid/mermaid.go +++ b/target/mermaid/mermaid.go @@ -6,6 +6,7 @@ import ( "context" "embed" "fmt" + "strings" "text/template" "github.com/holydocs/dberd" @@ -27,7 +28,9 @@ type Target struct { // NewTarget creates a new Mermaid JS diagram formatter instance. func NewTarget() (*Target, error) { - tmpl, err := template.ParseFS(templateFS, "schema.tmpl") + tmpl, err := template.New("schema.tmpl").Funcs(template.FuncMap{ + "mermaidComment": mermaidComment, + }).ParseFS(templateFS, "schema.tmpl") if err != nil { return nil, fmt.Errorf("parsing template: %w", err) } @@ -37,6 +40,20 @@ func NewTarget() (*Target, error) { }, nil } +func mermaidComment(definition, comment string) string { + parts := make([]string, 0, 2) + + for _, value := range []string{definition, comment} { + value = strings.Join(strings.Fields(value), " ") + value = strings.ReplaceAll(value, `"`, "'") + if value != "" { + parts = append(parts, value) + } + } + + return strings.Join(parts, "; ") +} + // Capabilities returns target capabilities. func (t *Target) Capabilities() dberd.TargetCapabilities { return dberd.TargetCapabilities{ diff --git a/target/mermaid/mermaid_test.go b/target/mermaid/mermaid_test.go index 69c39f3..19f46c6 100644 --- a/target/mermaid/mermaid_test.go +++ b/target/mermaid/mermaid_test.go @@ -10,10 +10,8 @@ import ( "github.com/stretchr/testify/require" ) -var ( - //go:embed testdata/schema.mmd - testSchema []byte -) +//go:embed testdata/schema.mmd +var testSchema []byte func TestFormatSchema(t *testing.T) { t.Parallel() diff --git a/target/mermaid/schema.tmpl b/target/mermaid/schema.tmpl index 5de70ef..0659163 100644 --- a/target/mermaid/schema.tmpl +++ b/target/mermaid/schema.tmpl @@ -3,7 +3,7 @@ erDiagram {{- range .Tables }} "{{ .Name }}" { {{- range .Columns }} - {{ .Definition }} {{ .Name }}{{ if .IsPrimary }} PK{{ end }} + column {{ .Name }}{{ if .IsPrimary }} PK{{ end }}{{ with mermaidComment .Definition .Comment }} "{{ . }}"{{ end }} {{- end }} } {{- end }} diff --git a/target/mermaid/testdata/schema.mmd b/target/mermaid/testdata/schema.mmd index 55a48e2..34908f9 100644 --- a/target/mermaid/testdata/schema.mmd +++ b/target/mermaid/testdata/schema.mmd @@ -1,27 +1,27 @@ erDiagram "public.users" { - INT8 NOT NULL id PK - VARCHAR(255) NOT NULL name - VARCHAR(255) NOT NULL email - TIMESTAMP DEFAULT current_timestamp() created_at + column id PK "INT8 NOT NULL" + column name "VARCHAR(255) NOT NULL" + column email "VARCHAR(255) NOT NULL; User email address" + column created_at "TIMESTAMP DEFAULT current_timestamp()" } "public.roles" { - INT8 NOT NULL id PK - VARCHAR(50) NOT NULL name - STRING description - TIMESTAMP DEFAULT current_timestamp() created_at + column id PK "INT8 NOT NULL" + column name "VARCHAR(50) NOT NULL" + column description "STRING; Role description and permissions" + column created_at "TIMESTAMP DEFAULT current_timestamp()" } "public.user_roles" { - INT8 NOT NULL user_id PK - INT8 NOT NULL role_id PK - TIMESTAMP DEFAULT current_timestamp() assigned_at + column user_id PK "INT8 NOT NULL" + column role_id PK "INT8 NOT NULL" + column assigned_at "TIMESTAMP DEFAULT current_timestamp()" } "public.posts" { - INT8 NOT NULL id PK - INT8 NOT NULL user_id - VARCHAR(255) NOT NULL title - STRING content - TIMESTAMP DEFAULT current_timestamp() created_at + column id PK "INT8 NOT NULL" + column user_id "INT8 NOT NULL" + column title "VARCHAR(255) NOT NULL" + column content "STRING" + column created_at "TIMESTAMP DEFAULT current_timestamp()" } "public.user_roles" }o--|| "public.roles" : "role_id -> id" "public.user_roles" }o--|| "public.users" : "user_id -> id"