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 #535 #536

Merged
merged 3 commits into from
Nov 3, 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
16 changes: 16 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,22 @@
return fmt.Errorf("viewpoint '%s' has unknown label '%s'", v.Name, l)
}
}
for vi, v := range s.Viewpoints {
// Add viewpoints to table

for _, t := range v.Tables {
println(v.Name, t)
table, err := s.FindTableByName(t)
if err != nil {
return err
}
table.Viewpoints = append(table.Viewpoints, &schema.TableViewpoint{
Index: vi,
Name: v.Name,
Desc: v.Desc,
})
}
}

return nil
}
Expand Down Expand Up @@ -537,7 +553,7 @@
}
}

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

Check failure on line 556 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 @@ -882,7 +898,7 @@
return nil
}

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

Check failure on line 901 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
60 changes: 51 additions & 9 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,22 +254,42 @@ func TestModifySchema(t *testing.T) {
t.Error(err)
}
tests := []struct {
name string
desc string
labels []string
comments []AdditionalComment
relations []AdditionalRelation
wantRel int
name string
desc string
labels []string
comments []AdditionalComment
relations []AdditionalRelation
viewpointATables []string
viewpointBTables []string
wantRel int
}{
{"", "", []string{}, nil, nil, 3},
{"mod_name_and_desc", "this is test schema", []string{}, nil, nil, 3},
{"", "", []string{}, nil, nil, []string{
"users",
"posts",
}, []string{
"users",
"user_options",
}, 3},
{"mod_name_and_desc", "this is test schema", []string{}, nil, nil, []string{
"users",
"posts",
}, []string{
"users",
"user_options",
}, 3},
{"relations", "", []string{}, nil, []AdditionalRelation{
{
Table: "users",
ParentTable: "categories",
Columns: []string{"id"},
ParentColumns: []string{"id"},
},
}, []string{
"users",
"posts",
}, []string{
"users",
"user_options",
}, 4},
{"not_override", "", []string{}, nil, []AdditionalRelation{
{
Expand All @@ -280,6 +300,12 @@ func TestModifySchema(t *testing.T) {
Def: "Additional Relation",
Override: false,
},
}, []string{
"users",
"posts",
}, []string{
"users",
"user_options",
}, 4},
{"override", "", []string{}, nil, []AdditionalRelation{
{
Expand All @@ -292,6 +318,12 @@ func TestModifySchema(t *testing.T) {
Def: "Override Relation",
Override: true,
},
}, []string{
"users",
"posts",
}, []string{
"users",
"user_options",
}, 3},
}
for _, tt := range tests {
Expand All @@ -301,11 +333,21 @@ func TestModifySchema(t *testing.T) {
c.Labels = tt.labels
c.Comments = tt.comments
c.Relations = tt.relations
c.Viewpoints = append(c.Viewpoints, Viewpoint{
Name: "A",
Desc: "Viewpoint A",
Tables: tt.viewpointATables,
})
c.Viewpoints = append(c.Viewpoints, Viewpoint{
Name: "B",
Desc: "Viewpoint B",
Tables: tt.viewpointBTables,
})

s := newTestSchemaViaJSON(t)
if err := c.ModifySchema(s); err != nil {
t.Error(err)
}

got, err := json.MarshalIndent(s, "", " ")
if err != nil {
t.Error(err)
Expand Down
5 changes: 0 additions & 5 deletions output/md/md.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,6 @@ func (m *Md) OutputViewpoint(wr io.Writer, i int, v *schema.Viewpoint) error {

// Output generate markdown files.
func Output(s *schema.Schema, c *config.Config, force bool) (e error) {
s, err := s.SetViewpointsToTables()
if err != nil {
return errors.WithStack(err)
}

docPath := c.DocPath

fullPath, err := filepath.Abs(docPath)
Expand Down
6 changes: 6 additions & 0 deletions sample/viewpoints/logs.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ CREATE TABLE `logs` (
| payload | text | | true | | | | |
| created | datetime | | false | | | | |

## Viewpoints

| Name | Definition |
| ---- | ---------- |
| [Ops](viewpoint-1.md) | Tables to be referenced during operation |

## Constraints

| Name | Type | Definition |
Expand Down
6 changes: 6 additions & 0 deletions sample/viewpoints/posts.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ CREATE TABLE `posts` (
| created | datetime | | false | | | | | |
| updated | datetime | | true | | | | | |

## Viewpoints

| Name | Definition |
| ---- | ---------- |
| [Ops](viewpoint-1.md) | Tables to be referenced during operation |

## Constraints

| Name | Type | Definition |
Expand Down
2 changes: 1 addition & 1 deletion sample/viewpoints/schema.json

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions sample/viewpoints/users.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ CREATE TABLE `users` (
| created | timestamp | | false | | | | | |
| updated | timestamp | | true | | | | | |

## Viewpoints

| Name | Definition |
| ---- | ---------- |
| [Ops](viewpoint-1.md) | Tables to be referenced during operation |
| [Around the users table](viewpoint-2.md) | Tables related to the users table |

## Constraints

| Name | Type | Definition |
Expand Down
18 changes: 0 additions & 18 deletions schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,24 +199,6 @@ type Schema struct {
Viewpoints Viewpoints `json:"viewpoints,omitempty"`
}

func (s *Schema) SetViewpointsToTables() (*Schema, error) {
for vi, v := range s.Viewpoints {
// Add viewpoints to table
for _, t := range v.Tables {
table, err := s.FindTableByName(t)
if err != nil {
return s, err
}
table.Viewpoints = append(table.Viewpoints, &TableViewpoint{
Index: vi,
Name: v.Name,
Desc: v.Desc,
})
}
}
return s, nil
}

func (s *Schema) NormalizeTableName(name string) string {
if s.Driver != nil && s.Driver.Meta != nil && s.Driver.Meta.CurrentSchema != "" && (s.Driver.Name == "postgres" || s.Driver.Name == "redshift") && !strings.Contains(name, ".") {
return fmt.Sprintf("%s.%s", s.Driver.Meta.CurrentSchema, name)
Expand Down
48 changes: 0 additions & 48 deletions schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,54 +11,6 @@ import (
"github.com/google/go-cmp/cmp"
)

func TestSetViewpointsToTables(t *testing.T) {
viewpointAName := "va"
viewpointBName := "vb"

tests := []struct {
viewpointATables []string
viewpointBTables []string
wantTableAViewpoints []*TableViewpoint
}{
{[]string{"a"}, []string{"b"}, []*TableViewpoint{{Name: viewpointAName}}},
{[]string{"a", "b"}, []string{"a"}, []*TableViewpoint{{
Index: 0,
Name: viewpointAName,
}, {
Index: 1,
Name: viewpointBName,
}}},
}

for _, tt := range tests {
t.Run(fmt.Sprintf("%v", tt.viewpointATables), func(t *testing.T) {
fmt.Println(tt.viewpointATables)
s := newTestSchema(t)
s.Viewpoints = []*Viewpoint{
{
Name: viewpointAName,
Tables: tt.viewpointATables,
},
{
Name: viewpointBName,
Tables: tt.viewpointBTables,
},
}
result, err := s.SetViewpointsToTables()
if err != nil {
t.Error(err)
}
gotTable, _ := result.FindTableByName("a")
got := gotTable.Viewpoints
want := tt.wantTableAViewpoints

if diff := cmp.Diff(got, want, nil); diff != "" {
t.Errorf("%s", diff)
}
})
}
}

func TestNormalizeTableName(t *testing.T) {
tests := []struct {
s *Schema
Expand Down
20 changes: 19 additions & 1 deletion testdata/modify_schema_.golden
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,23 @@
"name": "sqlite",
"database_version": "3.39.4",
"meta": {}
}
},
"viewpoints": [
{
"name": "A",
"desc": "Viewpoint A",
"tables": [
"users",
"posts"
]
},
{
"name": "B",
"desc": "Viewpoint B",
"tables": [
"users",
"user_options"
]
}
]
}
20 changes: 19 additions & 1 deletion testdata/modify_schema_mod_name_and_desc.golden
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,23 @@
"name": "sqlite",
"database_version": "3.39.4",
"meta": {}
}
},
"viewpoints": [
{
"name": "A",
"desc": "Viewpoint A",
"tables": [
"users",
"posts"
]
},
{
"name": "B",
"desc": "Viewpoint B",
"tables": [
"users",
"user_options"
]
}
]
}
20 changes: 19 additions & 1 deletion testdata/modify_schema_not_override.golden
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,23 @@
"name": "sqlite",
"database_version": "3.39.4",
"meta": {}
}
},
"viewpoints": [
{
"name": "A",
"desc": "Viewpoint A",
"tables": [
"users",
"posts"
]
},
{
"name": "B",
"desc": "Viewpoint B",
"tables": [
"users",
"user_options"
]
}
]
}
20 changes: 19 additions & 1 deletion testdata/modify_schema_override.golden
Original file line number Diff line number Diff line change
Expand Up @@ -409,5 +409,23 @@
"name": "sqlite",
"database_version": "3.39.4",
"meta": {}
}
},
"viewpoints": [
{
"name": "A",
"desc": "Viewpoint A",
"tables": [
"users",
"posts"
]
},
{
"name": "B",
"desc": "Viewpoint B",
"tables": [
"users",
"user_options"
]
}
]
}
20 changes: 19 additions & 1 deletion testdata/modify_schema_relations.golden
Original file line number Diff line number Diff line change
Expand Up @@ -423,5 +423,23 @@
"name": "sqlite",
"database_version": "3.39.4",
"meta": {}
}
},
"viewpoints": [
{
"name": "A",
"desc": "Viewpoint A",
"tables": [
"users",
"posts"
]
},
{
"name": "B",
"desc": "Viewpoint B",
"tables": [
"users",
"user_options"
]
}
]
}
Loading