Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add state struct for catalog finding (#2539)
rebelice4559bcf8cbb7
diff --git a/plugin/advisor/advisor.go b/plugin/advisor/advisor.goindex fa33a5a9c0..8c675dcf21 100644--- a/plugin/advisor/advisor.go+++ b/plugin/advisor/advisor.go@@ -242,8 +242,8 @@ type Context struct {Collation string// SQL review rule special fields.- Rule *SQLReviewRule- Database *catalog.Database+ Rule *SQLReviewRule+ Catalog *catalog.Finder}// Advisor is the interface for advisor.diff --git a/plugin/advisor/catalog/catalog.go b/plugin/advisor/catalog/catalog.goindex d0c2a05b5d..3c344dddd2 100644--- a/plugin/advisor/catalog/catalog.go+++ b/plugin/advisor/catalog/catalog.go@@ -104,119 +104,3 @@ type Extension struct {Version stringDescription string}--// HasNoTable returns true if the current database has no table.-func (d *Database) HasNoTable() bool {- for _, schema := range d.SchemaList {- for _, table := range schema.TableList {- if table != nil {- return false- }- }- }-- return true-}--// IndexFind is for find index.-type IndexFind struct {- SchemaName string- TableName string- IndexName string-}--// FindIndex finds the 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 == db.Postgres && find.SchemaName != "" && find.TableName == "")- for _, schema := range d.SchemaList {- if schema.Name != find.SchemaName {- continue- }- for _, table := range schema.TableList {- if !notMatchTable && table.Name != find.TableName {- continue- }- for _, index := range table.IndexList {- if index.Name == find.IndexName {- return table.Name, index- }- }- }- }- return "", nil-}--// PrimaryKeyFind is for find primary key.-type PrimaryKeyFind struct {- SchemaName string- TableName string-}--// FindPrimaryKey finds the primary key.-func (d *Database) FindPrimaryKey(find *PrimaryKeyFind) *Index {- for _, schema := range d.SchemaList {- if schema.Name != find.SchemaName {- continue- }- for _, table := range schema.TableList {- if table.Name != find.TableName {- continue- }- for _, index := range table.IndexList {- if index.Primary {- return index- }- }- }- }- return nil-}--// ColumnFind is for find column.-type ColumnFind struct {- SchemaName string- TableName string- ColumnName string-}--// FindColumn finds the column.-func (d *Database) FindColumn(find *ColumnFind) *Column {- for _, schema := range d.SchemaList {- if schema.Name != find.SchemaName {- continue- }- for _, table := range schema.TableList {- if table.Name != find.TableName {- continue- }- for _, column := range table.ColumnList {- if column.Name == find.ColumnName {- return column- }- }- }- }- return nil-}--// TableFind is for find table.-type TableFind struct {- SchemaName string- TableName string-}--// FindTable finds the table.-func (d *Database) FindTable(find *TableFind) *Table {- for _, schema := range d.SchemaList {- if schema.Name != find.SchemaName {- continue- }- for _, table := range schema.TableList {- if table.Name == find.TableName {- return table- }- }- }- return nil-}diff --git a/plugin/advisor/catalog/finder.go b/plugin/advisor/catalog/finder.gonew file mode 100644index 0000000000..57baa56a57--- /dev/null+++ b/plugin/advisor/catalog/finder.go@@ -0,0 +1,140 @@+package catalog++import "github.com/bytebase/bytebase/plugin/advisor/db"++// Finder is the service for finding schema information in database.+type Finder struct {+ database *databaseState+}++// NewFinder creates a new finder.+func NewFinder(database *Database) *Finder {+ return &Finder{database: newDatabaseState(database)}+}++// HasNoTable returns true if the current database has no table.+func (f *Finder) HasNoTable() bool {+ for _, schema := range f.database.schemaSet {+ if len(schema.tableSet) != 0 {+ return false+ }+ }++ return true+}++// DatabaseName returns the database name.+func (f *Finder) DatabaseName() string {+ return f.database.name+}++// IndexFind is for find index.+type IndexFind struct {+ SchemaName string+ TableName string+ IndexName string+}++// FindIndex finds the index.+func (f *Finder) FindIndex(find *IndexFind) (string, *Index) {+ // needMatchTable is used for PostgreSQL. In PostgreSQL, the index name is unique in a schema, not a table.+ needMatchTable := (f.database.dbType != db.Postgres || find.SchemaName == "" || find.TableName != "")+ if needMatchTable {+ schema, exists := f.database.schemaSet[find.SchemaName]+ if !exists {+ return "", nil+ }+ table, exists := schema.tableSet[find.TableName]+ if !exists {+ return "", nil+ }+ index, exists := table.indexSet[find.IndexName]+ if !exists {+ return "", nil+ }+ return table.name, index.convertToCatalog()+ }+ for _, schema := range f.database.schemaSet {+ if schema.name != find.SchemaName {+ continue+ }+ for _, table := range schema.tableSet {+ // no need to match table name.+ index, exists := table.indexSet[find.IndexName]+ if !exists {+ return "", nil+ }+ return table.name, index.convertToCatalog()+ }+ }+ return "", nil+}++// PrimaryKeyFind is for find primary key.+type PrimaryKeyFind struct {+ SchemaName string+ TableName string+}++// FindPrimaryKey finds the primary key.+func (f *Finder) FindPrimaryKey(find *PrimaryKeyFind) *Index {+ for _, schema := range f.database.schemaSet {+ if schema.name != find.SchemaName {+ continue+ }+ for _, table := range schema.tableSet {+ if table.name != find.TableName {+ continue+ }+ for _, index := range table.indexSet {+ if index.primary {+ return index.convertToCatalog()+ }+ }+ }+ }+ return nil+}++// ColumnFind is for find column.+type ColumnFind struct {+ SchemaName string+ TableName string+ ColumnName string+}++// FindColumn finds the column.+func (f *Finder) FindColumn(find *ColumnFind) *Column {+ schema, exists := f.database.schemaSet[find.SchemaName]+ if !exists {+ return nil+ }+ table, exists := schema.tableSet[find.TableName]+ if !exists {+ return nil+ }+ column, exists := table.columnSet[find.ColumnName]+ if !exists {+ return nil+ }+ return column.convertToCatalog()+}++// TableFind is for find table.+type TableFind struct {+ SchemaName string+ TableName string+}++// FindTable finds the table.+func (f *Finder) FindTable(find *TableFind) *Table {+ schema, exists := f.database.schemaSet[find.SchemaName]+ if !exists {+ return nil+ }+ table, exists := schema.tableSet[find.TableName]+ if !exists {+ return nil+ }+ return table.convertToCatalog()+}diff --git a/plugin/advisor/catalog/state.go b/plugin/advisor/catalog/state.gonew file mode 100644index 0000000000..3de123a37e--- /dev/null+++ b/plugin/advisor/catalog/state.go@@ -0,0 +1,259 @@+package catalog++// This file defines the database state during walk-through. The only two cases will use database state:+// 1. during walk-through+// 2. the underlying implementation of Finder++import (+ "github.com/bytebase/bytebase/plugin/advisor/db"+)++func newDatabaseState(d *Database) *databaseState {+ database := &databaseState{+ name: d.Name,+ characterSet: d.CharacterSet,+ collation: d.Collation,+ dbType: d.DbType,+ schemaSet: make(schemaStateMap),+ }++ for _, schema := range d.SchemaList {+ database.schemaSet[schema.Name] = newSchemaState(schema)+ }++ return database+}++func newSchemaState(s *Schema) *schemaState {+ schema := &schemaState{+ name: s.Name,+ tableSet: make(tableStateMap),+ viewSet: make(viewStateMap),+ extensionSet: make(extensionStateMap),+ }++ for _, table := range s.TableList {+ schema.tableSet[table.Name] = newTableState(table)+ }++ for _, view := range s.ViewList {+ schema.viewSet[view.Name] = newViewState(view)+ }++ for _, extension := range s.ExtensionList {+ schema.extensionSet[extension.Name] = newExtensionState(extension)+ }++ return schema+}++func newViewState(v *View) *viewState {+ return &viewState{+ name: v.Name,+ definition: v.Definition,+ comment: v.Comment,+ }+}++func newExtensionState(e *Extension) *extensionState {+ return &extensionState{+ name: e.Name,+ version: e.Version,+ description: e.Description,+ }+}++func newTableState(t *Table) *tableState {+ table := &tableState{+ name: t.Name,+ tableType: t.Type,+ engine: t.Engine,+ collation: t.Collation,+ rowCount: t.RowCount,+ dataSize: t.DataSize,+ indexSize: t.IndexSize,+ dataFree: t.DataFree,+ createOptions: t.CreateOptions,+ comment: t.Comment,+ columnSet: make(columnStateMap),+ indexSet: make(indexStateMap),+ }++ for _, column := range t.ColumnList {+ table.columnSet[column.Name] = newColumnState(column)+ }++ for _, index := range t.IndexList {+ table.indexSet[index.Name] = newIndexState(index)+ }++ return table+}++func newColumnState(c *Column) *columnState {+ return &columnState{+ name: c.Name,+ position: c.Position,+ defaultValue: c.Default,+ nullable: c.Nullable,+ columnType: c.Type,+ characterSet: c.CharacterSet,+ collation: c.Collation,+ comment: c.Comment,+ }+}++func newIndexState(i *Index) *indexState {+ index := &indexState{+ name: i.Name,+ indextype: i.Type,+ unique: i.Unique,+ primary: i.Primary,+ visible: i.Visible,+ comment: i.Comment,+ }+ index.expressionList = append(index.expressionList, i.ExpressionList...)+ return index+}++type databaseState struct {+ name string+ characterSet string+ collation string+ dbType db.Type+ schemaSet schemaStateMap+}+type schemaState struct {+ name string+ tableSet tableStateMap+ viewSet viewStateMap+ extensionSet extensionStateMap+}+type schemaStateMap map[string]*schemaState++type tableState struct {+ name string+ tableType string+ // engine isn't supported for Postgres, Snowflake, SQLite.+ engine string+ // collation isn't supported for Postgres, ClickHouse, Snowflake, SQLite.+ collation string+ rowCount int64+ // dataSize isn't supported for SQLite.+ dataSize int64+ // indexSize isn't supported for ClickHouse, Snowflake, SQLite.+ indexSize int64+ // dataFree isn't supported for Postgres, ClickHouse, Snowflake, SQLite.+ dataFree int64+ // createOptions isn't supported for Postgres, ClickHouse, Snowflake, SQLite.+ createOptions string+ // comment isn't supported for SQLite.+ comment string+ columnSet columnStateMap+ // indexSet isn't supported for ClickHouse, Snowflake.+ indexSet indexStateMap+}+type tableStateMap map[string]*tableState++func (table *tableState) convertToCatalog() *Table {+ return &Table{+ Name: table.name,+ Type: table.tableType,+ Engine: table.engine,+ Collation: table.collation,+ RowCount: table.rowCount,+ DataSize: table.dataSize,+ IndexSize: table.indexSize,+ DataFree: table.dataFree,+ CreateOptions: table.createOptions,+ Comment: table.comment,+ ColumnList: table.columnSet.convertToCatalog(),+ IndexList: table.indexSet.convertToCatalog(),+ }+}++type indexState struct {+ name string+ // This could refer to a column or an expression.+ expressionList []string+ // Type isn't supported for SQLite.+ indextype string+ unique bool+ primary bool+ // Visible isn't supported for Postgres, SQLite.+ visible bool+ // Comment isn't supported for SQLite.+ comment string+}+type indexStateMap map[string]*indexState++func (m indexStateMap) convertToCatalog() []*Index {+ var res []*Index+ for _, index := range m {+ res = append(res, index.convertToCatalog())+ }+ return res+}++func (index *indexState) convertToCatalog() *Index {+ return &Index{+ Name: index.name,+ ExpressionList: index.expressionList,+ Type: index.indextype,+ Unique: index.unique,+ Primary: index.primary,+ Visible: index.visible,+ Comment: index.comment,+ }+}++type columnState struct {+ name string+ position int+ defaultValue *string+ // nullable isn't supported for ClickHouse.+ nullable bool+ columnType string+ // characterSet isn't supported for Postgres, ClickHouse, SQLite.+ characterSet string+ // collation isn't supported for ClickHouse, SQLite.+ collation string+ // comment isn't supported for SQLite.+ comment string+}+type columnStateMap map[string]*columnState++func (m columnStateMap) convertToCatalog() []*Column {+ var res []*Column+ for _, column := range m {+ res = append(res, column.convertToCatalog())+ }+ return res+}++func (column *columnState) convertToCatalog() *Column {+ return &Column{+ Name: column.name,+ Position: column.position,+ Default: column.defaultValue,+ Nullable: column.nullable,+ Type: column.columnType,+ CharacterSet: column.characterSet,+ Collation: column.collation,+ Comment: column.comment,+ }+}++type viewState struct {+ name string+ definition string+ comment string+}+type viewStateMap map[string]*viewState++type extensionState struct {+ name string+ version string+ description string+}+type extensionStateMap map[string]*extensionStatediff --git a/plugin/advisor/mysql/advisor_column_disallow_changing_type.go b/plugin/advisor/mysql/advisor_column_disallow_changing_type.goindex 7b86a8dc3d..996a3f130a 100644--- a/plugin/advisor/mysql/advisor_column_disallow_changing_type.go+++ b/plugin/advisor/mysql/advisor_column_disallow_changing_type.go@@ -41,9 +41,9 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(ctx advisor.Context, statement sreturn nil, err}checker := &columnDisallowChangingTypeChecker{- level: level,- title: string(ctx.Rule.Type),- database: ctx.Database,+ level: level,+ title: string(ctx.Rule.Type),+ catalog: ctx.Catalog,}for _, stmt := range stmtList {@@ -69,7 +69,7 @@ type columnDisallowChangingTypeChecker struct {title stringtext stringline int- database *catalog.Database+ catalog *catalog.Finder}// Enter implements the ast.Visitor interface.@@ -123,7 +123,7 @@ func getTypeString(tp *types.FieldType) string {}func (checker *columnDisallowChangingTypeChecker) changeColumnType(tableName string, columName string, newType string) bool {- column := checker.database.FindColumn(&catalog.ColumnFind{+ column := checker.catalog.FindColumn(&catalog.ColumnFind{TableName: tableName,ColumnName: columName,})diff --git a/plugin/advisor/mysql/advisor_column_exists.go b/plugin/advisor/mysql/advisor_column_exists.goindex 203e0debc5..3926a06208 100644--- a/plugin/advisor/mysql/advisor_column_exists.go+++ b/plugin/advisor/mysql/advisor_column_exists.go@@ -38,9 +38,9 @@ func (*ColumnExistsAdvisor) Check(ctx advisor.Context, statement string) ([]advireturn nil, err}checker := &columnExistsChecker{- level: level,- title: string(ctx.Rule.Type),- database: ctx.Database,+ level: level,+ title: string(ctx.Rule.Type),+ catalog: ctx.Catalog,}for _, stmt := range stmtList {@@ -66,7 +66,7 @@ type columnExistsChecker struct {title stringtext stringline int- database *catalog.Database+ catalog *catalog.Finder}type columnData struct {@@ -112,7 +112,7 @@ func (checker *columnExistsChecker) Enter(in ast.Node) (ast.Node, bool) {}for _, column := range columnList {- table := checker.database.FindTable(&catalog.TableFind{TableName: column.table})+ table := checker.catalog.FindTable(&catalog.TableFind{TableName: column.table})if table == nil {continue}diff --git a/plugin/advisor/mysql/advisor_database_drop_empty_db.go b/plugin/advisor/mysql/advisor_database_drop_empty_db.goindex b411593b30..4f0bdf532b 100644--- a/plugin/advisor/mysql/advisor_database_drop_empty_db.go+++ b/plugin/advisor/mysql/advisor_database_drop_empty_db.go@@ -37,9 +37,9 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(ctx advisor.Context, statement str}checker := &allowDropEmptyDBChecker{- level: level,- title: string(ctx.Rule.Type),- database: ctx.Database,+ level: level,+ title: string(ctx.Rule.Type),+ catalog: ctx.Catalog,}for _, stmtNode := range root {(stmtNode).Accept(checker)@@ -60,21 +60,21 @@ type allowDropEmptyDBChecker struct {adviceList []advisor.Advicelevel advisor.Statustitle string- database *catalog.Database+ catalog *catalog.Finder}// Enter implements the ast.Visitor interface.func (v *allowDropEmptyDBChecker) Enter(in ast.Node) (ast.Node, bool) {if node, ok := in.(*ast.DropDatabaseStmt); ok {- if v.database.Name != node.Name {+ if v.catalog.DatabaseName() != node.Name {v.adviceList = append(v.adviceList, advisor.Advice{Status: v.level,Code: advisor.NotCurrentDatabase,Title: v.title,- Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", node.Name, v.database.Name),+ Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", node.Name, v.catalog.DatabaseName()),Line: node.OriginTextPosition(),})- } else if !v.database.HasNoTable() {+ } else if !v.catalog.HasNoTable() {v.adviceList = append(v.adviceList, advisor.Advice{Status: v.level,Code: advisor.DatabaseNotEmpty,diff --git a/plugin/advisor/mysql/advisor_index_not_exists.go b/plugin/advisor/mysql/advisor_index_not_exists.goindex 0b6bdd16e5..78a4355af3 100644--- a/plugin/advisor/mysql/advisor_index_not_exists.go+++ b/plugin/advisor/mysql/advisor_index_not_exists.go@@ -38,9 +38,9 @@ func (*IndexNotExistsAdvisor) Check(ctx advisor.Context, statement string) ([]adreturn nil, err}checker := &indexNotExistsChecker{- level: level,- title: string(ctx.Rule.Type),- database: ctx.Database,+ level: level,+ title: string(ctx.Rule.Type),+ catalog: ctx.Catalog,}for _, stmt := range stmtList {@@ -66,7 +66,7 @@ type indexNotExistsChecker struct {title stringtext stringline int- database *catalog.Database+ catalog *catalog.Finder}// Enter implements the ast.Visitor interface.@@ -124,6 +124,6 @@ func (*indexNotExistsChecker) Leave(in ast.Node) (ast.Node, bool) {}func (checker *indexNotExistsChecker) indexExists(table string, index string) bool {- _, idx := checker.database.FindIndex(&catalog.IndexFind{TableName: table, IndexName: index})+ _, idx := checker.catalog.FindIndex(&catalog.IndexFind{TableName: table, IndexName: index})return idx != nil}diff --git a/plugin/advisor/mysql/advisor_index_pk_type.go b/plugin/advisor/mysql/advisor_index_pk_type.goindex aff91e2316..6cbb675850 100644--- a/plugin/advisor/mysql/advisor_index_pk_type.go+++ b/plugin/advisor/mysql/advisor_index_pk_type.go@@ -44,7 +44,7 @@ func (*IndexPkTypeAdvisor) Check(ctx advisor.Context, statement string) ([]advislevel: level,title: string(ctx.Rule.Type),line: make(map[string]int),- database: ctx.Database,+ catalog: ctx.Catalog,tablesNewColumns: make(map[string]columnNameToColumnDef),}@@ -93,7 +93,7 @@ type indexPkTypeChecker struct {level advisor.Statustitle stringline map[string]int- database *catalog.Database+ catalog *catalog.FindertablesNewColumns tableNewColumn}@@ -224,7 +224,7 @@ func (v *indexPkTypeChecker) getPKColumnType(tableName string, columnName stringif colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {return v.getIntOrBigIntStr(colDef.Tp), nil}- column := v.database.FindColumn(&catalog.ColumnFind{+ column := v.catalog.FindColumn(&catalog.ColumnFind{TableName: tableName,ColumnName: columnName,})diff --git a/plugin/advisor/mysql/advisor_index_type_no_blob.go b/plugin/advisor/mysql/advisor_index_type_no_blob.goindex a1e3ad10f3..68a9f2b9cf 100644--- a/plugin/advisor/mysql/advisor_index_type_no_blob.go+++ b/plugin/advisor/mysql/advisor_index_type_no_blob.go@@ -44,7 +44,7 @@ func (*IndexTypeNoBlobAdvisor) Check(ctx advisor.Context, statement string) ([]achecker := &indexTypeNoBlobChecker{level: level,title: string(ctx.Rule.Type),- database: ctx.Database,+ catalog: ctx.Catalog,tablesNewColumns: make(map[string]columnNameToColumnDef),}@@ -71,7 +71,7 @@ type indexTypeNoBlobChecker struct {title stringtext stringline int- database *catalog.Database+ catalog *catalog.FindertablesNewColumns tableNewColumn}@@ -220,7 +220,7 @@ func (v *indexTypeNoBlobChecker) getColumnType(tableName string, columnName striif colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {return v.getBlobStr(colDef.Tp), nil}- column := v.database.FindColumn(&catalog.ColumnFind{+ column := v.catalog.FindColumn(&catalog.ColumnFind{TableName: tableName,ColumnName: columnName,})diff --git a/plugin/advisor/mysql/advisor_naming_index_convention.go b/plugin/advisor/mysql/advisor_naming_index_convention.goindex 7ab29f0421..83a25ab80b 100644--- a/plugin/advisor/mysql/advisor_naming_index_convention.go+++ b/plugin/advisor/mysql/advisor_naming_index_convention.go@@ -48,7 +48,7 @@ func (*NamingIndexConventionAdvisor) Check(ctx advisor.Context, statement stringformat: format,maxLength: maxLength,templateList: templateList,- database: ctx.Database,+ catalog: ctx.Catalog,}for _, stmtNode := range root {(stmtNode).Accept(checker)@@ -73,7 +73,7 @@ type namingIndexConventionChecker struct {format stringmaxLength inttemplateList []string- database *catalog.Database+ catalog *catalog.Finder}// Enter implements the ast.Visitor interface.@@ -154,7 +154,7 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*indfor _, spec := range node.Specs {switch spec.Tp {case ast.AlterTableRenameIndex:- _, index := checker.database.FindIndex(&catalog.IndexFind{+ _, index := checker.catalog.FindIndex(&catalog.IndexFind{TableName: node.Table.Name.String(),IndexName: spec.FromKey.String(),})diff --git a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go b/plugin/advisor/mysql/advisor_naming_unique_key_convention.goindex 87bcf6273c..3d9fed3896 100644--- a/plugin/advisor/mysql/advisor_naming_unique_key_convention.go+++ b/plugin/advisor/mysql/advisor_naming_unique_key_convention.go@@ -47,7 +47,7 @@ func (*NamingUKConventionAdvisor) Check(ctx advisor.Context, statement string) (format: format,maxLength: maxLength,templateList: templateList,- database: ctx.Database,+ catalog: ctx.Catalog,}for _, stmtNode := range root {(stmtNode).Accept(checker)@@ -71,7 +71,7 @@ type namingUKConventionChecker struct {format stringmaxLength inttemplateList []string- database *catalog.Database+ catalog *catalog.Finder}// Enter implements the ast.Visitor interface.@@ -146,7 +146,7 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexMfor _, spec := range node.Specs {switch spec.Tp {case ast.AlterTableRenameIndex:- _, index := checker.database.FindIndex(&catalog.IndexFind{+ _, index := checker.catalog.FindIndex(&catalog.IndexFind{TableName: node.Table.Name.String(),IndexName: spec.FromKey.String(),})diff --git a/plugin/advisor/mysql/advisor_table_exists.go b/plugin/advisor/mysql/advisor_table_exists.goindex 6318c3b324..d6d12f827c 100644--- a/plugin/advisor/mysql/advisor_table_exists.go+++ b/plugin/advisor/mysql/advisor_table_exists.go@@ -38,9 +38,9 @@ func (*TableExistsAdvisor) Check(ctx advisor.Context, statement string) ([]advisreturn nil, err}checker := &tableExistsChecker{- level: level,- title: string(ctx.Rule.Type),- database: ctx.Database,+ level: level,+ title: string(ctx.Rule.Type),+ catalog: ctx.Catalog,}for _, stmt := range stmtList {@@ -66,7 +66,7 @@ type tableExistsChecker struct {title stringtext stringline int- database *catalog.Database+ catalog *catalog.Finder}// Enter implements the ast.Visitor interface.@@ -80,13 +80,13 @@ func (checker *tableExistsChecker) Enter(in ast.Node) (ast.Node, bool) {case *ast.AlterTableStmt:tableList = append(tableList, node.Table.Name.O)case *ast.CreateTableStmt:- if node.ReferTable != nil && sameDatabase(node.ReferTable.Schema.O, checker.database.Name) {+ if node.ReferTable != nil && sameDatabase(node.ReferTable.Schema.O, checker.catalog.DatabaseName()) {tableList = append(tableList, node.ReferTable.Name.O)}}for _, table := range tableList {- if checker.database.FindTable(&catalog.TableFind{TableName: table}) == nil {+ if checker.catalog.FindTable(&catalog.TableFind{TableName: table}) == nil {checker.adviceList = append(checker.adviceList, advisor.Advice{Status: checker.level,Code: advisor.TableNotExists,diff --git a/plugin/advisor/mysql/advisor_table_not_exists.go b/plugin/advisor/mysql/advisor_table_not_exists.goindex e642c95187..0b007d9443 100644--- a/plugin/advisor/mysql/advisor_table_not_exists.go+++ b/plugin/advisor/mysql/advisor_table_not_exists.go@@ -38,9 +38,9 @@ func (*TableNotExistsAdvisor) Check(ctx advisor.Context, statement string) ([]adreturn nil, err}checker := &tableNotExistsChecker{- level: level,- title: string(ctx.Rule.Type),- database: ctx.Database,+ level: level,+ title: string(ctx.Rule.Type),+ catalog: ctx.Catalog,}for _, stmt := range stmtList {@@ -66,7 +66,7 @@ type tableNotExistsChecker struct {title stringtext stringline int- database *catalog.Database+ catalog *catalog.Finder}// Enter implements the ast.Visitor interface.@@ -92,5 +92,5 @@ func (*tableNotExistsChecker) Leave(in ast.Node) (ast.Node, bool) {}func (checker *tableNotExistsChecker) tableExists(table string) bool {- return checker.database.FindTable(&catalog.TableFind{TableName: table}) != nil+ return checker.catalog.FindTable(&catalog.TableFind{TableName: table}) != nil}diff --git a/plugin/advisor/mysql/advisor_table_require_pk.go b/plugin/advisor/mysql/advisor_table_require_pk.goindex 470125ef77..dd790dd915 100644--- a/plugin/advisor/mysql/advisor_table_require_pk.go+++ b/plugin/advisor/mysql/advisor_table_require_pk.go@@ -41,11 +41,11 @@ func (*TableRequirePKAdvisor) Check(ctx advisor.Context, statement string) ([]adreturn nil, err}checker := &tableRequirePKChecker{- level: level,- title: string(ctx.Rule.Type),- tables: make(tablePK),- line: make(map[string]int),- database: ctx.Database,+ level: level,+ title: string(ctx.Rule.Type),+ tables: make(tablePK),+ line: make(map[string]int),+ catalog: ctx.Catalog,}for _, stmtNode := range root {@@ -61,7 +61,7 @@ type tableRequirePKChecker struct {title stringtables tablePKline map[string]int- database *catalog.Database+ catalog *catalog.Finder}// Enter implements the ast.Visitor interface.@@ -169,7 +169,7 @@ func (v *tableRequirePKChecker) createTable(node *ast.CreateTableStmt) {func (v *tableRequirePKChecker) dropColumn(table string, column string) bool {if _, ok := v.tables[table]; !ok {- _, pk := v.database.FindIndex(&catalog.IndexFind{+ _, pk := v.catalog.FindIndex(&catalog.IndexFind{TableName: table,IndexName: primaryKeyName,})diff --git a/plugin/advisor/pg/advisor_column_no_null.go b/plugin/advisor/pg/advisor_column_no_null.goindex 6cc3ee56e3..6df9c4c82c 100644--- a/plugin/advisor/pg/advisor_column_no_null.go+++ b/plugin/advisor/pg/advisor_column_no_null.go@@ -38,7 +38,7 @@ func (*ColumnNoNullAdvisor) Check(ctx advisor.Context, statement string) ([]advichecker := &columnNoNullChecker{level: level,title: string(ctx.Rule.Type),- database: ctx.Database,+ catalog: ctx.Catalog,nullableColumns: make(columnMap),}@@ -53,7 +53,7 @@ type columnNoNullChecker struct {adviceList []advisor.Advicelevel advisor.Statustitle string- database *catalog.Database+ catalog *catalog.FindernullableColumns columnMap}@@ -147,7 +147,7 @@ func (checker *columnNoNullChecker) removeColumnByConstraintList(table *ast.Tablchecker.removeColumn(table, column)}case ast.ConstraintTypePrimaryUsingIndex:- _, index := checker.database.FindIndex(&catalog.IndexFind{+ _, index := checker.catalog.FindIndex(&catalog.IndexFind{SchemaName: normalizeSchemaName(table.Schema),TableName: table.Name,IndexName: constraint.IndexName,diff --git a/plugin/advisor/pg/advisor_naming_index_convention.go b/plugin/advisor/pg/advisor_naming_index_convention.goindex 7f144929f4..980739b3ab 100644--- a/plugin/advisor/pg/advisor_naming_index_convention.go+++ b/plugin/advisor/pg/advisor_naming_index_convention.go@@ -46,7 +46,7 @@ func (*NamingIndexConventionAdvisor) Check(ctx advisor.Context, statement stringformat: format,maxLength: maxLength,templateList: templateList,- database: ctx.Database,+ catalog: ctx.Catalog,}for _, stmt := range stmts {ast.Walk(checker, stmt)@@ -71,7 +71,7 @@ type namingIndexConventionChecker struct {format stringmaxLength inttemplateList []string- database *catalog.Database+ catalog *catalog.Finder}// Visit implements ast.Visitor interface.@@ -132,7 +132,7 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind})}case *ast.RenameIndexStmt:- tableName, index := checker.database.FindIndex(&catalog.IndexFind{+ tableName, index := checker.catalog.FindIndex(&catalog.IndexFind{SchemaName: normalizeSchemaName(node.Table.Schema),TableName: "",IndexName: node.IndexName,diff --git a/plugin/advisor/pg/advisor_naming_primary_key_convention.go b/plugin/advisor/pg/advisor_naming_primary_key_convention.goindex 2863378439..7ddd1741c3 100644--- a/plugin/advisor/pg/advisor_naming_primary_key_convention.go+++ b/plugin/advisor/pg/advisor_naming_primary_key_convention.go@@ -46,7 +46,7 @@ func (*NamingPKConventionAdvisor) Check(ctx advisor.Context, statement string) (format: format,maxLength: maxLength,templateList: templateList,- database: ctx.Database,+ catalog: ctx.Catalog,}for _, stmtNode := range stmts {@@ -72,7 +72,7 @@ type namingPKConventionChecker struct {format stringmaxLength inttemplateList []string- database *catalog.Database+ catalog *catalog.Finder}// Visit implements ast.Visitor interface.@@ -210,7 +210,7 @@ func (checker *namingPKConventionChecker) getPrimaryKeyMetadata(schemaName strin// findIndex returns index found in catalogs, nil if not found.func (checker *namingPKConventionChecker) findIndex(schemaName string, tableName string, indexName string) (string, *catalog.Index) {- return checker.database.FindIndex(&catalog.IndexFind{+ return checker.catalog.FindIndex(&catalog.IndexFind{SchemaName: normalizeSchemaName(schemaName),TableName: tableName,IndexName: indexName,diff --git a/plugin/advisor/pg/advisor_naming_unique_key_convention.go b/plugin/advisor/pg/advisor_naming_unique_key_convention.goindex a479ec7f0e..79b2e01892 100644--- a/plugin/advisor/pg/advisor_naming_unique_key_convention.go+++ b/plugin/advisor/pg/advisor_naming_unique_key_convention.go@@ -46,7 +46,7 @@ func (*NamingUKConventionAdvisor) Check(ctx advisor.Context, statement string) (format: format,maxLength: maxLength,templateList: templateList,- database: ctx.Database,+ catalog: ctx.Catalog,}for _, stmtNode := range root {@@ -72,7 +72,7 @@ type namingUKConventionChecker struct {format stringmaxLength inttemplateList []string- database *catalog.Database+ catalog *catalog.Finder}// Visit implements ast.Visitor interface.@@ -227,7 +227,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) {- return checker.database.FindIndex(&catalog.IndexFind{+ return checker.catalog.FindIndex(&catalog.IndexFind{SchemaName: normalizeSchemaName(schemaName),TableName: tableName,IndexName: indexName,diff --git a/plugin/advisor/pg/advisor_table_require_pk.go b/plugin/advisor/pg/advisor_table_require_pk.goindex 9a9f7a7e7f..16b1c24ae8 100644--- a/plugin/advisor/pg/advisor_table_require_pk.go+++ b/plugin/advisor/pg/advisor_table_require_pk.go@@ -35,9 +35,9 @@ func (*TableRequirePKAdvisor) Check(ctx advisor.Context, statement string) ([]ad}checker := &tableRequirePKChecker{- level: level,- title: string(ctx.Rule.Type),- database: ctx.Database,+ level: level,+ title: string(ctx.Rule.Type),+ catalog: ctx.Catalog,}for _, stmt := range stmts {@@ -60,7 +60,7 @@ type tableRequirePKChecker struct {adviceList []advisor.Advicelevel advisor.Statustitle string- database *catalog.Database+ catalog *catalog.Findertext string}@@ -84,7 +84,7 @@ func (checker *tableRequirePKChecker) Visit(node ast.Node) ast.Visitor {}// DROP CONSTRAINTcase *ast.DropConstraintStmt:- _, index := checker.database.FindIndex(&catalog.IndexFind{+ _, index := checker.catalog.FindIndex(&catalog.IndexFind{SchemaName: normalizeSchemaName(n.Table.Schema),TableName: n.Table.Name,IndexName: n.ConstraintName,@@ -94,7 +94,7 @@ func (checker *tableRequirePKChecker) Visit(node ast.Node) ast.Visitor {}// DROP COLUMNcase *ast.DropColumnStmt:- pk := checker.database.FindPrimaryKey(&catalog.PrimaryKeyFind{+ pk := checker.catalog.FindPrimaryKey(&catalog.PrimaryKeyFind{SchemaName: normalizeSchemaName(n.Table.Schema),TableName: n.Table.Name,})diff --git a/plugin/advisor/sql_review.go b/plugin/advisor/sql_review.goindex ff20be1b46..911432a129 100644--- a/plugin/advisor/sql_review.go+++ b/plugin/advisor/sql_review.go@@ -379,6 +379,7 @@ func SQLReviewCheck(statements string, ruleList []*SQLReviewRule, checkContext Sif err != nil {return nil, errors.Wrap(err, "failed to get database information from catalog")}+for _, rule := range ruleList {if rule.Level == SchemaRuleLevelDisabled {continue@@ -397,7 +398,7 @@ func SQLReviewCheck(statements string, ruleList []*SQLReviewRule, checkContext SCharset: checkContext.Charset,Collation: checkContext.Collation,Rule: rule,- Database: database,+ Catalog: catalog.NewFinder(database),},statements,)diff --git a/plugin/advisor/utils_for_tests.go b/plugin/advisor/utils_for_tests.goindex 50da3ff999..7341879dd7 100644--- a/plugin/advisor/utils_for_tests.go+++ b/plugin/advisor/utils_for_tests.go@@ -125,7 +125,7 @@ func RunSQLReviewRuleTests(Charset: "",Collation: "",Rule: rule,- Database: database,+ Catalog: catalog.NewFinder(database),}for _, tc := range tests {adviceList, err := adv.Check(ctx, tc.Statement)