Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix handling cardinality #538

Merged
merged 3 commits into from
Nov 7, 2023
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: 13 additions & 6 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -407,9 +407,6 @@
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 @@ -553,7 +550,7 @@
}
}

func excludeTableFromSchema(name string, s *schema.Schema) error {

Check failure on line 553 in config/config.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

func `excludeTableFromSchema` is unused (unused)
// Tables
tables := []*schema.Table{}
for _, t := range s.Tables {
Expand Down Expand Up @@ -657,15 +654,24 @@

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 @@
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 Expand Up @@ -898,7 +905,7 @@
return nil
}

func matchLabels(il []string, l schema.Labels) bool {

Check failure on line 908 in config/config.go

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

func `matchLabels` is unused (unused)
for _, ll := range l {
for _, ill := range il {
if wildcard.MatchSimple(ill, ll.Name) {
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
Loading