From 802fa6c3456d35e6f38f0762bf7bb4c86484a983 Mon Sep 17 00:00:00 2001 From: Makoto Date: Fri, 3 Nov 2023 12:27:08 +0900 Subject: [PATCH] refactor moved logis to modify schema --- config/config.go | 17 +++++- config/config_test.go | 60 ++++++++++++++++--- schema/schema.go | 18 ------ schema/schema_test.go | 48 --------------- testdata/modify_schema_.golden | 20 ++++++- .../modify_schema_mod_name_and_desc.golden | 20 ++++++- testdata/modify_schema_not_override.golden | 20 ++++++- testdata/modify_schema_override.golden | 20 ++++++- testdata/modify_schema_relations.golden | 20 ++++++- 9 files changed, 160 insertions(+), 83 deletions(-) diff --git a/config/config.go b/config/config.go index 420a7001a..2f140169f 100644 --- a/config/config.go +++ b/config/config.go @@ -506,10 +506,21 @@ func (c *Config) ModifySchema(s *schema.Schema) error { return fmt.Errorf("viewpoint '%s' has unknown label '%s'", v.Name, l) } } + for vi, v := range s.Viewpoints { + // Add viewpoints to table - _, err := s.SetViewpointsToTables() - if err != nil { - return errors.WithStack(err) + 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 diff --git a/config/config_test.go b/config/config_test.go index cd5328fcc..e9efb2d29 100644 --- a/config/config_test.go +++ b/config/config_test.go @@ -254,15 +254,29 @@ 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", @@ -270,6 +284,12 @@ func TestModifySchema(t *testing.T) { Columns: []string{"id"}, ParentColumns: []string{"id"}, }, + }, []string{ + "users", + "posts", + }, []string{ + "users", + "user_options", }, 4}, {"not_override", "", []string{}, nil, []AdditionalRelation{ { @@ -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{ { @@ -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 { @@ -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) diff --git a/schema/schema.go b/schema/schema.go index 7f9a352b7..e1af37448 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -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) diff --git a/schema/schema_test.go b/schema/schema_test.go index bc023f368..cd8f1fdc8 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -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 diff --git a/testdata/modify_schema_.golden b/testdata/modify_schema_.golden index 95d404902..5f8115fe7 100644 --- a/testdata/modify_schema_.golden +++ b/testdata/modify_schema_.golden @@ -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" + ] + } + ] } \ No newline at end of file diff --git a/testdata/modify_schema_mod_name_and_desc.golden b/testdata/modify_schema_mod_name_and_desc.golden index 4ae3d2e81..593bb4190 100644 --- a/testdata/modify_schema_mod_name_and_desc.golden +++ b/testdata/modify_schema_mod_name_and_desc.golden @@ -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" + ] + } + ] } \ No newline at end of file diff --git a/testdata/modify_schema_not_override.golden b/testdata/modify_schema_not_override.golden index 998d55726..969af534a 100644 --- a/testdata/modify_schema_not_override.golden +++ b/testdata/modify_schema_not_override.golden @@ -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" + ] + } + ] } \ No newline at end of file diff --git a/testdata/modify_schema_override.golden b/testdata/modify_schema_override.golden index c624569eb..81be5cea5 100644 --- a/testdata/modify_schema_override.golden +++ b/testdata/modify_schema_override.golden @@ -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" + ] + } + ] } \ No newline at end of file diff --git a/testdata/modify_schema_relations.golden b/testdata/modify_schema_relations.golden index f8a90516f..ff0577120 100644 --- a/testdata/modify_schema_relations.golden +++ b/testdata/modify_schema_relations.golden @@ -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" + ] + } + ] } \ No newline at end of file