From 4235f6dd1ebceae67ddcd1a9433c7246ec24b268 Mon Sep 17 00:00:00 2001 From: macoto1995 Date: Thu, 26 Oct 2023 09:20:37 +0900 Subject: [PATCH] feat:func SetViewpointsToTables --- schema/schema.go | 47 +++++++++++++++++++++++++++++--------- schema/schema_test.go | 52 +++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 86 insertions(+), 13 deletions(-) diff --git a/schema/schema.go b/schema/schema.go index 6097e76d..7f9a352b 100644 --- a/schema/schema.go +++ b/schema/schema.go @@ -131,19 +131,26 @@ type Column struct { HideForER bool `json:"-"` } +type TableViewpoint struct { + Index int `json:"index"` + Name string `json:"name"` + Desc string `json:"desc"` +} + // Table is the struct for database table type Table struct { - Name string `json:"name"` - Type string `json:"type"` - Comment string `json:"comment"` - Columns []*Column `json:"columns"` - Indexes []*Index `json:"indexes"` - Constraints []*Constraint `json:"constraints"` - Triggers []*Trigger `json:"triggers"` - Def string `json:"def"` - Labels Labels `json:"labels,omitempty"` - ReferencedTables []*Table `json:"referenced_tables,omitempty" yaml:"referencedTables,omitempty"` - External bool `json:"-"` // Table external to the schema + Name string `json:"name"` + Type string `json:"type"` + Comment string `json:"comment"` + Columns []*Column `json:"columns"` + Viewpoints []*TableViewpoint `json:"viewpoints"` + Indexes []*Index `json:"indexes"` + Constraints []*Constraint `json:"constraints"` + Triggers []*Trigger `json:"triggers"` + Def string `json:"def"` + Labels Labels `json:"labels,omitempty"` + ReferencedTables []*Table `json:"referenced_tables,omitempty" yaml:"referencedTables,omitempty"` + External bool `json:"-"` // Table external to the schema } // Relation is the struct for table relation @@ -192,6 +199,24 @@ 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 6678e6df..bc023f36 100644 --- a/schema/schema_test.go +++ b/schema/schema_test.go @@ -11,6 +11,54 @@ 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 @@ -212,11 +260,11 @@ func TestSchema_Sort(t *testing.T) { }, Functions: []*Function{ &Function{ - Name: "b", + Name: "b", Arguments: "arg b", }, &Function{ - Name: "b", + Name: "b", Arguments: "arg a", }, },