Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: refactor the catalog index struct (#2016)
rebelice143cdd3c483c
diff --git a/plugin/advisor/catalog/catalog.go b/plugin/advisor/catalog/catalog.goindex f50f1843be..3361d3430c 100644--- a/plugin/advisor/catalog/catalog.go+++ b/plugin/advisor/catalog/catalog.go@@ -2,7 +2,6 @@ package catalogimport ("context"- "sort")// Catalog is the service for catalog.@@ -59,8 +58,7 @@ type Table struct {type Index struct {Name string// This could refer to a column or an expression.- Expression string- Position int+ ExpressionList []string// Type isn't supported for SQLite.Type stringUnique bool@@ -125,9 +123,7 @@ type IndexFind struct {}// FindIndex finds the index.-func (d *Database) FindIndex(find *IndexFind) (string, []*Index) {- tableName := ""- var indexList []*Index+func (d *Database) FindIndex(find *IndexFind) (string, *Index) {// notMatchTable is used for PostgreSQL. In PostgreSQL, the index name is unique in a schema, not a table.notMatchTable := (d.DbType == Postgres && find.SchemaName != "" && find.TableName == "")for _, schema := range d.SchemaList {@@ -140,14 +136,10 @@ func (d *Database) FindIndex(find *IndexFind) (string, []*Index) {}for _, index := range table.IndexList {if index.Name == find.IndexName {- tableName = table.Name- indexList = append(indexList, index)+ return table.Name, index}}}}- sort.Slice(indexList, func(i, j int) bool {- return indexList[i].Position < indexList[j].Position- })- return tableName, indexList+ return "", nil}diff --git a/plugin/advisor/catalog/utils.go b/plugin/advisor/catalog/utils.godeleted file mode 100644index cdd757f46e..0000000000--- a/plugin/advisor/catalog/utils.go+++ /dev/null@@ -1,19 +0,0 @@-package catalog--import (- "strings"-)--// JoinColumnListForIndex joins the column name with sep.-func JoinColumnListForIndex(indexList []*Index, sep string) string {- return strings.Join(GetColumnListForIndex(indexList), sep)-}--// GetColumnListForIndex returns the column name list.-func GetColumnListForIndex(indexList []*Index) []string {- var columnList []string- for _, column := range indexList {- columnList = append(columnList, column.Expression)- }- return columnList-}diff --git a/plugin/advisor/mysql/advisor_naming_index_convention.go b/plugin/advisor/mysql/advisor_naming_index_convention.goindex 8b61b22967..d2f7281a71 100644--- a/plugin/advisor/mysql/advisor_naming_index_convention.go+++ b/plugin/advisor/mysql/advisor_naming_index_convention.go@@ -148,19 +148,19 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*indfor _, spec := range node.Specs {switch spec.Tp {case ast.AlterTableRenameIndex:- _, indexList := checker.database.FindIndex(&catalog.IndexFind{+ _, index := checker.database.FindIndex(&catalog.IndexFind{TableName: node.Table.Name.String(),IndexName: spec.FromKey.String(),})- if len(indexList) == 0 {+ if index == nil {continue}- if indexList[0].Unique {+ if index.Unique {// Unique index naming convention should in advisor_naming_unique_key_convention.gocontinue}metaData := map[string]string{- advisor.ColumnListTemplateToken: catalog.JoinColumnListForIndex(indexList, "_"),+ advisor.ColumnListTemplateToken: strings.Join(index.ExpressionList, "_"),advisor.TableNameTemplateToken: node.Table.Name.String(),}res = append(res, &indexMetaData{diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention.goindex 115c011af1..14691f410b 100644--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go@@ -141,19 +141,19 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexMfor _, spec := range node.Specs {switch spec.Tp {case ast.AlterTableRenameIndex:- _, indexList := checker.database.FindIndex(&catalog.IndexFind{+ _, index := checker.database.FindIndex(&catalog.IndexFind{TableName: node.Table.Name.String(),IndexName: spec.FromKey.String(),})- if len(indexList) == 0 {+ if index == nil {continue}- if !indexList[0].Unique {+ if !index.Unique {// Index naming convention should in advisor_naming_index_convention.gocontinue}metaData := map[string]string{- advisor.ColumnListTemplateToken: catalog.JoinColumnListForIndex(indexList, "_"),+ advisor.ColumnListTemplateToken: strings.Join(index.ExpressionList, "_"),advisor.TableNameTemplateToken: node.Table.Name.String(),}res = append(res, &indexMetaData{diff --git a/plugin/advisor/mysql/advisor_table_require_pk.go b/plugin/advisor/mysql/advisor_table_require_pk.goindex 93c27c1b06..1d877ab2d0 100644--- a/plugin/advisor/mysql/advisor_table_require_pk.go+++ b/plugin/advisor/mysql/advisor_table_require_pk.go@@ -158,14 +158,14 @@ func (v *tableRequirePKChecker) createTable(node *ast.CreateTableStmt) {func (v *tableRequirePKChecker) dropColumn(table string, column string) {if _, ok := v.tables[table]; !ok {v.tables[table] = make(columnSet)- _, pkList := v.database.FindIndex(&catalog.IndexFind{+ _, pk := v.database.FindIndex(&catalog.IndexFind{TableName: table,IndexName: primaryKeyName,})- if len(pkList) == 0 {+ if pk == nil {return}- v.tables[table] = newColumnSet(catalog.GetColumnListForIndex(pkList))+ v.tables[table] = newColumnSet(pk.ExpressionList)}delete(v.tables[table], column)diff --git a/plugin/advisor/pg/advisor_column_no_null.go b/plugin/advisor/pg/advisor_column_no_null.goindex 5c7d18d75e..ae49e4cff5 100644--- a/plugin/advisor/pg/advisor_column_no_null.go+++ b/plugin/advisor/pg/advisor_column_no_null.go@@ -145,16 +145,16 @@ func (checker *columnNoNullChecker) removeColumnByConstraintList(table *ast.Tablchecker.removeColumn(table, column)}case ast.ConstraintTypePrimaryUsingIndex:- _, indexList := checker.database.FindIndex(&catalog.IndexFind{+ _, index := checker.database.FindIndex(&catalog.IndexFind{SchemaName: normalizeSchemaName(table.Schema),TableName: table.Name,IndexName: constraint.IndexName,})- if len(indexList) == 0 {+ if index == nil {continue}- for _, index := range indexList {- checker.removeColumn(table, index.Expression)+ for _, expression := range index.ExpressionList {+ checker.removeColumn(table, expression)}}}diff --git a/plugin/advisor/pg/advisor_naming_unique_key_convention.go b/plugin/advisor/pg/advisor_naming_unique_key_convention.goindex 4753a3340c..c50714672e 100644--- a/plugin/advisor/pg/advisor_naming_unique_key_convention.go+++ b/plugin/advisor/pg/advisor_naming_unique_key_convention.go@@ -157,10 +157,10 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM})}case *ast.RenameConstraintStmt:- tableName, indexList := checker.findIndex(node.Table.Schema, node.Table.Name, node.ConstraintName)- if len(indexList) != 0 && indexList[0].Unique && !indexList[0].Primary {+ tableName, index := checker.findIndex(node.Table.Schema, node.Table.Name, node.ConstraintName)+ if index != nil && index.Unique && !index.Primary {metaData := map[string]string{- advisor.ColumnListTemplateToken: catalog.JoinColumnListForIndex(indexList, "_"),+ advisor.ColumnListTemplateToken: strings.Join(index.ExpressionList, "_"),advisor.TableNameTemplateToken: tableName,}res = append(res, &indexMetaData{@@ -171,10 +171,10 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM}case *ast.RenameIndexStmt:// "ALTER INDEX name RENAME TO new_name" doesn't take a table name- tableName, indexList := checker.findIndex(node.Table.Schema, "", node.IndexName)- if len(indexList) != 0 && indexList[0].Unique && !indexList[0].Primary {+ tableName, index := checker.findIndex(node.Table.Schema, "", node.IndexName)+ if index != nil && index.Unique && !index.Primary {metaData := map[string]string{- advisor.ColumnListTemplateToken: catalog.JoinColumnListForIndex(indexList, "_"),+ advisor.ColumnListTemplateToken: strings.Join(index.ExpressionList, "_"),advisor.TableNameTemplateToken: tableName,}res = append(res, &indexMetaData{@@ -201,10 +201,10 @@ func (checker *namingUKConventionChecker) getUniqueKeyMetadata(schemaName stringmetaData: metaData,}case ast.ConstraintTypeUniqueUsingIndex:- tableName, indexList := checker.findIndex(schemaName, tableName, constraint.IndexName)- if len(indexList) != 0 {+ tableName, index := checker.findIndex(schemaName, tableName, constraint.IndexName)+ if index != nil {metaData := map[string]string{- advisor.ColumnListTemplateToken: catalog.JoinColumnListForIndex(indexList, "_"),+ advisor.ColumnListTemplateToken: strings.Join(index.ExpressionList, "_"),advisor.TableNameTemplateToken: tableName,}return &indexMetaData{@@ -218,7 +218,7 @@ func (checker *namingUKConventionChecker) getUniqueKeyMetadata(schemaName string}// findIndex returns index found in catalogs, nil if not found.-func (checker *namingUKConventionChecker) findIndex(schemaName string, tableName string, indexName string) (string, []*catalog.Index) {+func (checker *namingUKConventionChecker) findIndex(schemaName string, tableName string, indexName string) (string, *catalog.Index) {return checker.database.FindIndex(&catalog.IndexFind{SchemaName: normalizeSchemaName(schemaName),TableName: tableName,diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.goindex 8884a459ef..273ac31eae 100644--- a/plugin/advisor/utils_for_tests.go+++ b/plugin/advisor/utils_for_tests.go@@ -41,34 +41,19 @@ var (},IndexList: []*catalog.Index{{- Name: MockOldMySQLPKName,- Expression: "id",- Unique: true,- Primary: true,+ Name: MockOldMySQLPKName,+ ExpressionList: []string{"id", "name"},+ Unique: true,+ Primary: true,},{- Name: MockOldMySQLPKName,- Expression: "name",- Unique: true,- Primary: true,+ Name: MockOldUKName,+ ExpressionList: []string{"id", "name"},+ Unique: true,},{- Name: MockOldUKName,- Expression: "id",- Unique: true,- },- {- Name: MockOldUKName,- Expression: "name",- Unique: true,- },- {- Name: MockOldIndexName,- Expression: "id",- },- {- Name: MockOldIndexName,- Expression: "name",+ Name: MockOldIndexName,+ ExpressionList: []string{"id", "name"},},},},@@ -92,34 +77,19 @@ var (},IndexList: []*catalog.Index{{- Name: MockOldPostgreSQLPKName,- Expression: "id",- Unique: true,- Primary: true,- },- {- Name: MockOldPostgreSQLPKName,- Expression: "name",- Unique: true,- Primary: true,- },- {- Name: MockOldUKName,- Expression: "id",- Unique: true,- },- {- Name: MockOldUKName,- Expression: "name",- Unique: true,+ Name: MockOldPostgreSQLPKName,+ ExpressionList: []string{"id", "name"},+ Unique: true,+ Primary: true,},{- Name: MockOldIndexName,- Expression: "id",+ Name: MockOldUKName,+ ExpressionList: []string{"id", "name"},+ Unique: true,},{- Name: MockOldIndexName,- Expression: "name",+ Name: MockOldIndexName,+ ExpressionList: []string{"id", "name"},},},},diff --git a/store/catalog.go b/store/catalog.goindex 215ff2ca2d..b05eb4e4d0 100644--- a/store/catalog.go+++ b/store/catalog.go@@ -3,6 +3,7 @@ package storeimport ("context""fmt"+ "sort""strings""github.com/bytebase/bytebase/api"@@ -172,23 +173,47 @@ func convertTable(table *api.Table) *catalog.Table {CreateOptions: table.CreateOptions,Comment: table.Comment,ColumnList: convertColumnList(table.ColumnList),- IndexList: converIndexList(table.IndexList),+ IndexList: convertIndexList(table.IndexList),}}-func converIndexList(list []*api.Index) []*catalog.Index {+func convertIndexList(list []*api.Index) []*catalog.Index {+ if len(list) == 0 {+ return nil+ }var res []*catalog.Index- for _, index := range list {- res = append(res, &catalog.Index{- Name: index.Name,- Expression: index.Expression,- Position: index.Position,- Type: index.Type,- Unique: index.Unique,- Primary: index.Primary,- Visible: index.Visible,- Comment: index.Comment,- })+ indexMap := make(map[string][]*api.Index)+ for _, expression := range list {+ indexMap[expression.Name] = append(indexMap[expression.Name], expression)+ }++ for _, expressionList := range indexMap {+ res = append(res, convertIndexExceptExpression(expressionList))+ }+ // Order it cause the random iteration order in Go, see https://go.dev/blog/maps+ sort.Slice(res, func(i, j int) bool {+ return res[i].Name < res[j].Name+ })+ return res+}++func convertIndexExceptExpression(list []*api.Index) *catalog.Index {+ if len(list) == 0 {+ return nil+ }+ res := &catalog.Index{+ Name: list[0].Name,+ Type: list[0].Type,+ Unique: list[0].Unique,+ Primary: list[0].Primary,+ Visible: list[0].Visible,+ Comment: list[0].Comment,+ }+ sort.Slice(list, func(i, j int) bool {+ return list[i].Position < list[j].Position+ })+ for _, expression := range list {+ res.ExpressionList = append(res.ExpressionList, expression.Expression)}return res}diff --git a/store/catalog_test.go b/store/catalog_test.gonew file mode 100644index 0000000000..094bbe58f0--- /dev/null+++ b/store/catalog_test.go@@ -0,0 +1,57 @@+package store++import (+ "testing"++ "github.com/bytebase/bytebase/api"+ "github.com/bytebase/bytebase/plugin/advisor/catalog"+ "github.com/stretchr/testify/assert"+)++func TestConvertIndexList(t *testing.T) {+ type testData struct {+ origin []*api.Index+ want []*catalog.Index+ }++ tests := []testData{+ {+ origin: []*api.Index{+ {+ Name: "idx_1",+ Position: 1,+ Expression: "id",+ },+ {+ Name: "idx_1",+ Position: 2,+ Expression: "name",+ },+ {+ Name: "idx_1",+ Position: 3,+ Expression: "user",+ },+ {+ Name: "idx_2",+ Position: 1,+ Expression: "name",+ },+ },+ want: []*catalog.Index{+ {+ Name: "idx_1",+ ExpressionList: []string{"id", "name", "user"},+ },+ {+ Name: "idx_2",+ ExpressionList: []string{"name"},+ },+ },+ },+ }++ for _, test := range tests {+ assert.Equal(t, test.want, convertIndexList(test.origin))+ }+}