diff --git a/output/md/md.go b/output/md/md.go index a204cecb..7ce41e3e 100644 --- a/output/md/md.go +++ b/output/md/md.go @@ -10,6 +10,7 @@ import ( "github.com/k1LoW/tbls/schema" "github.com/sergi/go-diff/diffmatchpatch" + "strings" ) // Output generate markdown files. @@ -31,7 +32,7 @@ func Output(s *schema.Schema, path string, force bool) error { } f, _ := Assets.Open(filepath.Join("/", "index.md.tmpl")) bs, _ := ioutil.ReadAll(f) - tmpl, err := template.New("index").Parse(string(bs)) + tmpl, err := template.New("index").Funcs(funcMap()).Parse(string(bs)) if err != nil { return err } @@ -57,7 +58,7 @@ func Output(s *schema.Schema, path string, force bool) error { } f, _ := Assets.Open(filepath.Join("/", "table.md.tmpl")) bs, _ := ioutil.ReadAll(f) - tmpl, err := template.New(t.Name).Parse(string(bs)) + tmpl, err := template.New(t.Name).Funcs(funcMap()).Parse(string(bs)) if err != nil { file.Close() return err @@ -97,7 +98,7 @@ func Diff(s *schema.Schema, path string) error { a := new(bytes.Buffer) f, _ := Assets.Open(filepath.Join("/", "index.md.tmpl")) bs, _ := ioutil.ReadAll(f) - tmpl, err := template.New("index").Parse(string(bs)) + tmpl, err := template.New("index").Funcs(funcMap()).Parse(string(bs)) if err != nil { return err } @@ -132,7 +133,7 @@ func Diff(s *schema.Schema, path string) error { a := new(bytes.Buffer) f, _ := Assets.Open(filepath.Join("/", "table.md.tmpl")) bs, _ := ioutil.ReadAll(f) - tmpl, err := template.New(t.Name).Parse(string(bs)) + tmpl, err := template.New(t.Name).Funcs(funcMap()).Parse(string(bs)) if err != nil { return err } @@ -177,3 +178,14 @@ func outputExists(s *schema.Schema, path string) bool { } return false } + +func funcMap() map[string]interface{} { + return template.FuncMap{ + "nl2br": func(text string) string { + return strings.Replace(text, "\n", "
", -1) + }, + "nl2mdnl": func(text string) string { + return strings.Replace(text, "\n", " \n", -1) + }, + } +} diff --git a/output/md/templates.go b/output/md/templates.go index c1d8bd8a..142e014d 100644 --- a/output/md/templates.go +++ b/output/md/templates.go @@ -6,24 +6,24 @@ import ( "github.com/jessevdk/go-assets" ) -var _Assets43889384df1c6f74d764c29d91b9d5637eb46061 = "# {{ .Schema.Name }}\n\n## Tables\n\n| Name | Columns | Comment | Type |\n| ---- | ------- | ------- | ---- |\n{{- range $i, $t := .Schema.Tables }}\n| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment }} | {{ $t.Type }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er](schema.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)" -var _Assetsac44302fb6150a621aa9d04a0350aac972bf7e18 = "# {{ .Table.Name }}\n\n## Description\n\n{{ .Table.Comment -}}\n{{ if .Table.Def }}\n
\nTable Definition\n\n```sql\n{{ .Table.Def }}\n```\n\n
\n{{ end }}\n\n## Columns\n\n| Name | Type | Default | Nullable | Children | Parents | Comment |\n| ---- | ---- | ------- | -------- | -------- | ------- | ------- |\n{{- range $i, $c := .Table.Columns }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment }} |\n{{- end }}\n\n## Constraints\n\n| Name | Type | Definition |\n| ---- | ---- | --- |\n{{- range $i, $c := .Table.Constraints }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Def }} |\n{{- end }}\n\n## Indexes\n\n| Name | Definition |\n| ---- | --- |\n{{- range $i, $idx := .Table.Indexes }}\n| {{ $idx.Name }} | {{ $idx.Def }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er]({{ .Table.Name }}.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)" +var _Assets43889384df1c6f74d764c29d91b9d5637eb46061 = "# {{ .Schema.Name }}\n\n## Tables\n\n| Name | Columns | Comment | Type |\n| ---- | ------- | ------- | ---- |\n{{- range $i, $t := .Schema.Tables }}\n| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment | nl2br }} | {{ $t.Type }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er](schema.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)" +var _Assetsac44302fb6150a621aa9d04a0350aac972bf7e18 = "# {{ .Table.Name }}\n\n## Description\n\n{{ .Table.Comment | nl2mdnl -}}\n{{ if .Table.Def }}\n
\nTable Definition\n\n```sql\n{{ .Table.Def }}\n```\n\n
\n{{ end }}\n\n## Columns\n\n| Name | Type | Default | Nullable | Children | Parents | Comment |\n| ---- | ---- | ------- | -------- | -------- | ------- | ------- |\n{{- range $i, $c := .Table.Columns }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment | nl2br }} |\n{{- end }}\n\n## Constraints\n\n| Name | Type | Definition |\n| ---- | ---- | --- |\n{{- range $i, $c := .Table.Constraints }}\n| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Def }} |\n{{- end }}\n\n## Indexes\n\n| Name | Definition |\n| ---- | --- |\n{{- range $i, $idx := .Table.Indexes }}\n| {{ $idx.Name }} | {{ $idx.Def }} |\n{{- end }}\n\n{{ if .er -}}\n## Relations\n\n![er]({{ .Table.Name }}.png)\n{{- end }}\n\n---\n\n> Generated by [tbls](https://github.com/k1LoW/tbls)" // Assets returns go-assets FileSystem var Assets = assets.NewFileSystem(map[string][]string{"/": []string{"index.md.tmpl", "table.md.tmpl"}}, map[string]*assets.File{ "/": &assets.File{ Path: "/", FileMode: 0x800001ed, - Mtime: time.Unix(1528211857, 1528211857000000000), + Mtime: time.Unix(1531144283, 1531144283000000000), Data: nil, }, "/index.md.tmpl": &assets.File{ Path: "/index.md.tmpl", FileMode: 0x1a4, - Mtime: time.Unix(1527684406, 1527684406000000000), + Mtime: time.Unix(1531143680, 1531143680000000000), Data: []byte(_Assets43889384df1c6f74d764c29d91b9d5637eb46061), }, "/table.md.tmpl": &assets.File{ Path: "/table.md.tmpl", FileMode: 0x1a4, - Mtime: time.Unix(1528211857, 1528211857000000000), + Mtime: time.Unix(1531144283, 1531144283000000000), Data: []byte(_Assetsac44302fb6150a621aa9d04a0350aac972bf7e18), }}, "") diff --git a/output/md/templates/index.md.tmpl b/output/md/templates/index.md.tmpl index b20c9cf7..6a94ef3a 100644 --- a/output/md/templates/index.md.tmpl +++ b/output/md/templates/index.md.tmpl @@ -5,7 +5,7 @@ | Name | Columns | Comment | Type | | ---- | ------- | ------- | ---- | {{- range $i, $t := .Schema.Tables }} -| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment }} | {{ $t.Type }} | +| [{{ $t.Name }}]({{ $t.Name }}.md) | {{ len $t.Columns }} | {{ $t.Comment | nl2br }} | {{ $t.Type }} | {{- end }} {{ if .er -}} diff --git a/output/md/templates/table.md.tmpl b/output/md/templates/table.md.tmpl index d4b29902..2f60cfad 100644 --- a/output/md/templates/table.md.tmpl +++ b/output/md/templates/table.md.tmpl @@ -2,7 +2,7 @@ ## Description -{{ .Table.Comment -}} +{{ .Table.Comment | nl2mdnl -}} {{ if .Table.Def }}
Table Definition @@ -19,7 +19,7 @@ | Name | Type | Default | Nullable | Children | Parents | Comment | | ---- | ---- | ------- | -------- | -------- | ------- | ------- | {{- range $i, $c := .Table.Columns }} -| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment }} | +| {{ $c.Name }} | {{ $c.Type }} | {{ $c.Default.String }} | {{ $c.Nullable }} | {{ range $ii, $r := $c.ChildRelations -}}[{{ $r.Table.Name }}]({{ $r.Table.Name }}.md) {{ end }} | {{ range $ii, $r := $c.ParentRelations -}}[{{ $r.ParentTable.Name }}]({{ $r.ParentTable.Name }}.md) {{ end }} | {{ $c.Comment | nl2br }} | {{- end }} ## Constraints diff --git a/sample/mysql/README.md b/sample/mysql/README.md index 80318722..82b9c187 100644 --- a/sample/mysql/README.md +++ b/sample/mysql/README.md @@ -6,7 +6,7 @@ | ---- | ------- | ------- | ---- | | [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE | | [comment_stars](comment_stars.md) | 6 | | BASE TABLE | -| [comments](comments.md) | 6 | | BASE TABLE | +| [comments](comments.md) | 6 | Comments
Multi-line table comment | BASE TABLE | | [logs](logs.md) | 7 | audit log table | BASE TABLE | | [post_comments](post_comments.md) | 7 | post and comments View table | VIEW | | [posts](posts.md) | 7 | Posts table | BASE TABLE | diff --git a/sample/mysql/comments.md b/sample/mysql/comments.md index cc0b35a3..2030bc95 100644 --- a/sample/mysql/comments.md +++ b/sample/mysql/comments.md @@ -2,7 +2,8 @@ ## Description - +Comments +Multi-line table comment
Table Definition @@ -11,7 +12,7 @@ CREATE TABLE `comments` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `post_id` bigint(20) NOT NULL, `user_id` int(11) NOT NULL, - `comment` text NOT NULL, + `comment` text NOT NULL COMMENT 'Comment\nMulti-line column comment', `created` datetime NOT NULL, `updated` datetime DEFAULT NULL, PRIMARY KEY (`id`), @@ -20,7 +21,7 @@ CREATE TABLE `comments` ( KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`) USING HASH, CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`), CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=latin1 +) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Comments\nMulti-line table comment' ```
@@ -33,7 +34,7 @@ CREATE TABLE `comments` ( | id | bigint(20) | | false | [logs](logs.md) | | | | post_id | bigint(20) | | false | [comment_stars](comment_stars.md) | [posts](posts.md) | | | user_id | int(11) | | false | [comment_stars](comment_stars.md) | [users](users.md) | | -| comment | text | | false | | | | +| comment | text | | false | | | Comment
Multi-line column comment | | created | datetime | | false | | | | | updated | datetime | | true | | | | diff --git a/sample/mysql/post_comments.md b/sample/mysql/post_comments.md index 376b8de5..8c657e12 100644 --- a/sample/mysql/post_comments.md +++ b/sample/mysql/post_comments.md @@ -20,7 +20,7 @@ CREATE VIEW post_comments AS ((select `c`.`id` AS `id`,`p`.`title` AS `title`,`u | id | bigint(20) | 0 | true | | | comments.id | | title | varchar(255) | | false | | | posts.title | | post_user | varchar(50) | | true | | | posts.users.username | -| comment | text | | true | | | | +| comment | text | | true | | | Comment
Multi-line column comment | | comment_user | varchar(50) | | true | | | comments.users.username | | created | datetime | | true | | | comments.created | | updated | datetime | | true | | | comments.updated | diff --git a/sample/mysql8/README.md b/sample/mysql8/README.md index 80318722..82b9c187 100644 --- a/sample/mysql8/README.md +++ b/sample/mysql8/README.md @@ -6,7 +6,7 @@ | ---- | ------- | ------- | ---- | | [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE | | [comment_stars](comment_stars.md) | 6 | | BASE TABLE | -| [comments](comments.md) | 6 | | BASE TABLE | +| [comments](comments.md) | 6 | Comments
Multi-line table comment | BASE TABLE | | [logs](logs.md) | 7 | audit log table | BASE TABLE | | [post_comments](post_comments.md) | 7 | post and comments View table | VIEW | | [posts](posts.md) | 7 | Posts table | BASE TABLE | diff --git a/sample/mysql8/comments.md b/sample/mysql8/comments.md index 36dd2b53..317f1ec1 100644 --- a/sample/mysql8/comments.md +++ b/sample/mysql8/comments.md @@ -2,7 +2,8 @@ ## Description - +Comments +Multi-line table comment
Table Definition @@ -11,7 +12,7 @@ CREATE TABLE `comments` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `post_id` bigint(20) NOT NULL, `user_id` int(11) NOT NULL, - `comment` text NOT NULL, + `comment` text NOT NULL COMMENT 'Comment\nMulti-line column comment', `created` datetime NOT NULL, `updated` datetime DEFAULT NULL, PRIMARY KEY (`id`), @@ -20,7 +21,7 @@ CREATE TABLE `comments` ( KEY `comments_post_id_user_id_idx` (`post_id`,`user_id`), CONSTRAINT `comments_post_id_fk` FOREIGN KEY (`post_id`) REFERENCES `posts` (`id`), CONSTRAINT `comments_user_id_fk` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci COMMENT='Comments\nMulti-line table comment' ```
@@ -33,7 +34,7 @@ CREATE TABLE `comments` ( | id | bigint(20) | | false | [logs](logs.md) | | | | post_id | bigint(20) | | false | [comment_stars](comment_stars.md) | [posts](posts.md) | | | user_id | int(11) | | false | [comment_stars](comment_stars.md) | [users](users.md) | | -| comment | text | | false | | | | +| comment | text | | false | | | Comment
Multi-line column comment | | created | datetime | | false | | | | | updated | datetime | | true | | | | diff --git a/sample/mysql8/post_comments.md b/sample/mysql8/post_comments.md index 808dae96..39989554 100644 --- a/sample/mysql8/post_comments.md +++ b/sample/mysql8/post_comments.md @@ -20,7 +20,7 @@ CREATE VIEW post_comments AS (select `c`.`id` AS `id`,`p`.`title` AS `title`,`u2 | id | bigint(20) | 0 | true | | | comments.id | | title | varchar(255) | | false | | | posts.title | | post_user | varchar(50) | | true | | | posts.users.username | -| comment | text | | true | | | | +| comment | text | | true | | | Comment
Multi-line column comment | | comment_user | varchar(50) | | true | | | comments.users.username | | created | datetime | | true | | | comments.created | | updated | datetime | | true | | | comments.updated | diff --git a/sample/postgres/README.md b/sample/postgres/README.md index 680b3715..0baac25a 100644 --- a/sample/postgres/README.md +++ b/sample/postgres/README.md @@ -6,7 +6,7 @@ | ---- | ------- | ------- | ---- | | [users](users.md) | 6 | Users table | BASE TABLE | | [posts](posts.md) | 8 | Posts table | BASE TABLE | -| [comments](comments.md) | 6 | | BASE TABLE | +| [comments](comments.md) | 6 | Comments
Multi-line table comment | BASE TABLE | | [comment_stars](comment_stars.md) | 6 | | BASE TABLE | | [logs](logs.md) | 7 | audit log table | BASE TABLE | | [post_comments](post_comments.md) | 7 | post and comments View table | VIEW | diff --git a/sample/postgres/comments.md b/sample/postgres/comments.md index 2cfab85f..800b377a 100644 --- a/sample/postgres/comments.md +++ b/sample/postgres/comments.md @@ -2,7 +2,8 @@ ## Description - +Comments +Multi-line table comment ## Columns @@ -11,7 +12,7 @@ | id | bigint | nextval('comments_id_seq'::regclass) | false | [logs](logs.md) | | | | post_id | bigint | | false | [comment_stars](comment_stars.md) | [posts](posts.md) | | | user_id | integer | | false | [comment_stars](comment_stars.md) | [users](users.md) | | -| comment | text | | false | | | | +| comment | text | | false | | | Comment
Multi-line column comment | | created | timestamp without time zone | | false | | | | | updated | timestamp without time zone | | true | | | | diff --git a/test/my.sql b/test/my.sql index 7c7cc6be..7b052579 100644 --- a/test/my.sql +++ b/test/my.sql @@ -33,14 +33,14 @@ CREATE TABLE comments ( id bigint AUTO_INCREMENT, post_id bigint NOT NULL, user_id int NOT NULL, - comment text NOT NULL, + comment text NOT NULL COMMENT 'Comment\nMulti-line column comment', created datetime NOT NULL, updated datetime, CONSTRAINT comments_id_pk PRIMARY KEY(id), CONSTRAINT comments_post_id_fk FOREIGN KEY(post_id) REFERENCES posts(id) MATCH SIMPLE, CONSTRAINT comments_user_id_fk FOREIGN KEY(user_id) REFERENCES users(id) MATCH SIMPLE, UNIQUE(post_id, user_id) -); +) COMMENT = 'Comments\nMulti-line table comment'; CREATE INDEX comments_post_id_user_id_idx ON comments(post_id, user_id) USING HASH; CREATE TABLE comment_stars ( diff --git a/test/pg.sql b/test/pg.sql index cf020f4a..4f3683a1 100644 --- a/test/pg.sql +++ b/test/pg.sql @@ -24,8 +24,8 @@ CREATE TABLE users ( created timestamp NOT NULL, updated timestamp ); -COMMENT ON TABLE users is 'Users table'; -COMMENT ON COLUMN users.email is 'ex. user@example.com'; +COMMENT ON TABLE users IS 'Users table'; +COMMENT ON COLUMN users.email IS 'ex. user@example.com'; CREATE TABLE posts ( id bigserial NOT NULL, @@ -40,8 +40,8 @@ CREATE TABLE posts ( CONSTRAINT posts_user_id_fk FOREIGN KEY(user_id) REFERENCES users(id) MATCH SIMPLE ON UPDATE NO ACTION ON DELETE CASCADE, UNIQUE(user_id, title) ); -COMMENT ON TABLE posts is 'Posts table'; -COMMENT ON COLUMN posts.post_type is 'public/private/draft'; +COMMENT ON TABLE posts IS 'Posts table'; +COMMENT ON COLUMN posts.post_type IS 'public/private/draft'; CREATE INDEX posts_user_id_idx ON posts USING btree(user_id); @@ -57,6 +57,9 @@ CREATE TABLE comments ( CONSTRAINT comments_user_id_fk FOREIGN KEY(user_id) REFERENCES users(id) MATCH SIMPLE, UNIQUE(post_id, user_id) ); +COMMENT ON TABLE comments IS E'Comments\nMulti-line table comment'; +COMMENT ON COLUMN comments.comment IS E'Comment\nMulti-line column comment'; + CREATE INDEX comments_post_id_user_id_idx ON comments USING btree(post_id, user_id); CREATE TABLE comment_stars (