From 5c636a2409e66c9c3d360a37865ab51116ebf57a Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sat, 2 Jun 2018 17:41:03 +0900 Subject: [PATCH] `--add` option support additional comments --- Makefile | 18 ++++---- cmd/diff.go | 2 +- cmd/doc.go | 2 +- cmd/dot.go | 2 +- sample/mysql/README.md | 4 +- sample/mysql/logs.md | 2 +- sample/mysql/post_comments.md | 14 +++--- sample/mysql/posts.md | 2 +- sample/mysql8/README.md | 4 +- sample/mysql8/logs.md | 2 +- sample/mysql8/post_comments.md | 14 +++--- sample/mysql8/posts.md | 2 +- sample/postgres/README.md | 4 +- sample/postgres/logs.md | 2 +- sample/postgres/post_comments.md | 14 +++--- sample/postgres/posts.md | 2 +- schema/schema.go | 47 +++++++++++++++++++-- test/{relations.yml => additional_data.yml} | 18 ++++++++ 18 files changed, 107 insertions(+), 48 deletions(-) rename test/{relations.yml => additional_data.yml} (52%) diff --git a/Makefile b/Makefile index 9cb20c062..a5802c5d8 100644 --- a/Makefile +++ b/Makefile @@ -26,17 +26,17 @@ template: go generate doc: build - ./tbls doc pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable -a test/relations.yml -f sample/postgres - ./tbls doc my://root:mypass@localhost:33306/testdb -a test/relations.yml -f sample/mysql - ./tbls doc my://root:mypass@localhost:33308/testdb -a test/relations.yml -f sample/mysql8 + ./tbls doc pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable -a test/additional_data.yml -f sample/postgres + ./tbls doc my://root:mypass@localhost:33306/testdb -a test/additional_data.yml -f sample/mysql + ./tbls doc my://root:mypass@localhost:33308/testdb -a test/additional_data.yml -f sample/mysql8 testdoc: build - $(eval DIFF := $(shell ./tbls diff pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable -a test/relations.yml sample/postgres)) - @test -z "$(DIFF)" || (echo "document does not match database." && ./tbls diff pg://postgres:pgpass@localhost:55432?sslmode=disable -a test/relations.yml sample/postgres && exit 1) - $(eval DIFF := $(shell ./tbls diff my://root:mypass@localhost:33306/testdb -a test/relations.yml sample/mysql)) - @test -z "$(DIFF)" || (echo "document does not match database." && ./tbls diff my://root:mypass@localhost:33306/testdb -a test/relations.yml sample/mysql && exit 1) - $(eval DIFF := $(shell ./tbls diff my://root:mypass@localhost:33308/testdb -a test/relations.yml sample/mysql8)) - @test -z "$(DIFF)" || (echo "document does not match database." && ./tbls diff my://root:mypass@localhost:33308/testdb -a test/relations.yml sample/mysql8 && exit 1) + $(eval DIFF := $(shell ./tbls diff pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable -a test/additional_data.yml sample/postgres)) + @test -z "$(DIFF)" || (echo "document does not match database." && ./tbls diff pg://postgres:pgpass@localhost:55432?sslmode=disable -a test/additional_data.yml sample/postgres && exit 1) + $(eval DIFF := $(shell ./tbls diff my://root:mypass@localhost:33306/testdb -a test/additional_data.yml sample/mysql)) + @test -z "$(DIFF)" || (echo "document does not match database." && ./tbls diff my://root:mypass@localhost:33306/testdb -a test/additional_data.yml sample/mysql && exit 1) + $(eval DIFF := $(shell ./tbls diff my://root:mypass@localhost:33308/testdb -a test/additional_data.yml sample/mysql8)) + @test -z "$(DIFF)" || (echo "document does not match database." && ./tbls diff my://root:mypass@localhost:33308/testdb -a test/additional_data.yml sample/mysql8 && exit 1) build: go build -ldflags="$(BUILD_LDFLAGS)" diff --git a/cmd/diff.go b/cmd/diff.go index 5866ef266..ad963c418 100644 --- a/cmd/diff.go +++ b/cmd/diff.go @@ -49,7 +49,7 @@ var diffCmd = &cobra.Command{ } if additionalDataPath != "" { - err = s.LoadAdditionalRelations(additionalDataPath) + err = s.LoadAdditionalData(additionalDataPath) if err != nil { fmt.Println(err) os.Exit(1) diff --git a/cmd/doc.go b/cmd/doc.go index 020c99c30..a7b81f058 100644 --- a/cmd/doc.go +++ b/cmd/doc.go @@ -56,7 +56,7 @@ var docCmd = &cobra.Command{ } if additionalDataPath != "" { - err = s.LoadAdditionalRelations(additionalDataPath) + err = s.LoadAdditionalData(additionalDataPath) if err != nil { fmt.Println(err) os.Exit(1) diff --git a/cmd/dot.go b/cmd/dot.go index 627807cbe..f51e2cbe4 100644 --- a/cmd/dot.go +++ b/cmd/dot.go @@ -51,7 +51,7 @@ var dotCmd = &cobra.Command{ } if additionalDataPath != "" { - err = s.LoadAdditionalRelations(additionalDataPath) + err = s.LoadAdditionalData(additionalDataPath) if err != nil { fmt.Println(err) os.Exit(1) diff --git a/sample/mysql/README.md b/sample/mysql/README.md index 51a2ff1f3..803187228 100644 --- a/sample/mysql/README.md +++ b/sample/mysql/README.md @@ -7,8 +7,8 @@ | [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE | | [comment_stars](comment_stars.md) | 6 | | BASE TABLE | | [comments](comments.md) | 6 | | BASE TABLE | -| [logs](logs.md) | 7 | | BASE TABLE | -| [post_comments](post_comments.md) | 7 | VIEW | VIEW | +| [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 | | [users](users.md) | 6 | Users table | BASE TABLE | diff --git a/sample/mysql/logs.md b/sample/mysql/logs.md index 8d6412960..c5dcdbf3f 100644 --- a/sample/mysql/logs.md +++ b/sample/mysql/logs.md @@ -2,7 +2,7 @@ ## Description - +audit log table ## Columns diff --git a/sample/mysql/post_comments.md b/sample/mysql/post_comments.md index 022c0f075..f38fc5b76 100644 --- a/sample/mysql/post_comments.md +++ b/sample/mysql/post_comments.md @@ -2,19 +2,19 @@ ## Description -VIEW +post and comments View table ## Columns | Name | Type | Default | Nullable | Children | Parents | Comment | | ---- | ---- | ------- | -------- | -------- | ------- | ------- | -| id | bigint(20) | 0 | true | | | | -| title | varchar(255) | | false | | | | -| post_user | varchar(50) | | true | | | | +| 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_user | varchar(50) | | true | | | | -| created | datetime | | true | | | | -| updated | datetime | | true | | | | +| comment_user | varchar(50) | | true | | | comments.users.username | +| created | datetime | | true | | | comments.created | +| updated | datetime | | true | | | comments.updated | ## Constraints diff --git a/sample/mysql/posts.md b/sample/mysql/posts.md index 337619c87..bbd2ab409 100644 --- a/sample/mysql/posts.md +++ b/sample/mysql/posts.md @@ -11,7 +11,7 @@ Posts table | id | bigint(20) | | false | [comments](comments.md) [logs](logs.md) | | | | user_id | int(11) | | false | | [users](users.md) | | | title | varchar(255) | | false | | | | -| body | text | | false | | | | +| body | text | | false | | | post body | | post_type | enum('public','private','draft') | | false | | | public/private/draft | | created | datetime | | false | | | | | updated | datetime | | true | | | | diff --git a/sample/mysql8/README.md b/sample/mysql8/README.md index 51a2ff1f3..803187228 100644 --- a/sample/mysql8/README.md +++ b/sample/mysql8/README.md @@ -7,8 +7,8 @@ | [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE | | [comment_stars](comment_stars.md) | 6 | | BASE TABLE | | [comments](comments.md) | 6 | | BASE TABLE | -| [logs](logs.md) | 7 | | BASE TABLE | -| [post_comments](post_comments.md) | 7 | VIEW | VIEW | +| [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 | | [users](users.md) | 6 | Users table | BASE TABLE | diff --git a/sample/mysql8/logs.md b/sample/mysql8/logs.md index 8d6412960..c5dcdbf3f 100644 --- a/sample/mysql8/logs.md +++ b/sample/mysql8/logs.md @@ -2,7 +2,7 @@ ## Description - +audit log table ## Columns diff --git a/sample/mysql8/post_comments.md b/sample/mysql8/post_comments.md index 022c0f075..f38fc5b76 100644 --- a/sample/mysql8/post_comments.md +++ b/sample/mysql8/post_comments.md @@ -2,19 +2,19 @@ ## Description -VIEW +post and comments View table ## Columns | Name | Type | Default | Nullable | Children | Parents | Comment | | ---- | ---- | ------- | -------- | -------- | ------- | ------- | -| id | bigint(20) | 0 | true | | | | -| title | varchar(255) | | false | | | | -| post_user | varchar(50) | | true | | | | +| 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_user | varchar(50) | | true | | | | -| created | datetime | | true | | | | -| updated | datetime | | true | | | | +| comment_user | varchar(50) | | true | | | comments.users.username | +| created | datetime | | true | | | comments.created | +| updated | datetime | | true | | | comments.updated | ## Constraints diff --git a/sample/mysql8/posts.md b/sample/mysql8/posts.md index 337619c87..bbd2ab409 100644 --- a/sample/mysql8/posts.md +++ b/sample/mysql8/posts.md @@ -11,7 +11,7 @@ Posts table | id | bigint(20) | | false | [comments](comments.md) [logs](logs.md) | | | | user_id | int(11) | | false | | [users](users.md) | | | title | varchar(255) | | false | | | | -| body | text | | false | | | | +| body | text | | false | | | post body | | post_type | enum('public','private','draft') | | false | | | public/private/draft | | created | datetime | | false | | | | | updated | datetime | | true | | | | diff --git a/sample/postgres/README.md b/sample/postgres/README.md index eabc2155e..f32397e5f 100644 --- a/sample/postgres/README.md +++ b/sample/postgres/README.md @@ -8,8 +8,8 @@ | [posts](posts.md) | 8 | Posts table | BASE TABLE | | [comments](comments.md) | 6 | | BASE TABLE | | [comment_stars](comment_stars.md) | 6 | | BASE TABLE | -| [logs](logs.md) | 7 | | BASE TABLE | -| [post_comments](post_comments.md) | 7 | | VIEW | +| [logs](logs.md) | 7 | audit log table | BASE TABLE | +| [post_comments](post_comments.md) | 7 | post and comments View table | VIEW | | [CamelizeTable](CamelizeTable.md) | 2 | | BASE TABLE | ## Relations diff --git a/sample/postgres/logs.md b/sample/postgres/logs.md index 282817221..ee437b86c 100644 --- a/sample/postgres/logs.md +++ b/sample/postgres/logs.md @@ -2,7 +2,7 @@ ## Description - +audit log table ## Columns diff --git a/sample/postgres/post_comments.md b/sample/postgres/post_comments.md index 851f109fa..b82fdb50b 100644 --- a/sample/postgres/post_comments.md +++ b/sample/postgres/post_comments.md @@ -2,19 +2,19 @@ ## Description - +post and comments View table ## Columns | Name | Type | Default | Nullable | Children | Parents | Comment | | ---- | ---- | ------- | -------- | -------- | ------- | ------- | -| id | bigint | | true | | | | -| title | varchar(255) | | true | | | | -| post_user | varchar(50) | | true | | | | +| id | bigint | | true | | | comments.id | +| title | varchar(255) | | true | | | posts.title | +| post_user | varchar(50) | | true | | | posts.users.username | | comment | text | | true | | | | -| comment_user | varchar(50) | | true | | | | -| created | timestamp without time zone | | true | | | | -| updated | timestamp without time zone | | true | | | | +| comment_user | varchar(50) | | true | | | comments.users.username | +| created | timestamp without time zone | | true | | | comments.created | +| updated | timestamp without time zone | | true | | | comments.updated | ## Constraints diff --git a/sample/postgres/posts.md b/sample/postgres/posts.md index 8b776fcc6..f853560f1 100755 --- a/sample/postgres/posts.md +++ b/sample/postgres/posts.md @@ -11,7 +11,7 @@ Posts table | id | bigint | nextval('posts_id_seq'::regclass) | false | [comments](comments.md) [logs](logs.md) | | | | user_id | integer | | false | | [users](users.md) | | | title | varchar(255) | | false | | | | -| body | text | | false | | | | +| body | text | | false | | | post body | | post_type | post_types | | false | | | public/private/draft | | labels | array | | true | | | | | created | timestamp without time zone | | false | | | | diff --git a/schema/schema.go b/schema/schema.go index 6911da1a6..9c356ecdd 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -63,6 +63,7 @@ type Schema struct { // AdditionalData is the struct for table relations from yaml type AdditionalData struct { Relations []AdditionalRelation `yaml:"relations"` + Comments []AdditionalComment `yaml:"comments"` } // AdditionalRelation is the struct for table relation from yaml @@ -74,6 +75,13 @@ type AdditionalRelation struct { Def string `yaml:"def"` } +// AdditionalComment is the struct for table relation from yaml +type AdditionalComment struct { + Table string `yaml:"table"` + TableComment string `yaml:"tableComment"` + ColumnComments map[string]string `yaml:"columnComments"` +} + // FindTableByName find table by table name func (s *Schema) FindTableByName(name string) (*Table, error) { for _, t := range s.Tables { @@ -124,8 +132,8 @@ func (s *Schema) Sort() error { return nil } -// LoadAdditionalRelations load additional relations from yaml file -func (s *Schema) LoadAdditionalRelations(path string) error { +// LoadAdditionalData load additional data (relations, comments) from yaml file +func (s *Schema) LoadAdditionalData(path string) error { fullPath, err := filepath.Abs(path) if err != nil { return err @@ -142,7 +150,20 @@ func (s *Schema) LoadAdditionalRelations(path string) error { return err } - for _, r := range data.Relations { + err = loadAdditionalRelations(s, data.Relations) + if err != nil { + return err + } + err = loadAdditionalComments(s, data.Comments) + if err != nil { + return err + } + + return nil +} + +func loadAdditionalRelations(s *Schema, relations []AdditionalRelation) error { + for _, r := range relations { relation := &Relation{ IsAdditional: true, } @@ -151,6 +172,7 @@ func (s *Schema) LoadAdditionalRelations(path string) error { } else { relation.Def = "Additional Relation" } + var err error relation.Table, err = s.FindTableByName(r.Table) if err != nil { return err @@ -178,6 +200,25 @@ func (s *Schema) LoadAdditionalRelations(path string) error { s.Relations = append(s.Relations, relation) } + return nil +} +func loadAdditionalComments(s *Schema, comments []AdditionalComment) error { + for _, c := range comments { + table, err := s.FindTableByName(c.Table) + if err != nil { + return err + } + if c.TableComment != "" { + table.Comment = c.TableComment + } + for c, comment := range c.ColumnComments { + column, err := table.FindColumnByName(c) + if err != nil { + return err + } + column.Comment = comment + } + } return nil } diff --git a/test/relations.yml b/test/additional_data.yml similarity index 52% rename from test/relations.yml rename to test/additional_data.yml index a05fe664d..56450d5b6 100644 --- a/test/relations.yml +++ b/test/additional_data.yml @@ -29,3 +29,21 @@ relations: parentTable: comment_stars parentColumns: - id +comments: + - + table: posts + columnComments: + body: post body + - + table: logs + tableComment: audit log table + - + table: post_comments + tableComment: post and comments View table + columnComments: + id: comments.id + title: posts.title + post_user: posts.users.username + comment_user: comments.users.username + created: comments.created + updated: comments.updated