Skip to content
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
19 changes: 18 additions & 1 deletion target/mermaid/mermaid.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"embed"
"fmt"
"strings"
"text/template"

"github.com/holydocs/dberd"
Expand All @@ -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)
}
Expand All @@ -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{
Expand Down
6 changes: 2 additions & 4 deletions target/mermaid/mermaid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion target/mermaid/schema.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
32 changes: 16 additions & 16 deletions target/mermaid/testdata/schema.mmd
Original file line number Diff line number Diff line change
@@ -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"
Expand Down
Loading