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 option support additional comments #21

Merged
merged 1 commit into from
Jun 2, 2018
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
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
Expand Down
2 changes: 1 addition & 1 deletion cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion cmd/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sample/mysql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
2 changes: 1 addition & 1 deletion sample/mysql/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description


audit log table

## Columns

Expand Down
14 changes: 7 additions & 7 deletions sample/mysql/post_comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sample/mysql/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | | |
Expand Down
4 changes: 2 additions & 2 deletions sample/mysql8/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |

Expand Down
2 changes: 1 addition & 1 deletion sample/mysql8/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description


audit log table

## Columns

Expand Down
14 changes: 7 additions & 7 deletions sample/mysql8/post_comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sample/mysql8/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | | |
Expand Down
4 changes: 2 additions & 2 deletions sample/postgres/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion sample/postgres/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Description


audit log table

## Columns

Expand Down
14 changes: 7 additions & 7 deletions sample/postgres/post_comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion sample/postgres/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | | | |
Expand Down
47 changes: 44 additions & 3 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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,
}
Expand All @@ -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
Expand Down Expand Up @@ -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
}
18 changes: 18 additions & 0 deletions test/relations.yml → test/additional_data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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