Skip to content

Commit

Permalink
Merge pull request #538 from k1LoW/fix-cardinality
Browse files Browse the repository at this point in the history
Fix handling cardinality
  • Loading branch information
k1LoW authored Nov 7, 2023
2 parents 4bcd621 + 94a6a55 commit f6f5b62
Show file tree
Hide file tree
Showing 24 changed files with 37 additions and 28 deletions.
19 changes: 13 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,6 @@ func (c *Config) ModifySchema(s *schema.Schema) error {
for _, l := range c.Labels {
s.Labels = s.Labels.Merge(l)
}
if err := detectCardinality(s); err != nil {
return err
}
if err := detectPKFK(s); err != nil {
return err
}
Expand Down Expand Up @@ -657,15 +654,24 @@ func (c *Config) detectShowColumnsForER(s *schema.Schema) error {

func mergeAdditionalRelations(s *schema.Schema, relations []AdditionalRelation) error {
for _, r := range relations {
c, err := schema.ToCardinality(r.Cardinality)
if err != nil {
return errors.Wrap(err, "failed to add relation")
}
pc, err := schema.ToCardinality(r.ParentCardinality)
if err != nil {
return errors.Wrap(err, "failed to add relation")
}
relation := &schema.Relation{
Virtual: true,
Cardinality: c,
ParentCardinality: pc,
Virtual: true,
}
if r.Def != "" {
relation.Def = r.Def
} else {
relation.Def = "Additional Relation"
}
var err error
relation.Table, err = s.FindTableByName(r.Table)
if err != nil {
return errors.Wrap(err, "failed to add relation")
Expand Down Expand Up @@ -822,8 +828,9 @@ func matchLength(s []string, e string) (int, bool) {
return 0, false
}

// detectCardinality detects the cardinality of the relations
// This function should be applied to the completed schema
func detectCardinality(s *schema.Schema) error {
// This function should be applied to the completed schema
for _, r := range s.Relations {
// child
if r.Cardinality == schema.UnknownCardinality {
Expand Down
2 changes: 1 addition & 1 deletion sample/adjust/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/detect_relations/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/detect_relations_singular/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/dict/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/exclude/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/font/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/hide/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/hide_not_related_column/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/mariadb/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/mermaid/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ erDiagram
"logs" }o--|| "users" : "logs->users"
"logs" }o--o| "posts" : "Additional Relation"
"logs" }o--o| "comments" : "Additional Relation"
"logs" }o--o| "comment_stars" : "Additional Relation"
"logs" ||--|| "comment_stars" : "Additional Relation"
"CamelizeTable" {
bigint id PK
Expand Down
2 changes: 1 addition & 1 deletion sample/mermaid/comment_stars.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ CREATE TABLE `comment_stars` (
```mermaid
erDiagram
"logs" }o--o| "comment_stars" : "Additional Relation"
"logs" ||--|| "comment_stars" : "Additional Relation"
"comment_stars" }o--|| "comments" : "FOREIGN KEY (comment_post_id, comment_user_id) REFERENCES comments (post_id, user_id)"
"comment_stars" }o--|| "users" : "FOREIGN KEY (comment_user_id) REFERENCES users (id)"
Expand Down
2 changes: 1 addition & 1 deletion sample/mermaid/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ erDiagram
"logs" }o--|| "users" : "logs->users"
"logs" }o--o| "posts" : "Additional Relation"
"logs" }o--o| "comments" : "Additional Relation"
"logs" }o--o| "comment_stars" : "Additional Relation"
"logs" ||--|| "comment_stars" : "Additional Relation"
"logs" {
bigint id PK
Expand Down
2 changes: 1 addition & 1 deletion sample/mermaid/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/mssql/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/mysql/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/mysql56/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/number/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/png/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/postgres/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/postgres95/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/sqlite/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion sample/viewpoints/schema.json

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions testdata/test_tbls.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ relations:
parentTable: comment_stars
parentColumns:
- id
cardinality: exactly one
parentCardinality: exactly one
comments:
-
table: posts
Expand Down

0 comments on commit f6f5b62

Please sign in to comment.