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: remove CheckIntegrity flag and always enforce integrity checks (#18100)
Danny Xua32f10215b2d
diff --git a/backend/plugin/advisor/catalog/catalog.go b/backend/plugin/advisor/catalog/catalog.goindex bef6430f8d..9590489644 100644--- a/backend/plugin/advisor/catalog/catalog.go+++ b/backend/plugin/advisor/catalog/catalog.go@@ -24,7 +24,7 @@ func NewCatalog(ctx context.Context, s *store.Store, instanceID, databaseName st}dbMetadata = databaseMeta.GetMetadata()}- finderCtx := &FinderContext{CheckIntegrity: true, EngineType: engineType, IgnoreCaseSensitive: !isCaseSensitive}+ finderCtx := &FinderContext{EngineType: engineType, IgnoreCaseSensitive: !isCaseSensitive}origin = NewDatabaseState(dbMetadata, finderCtx)final = NewDatabaseState(dbMetadata, finderCtx)return origin, final, nildiff --git a/backend/plugin/advisor/catalog/finder.go b/backend/plugin/advisor/catalog/finder.goindex db981cb786..85d80da473 100644--- a/backend/plugin/advisor/catalog/finder.go+++ b/backend/plugin/advisor/catalog/finder.go@@ -6,21 +6,6 @@ import (// FinderContext is the context for finder.type FinderContext struct {- // CheckIntegrity defines the policy for integrity checking.- // There are two cases that will cause database to have an empty catalog:- // 1. we cannot fetch the catalog, such as GitHub App/Actions.- // 2. the database is indeed empty.- // We need different logic to deal with these two cases separately.- // If DROP TABLE t and t not exists:- // 1. For case one, just ignore this statement.- // 2. For case two, return the error that table t not exists.- // In addition, We need fine-grained CheckIntegrity.- // Consider the case one, and then create a table t by CREATE TABLE statement.- // After this, drop column a in table t, but column a not exists.- // In this case, we need return the error that column a does not exist in table t,- // instead of ignoring this drop-column statement.- CheckIntegrity bool-// EngineType is the engine type for database engine.EngineType storepb.Enginediff --git a/backend/plugin/advisor/catalog/state.go b/backend/plugin/advisor/catalog/state.goindex a9876ec22e..89e67494bd 100644--- a/backend/plugin/advisor/catalog/state.go+++ b/backend/plugin/advisor/catalog/state.go@@ -744,10 +744,7 @@ func (s *SchemaState) RenameTable(oldName string, newName string) *WalkThroughErtable, exists := s.getTable(oldName)if !exists {- if s.ctx.CheckIntegrity {- return NewTableNotExistsError(oldName)- }- table = s.createIncompleteTable(oldName)+ return NewTableNotExistsError(oldName)}if _, exists := s.getTable(newName); exists {@@ -816,45 +813,39 @@ func (t *TableState) GetColumn(columnName string) (*ColumnState, *WalkThroughErr// DropColumn drops a column from the table.////nolint:revive-func (t *TableState) DropColumn(columnName string, checkIntegrity bool, checkViewDependency func(*ColumnState) ([]string, *WalkThroughError)) *WalkThroughError {+func (t *TableState) DropColumn(columnName string, checkViewDependency func(*ColumnState) ([]string, *WalkThroughError)) *WalkThroughError {column, exists := t.columnSet[strings.ToLower(columnName)]if !exists {- if checkIntegrity {- return NewColumnNotExistsError(t.name, columnName)- }- return nil+ return NewColumnNotExistsError(t.name, columnName)}- // Check integrity mode requirements- if checkIntegrity {- // Cannot drop all columns in a table using ALTER TABLE DROP COLUMN.- if len(t.columnSet) == 1 {- return &WalkThroughError{- Type: ErrorTypeDropAllColumns,- Content: fmt.Sprintf("Can't delete all columns with ALTER TABLE; use DROP TABLE %s instead", t.name),- }+ // Cannot drop all columns in a table using ALTER TABLE DROP COLUMN.+ if len(t.columnSet) == 1 {+ return &WalkThroughError{+ Type: ErrorTypeDropAllColumns,+ Content: fmt.Sprintf("Can't delete all columns with ALTER TABLE; use DROP TABLE %s instead", t.name),}+ }- // Check for view dependencies if checker is provided- if checkViewDependency != nil {- viewList, err := checkViewDependency(column)- if err != nil {- return err- }- if len(viewList) > 0 {- return &WalkThroughError{- Type: ErrorTypeColumnIsReferencedByView,- Content: fmt.Sprintf("Cannot drop column %q in table %q, it's referenced by view: %s", column.name, t.name, strings.Join(viewList, ", ")),- Payload: viewList,- }+ // Check for view dependencies if checker is provided+ if checkViewDependency != nil {+ viewList, err := checkViewDependency(column)+ if err != nil {+ return err+ }+ if len(viewList) > 0 {+ return &WalkThroughError{+ Type: ErrorTypeColumnIsReferencedByView,+ Content: fmt.Sprintf("Cannot drop column %q in table %q, it's referenced by view: %s", column.name, t.name, strings.Join(viewList, ", ")),+ Payload: viewList,}}+ }- // Modify the column position- for _, col := range t.columnSet {- if *col.position > *column.position {- *col.position--- }+ // Modify the column position+ for _, col := range t.columnSet {+ if *col.position > *column.position {+ *col.position--}}@@ -882,17 +873,14 @@ func (t *TableState) DropColumn(columnName string, checkIntegrity bool, checkVie// RenameColumn renames a column in the table.////nolint:revive-func (t *TableState) RenameColumn(oldName string, newName string, checkIntegrity bool) *WalkThroughError {+func (t *TableState) RenameColumn(oldName string, newName string) *WalkThroughError {if strings.EqualFold(oldName, newName) {return nil}column, exists := t.columnSet[strings.ToLower(oldName)]if !exists {- if checkIntegrity {- return NewColumnNotExistsError(t.name, oldName)- }- column = t.createIncompleteColumn(oldName)+ return NewColumnNotExistsError(t.name, oldName)}if _, exists := t.columnSet[strings.ToLower(newName)]; exists {@@ -1037,17 +1025,15 @@ func (t *TableState) GetIndex(indexName string) (*IndexState, *WalkThroughError)// DropIndex drops an index from the table.////nolint:revive-func (t *TableState) DropIndex(indexName string, checkIntegrity bool, identifierMap identifierMap) *WalkThroughError {- if checkIntegrity {- if _, exists := t.indexSet[strings.ToLower(indexName)]; !exists {- if strings.EqualFold(indexName, PrimaryKeyName) {- return &WalkThroughError{- Type: ErrorTypePrimaryKeyNotExists,- Content: fmt.Sprintf("Primary key does not exist in table `%s`", t.name),- }+func (t *TableState) DropIndex(indexName string, identifierMap identifierMap) *WalkThroughError {+ if _, exists := t.indexSet[strings.ToLower(indexName)]; !exists {+ if strings.EqualFold(indexName, PrimaryKeyName) {+ return &WalkThroughError{+ Type: ErrorTypePrimaryKeyNotExists,+ Content: fmt.Sprintf("Primary key does not exist in table `%s`", t.name),}- return NewIndexNotExistsError(t.name, indexName)}+ return NewIndexNotExistsError(t.name, indexName)}// For PostgreSQL, remove from identifier map@@ -1062,7 +1048,7 @@ func (t *TableState) DropIndex(indexName string, checkIntegrity bool, identifier// RenameIndex renames an index in the table.////nolint:revive-func (t *TableState) RenameIndex(oldName string, newName string, checkIntegrity bool, identifierMap identifierMap) *WalkThroughError {+func (t *TableState) RenameIndex(oldName string, newName string, identifierMap identifierMap) *WalkThroughError {// For MySQL, the primary key has a special name 'PRIMARY'.if strings.ToUpper(oldName) == PrimaryKeyName || strings.ToUpper(newName) == PrimaryKeyName {incorrectName := oldName@@ -1077,10 +1063,7 @@ func (t *TableState) RenameIndex(oldName string, newName string, checkIntegrityindex, exists := t.indexSet[strings.ToLower(oldName)]if !exists {- if checkIntegrity {- return NewIndexNotExistsError(t.name, oldName)- }- index = t.createIncompleteIndex(oldName)+ return NewIndexNotExistsError(t.name, oldName)}if _, exists := t.indexSet[strings.ToLower(newName)]; exists {diff --git a/backend/plugin/advisor/catalog/walk_through.go b/backend/plugin/advisor/catalog/walk_through.goindex 97fe2947fd..425de41072 100644--- a/backend/plugin/advisor/catalog/walk_through.go+++ b/backend/plugin/advisor/catalog/walk_through.go@@ -186,13 +186,7 @@ func WalkThrough(d *DatabaseState, ast any) error {case storepb.Engine_MYSQL, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:return MySQLWalkThrough(d, ast)case storepb.Engine_POSTGRES:- if err := PgWalkThrough(d, ast); err != nil {- if d.ctx.CheckIntegrity {- return err- }- d.usable = false- }- return nil+ return PgWalkThrough(d, ast)default:return &WalkThroughError{Type: ErrorTypeUnsupported,@@ -228,29 +222,18 @@ func (s *SchemaState) getTable(table string) (*TableState, bool) {return nil, false}-func (t *TableState) createIncompleteIndex(name string) *IndexState {- index := &IndexState{- name: name,- }- t.indexSet[strings.ToLower(name)] = index- return index-}-//nolint:revive-func (s *SchemaState) renameTable(ctx *FinderContext, oldName string, newName string) *WalkThroughError {+func (s *SchemaState) renameTable(_ *FinderContext, oldName string, newName string) *WalkThroughError {if oldName == newName {return nil}table, exists := s.getTable(oldName)if !exists {- if ctx.CheckIntegrity {- return &WalkThroughError{- Type: ErrorTypeTableNotExists,- Content: fmt.Sprintf("Table `%s` does not exist", oldName),- }+ return &WalkThroughError{+ Type: ErrorTypeTableNotExists,+ Content: fmt.Sprintf("Table `%s` does not exist", oldName),}- table = s.createIncompleteTable(oldName)}if _, exists := s.getTable(newName); exists {@@ -266,24 +249,6 @@ func (s *SchemaState) renameTable(ctx *FinderContext, oldName string, newName streturn nil}-func (s *SchemaState) createIncompleteTable(name string) *TableState {- table := &TableState{- name: name,- columnSet: make(columnStateMap),- indexSet: make(IndexStateMap),- }- s.tableSet[name] = table- return table-}--func (t *TableState) createIncompleteColumn(name string) *ColumnState {- column := &ColumnState{- name: name,- }- t.columnSet[strings.ToLower(name)] = column- return column-}-func (d *DatabaseState) createSchema() *SchemaState {schema := &SchemaState{ctx: d.ctx,diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql.go b/backend/plugin/advisor/catalog/walk_through_for_mysql.goindex d77534cbb8..72b5f35a15 100644--- a/backend/plugin/advisor/catalog/walk_through_for_mysql.go+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql.go@@ -190,7 +190,7 @@ func (l *mysqlListener) EnterDropTable(ctx *mysql.DropTableContext) {table, exists := schema.getTable(tableName)if !exists {- if ctx.IfExists() != nil || !l.databaseState.ctx.CheckIntegrity {+ if ctx.IfExists() != nil {return}l.err = &WalkThroughError{@@ -304,20 +304,20 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {// drop column.case item.ColumnInternalRef() != nil:columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())- if err := table.DropColumn(columnName, l.databaseState.ctx.CheckIntegrity, nil); err != nil {+ if err := table.DropColumn(columnName, nil); err != nil {l.err = errreturn}// drop primary key.case item.PRIMARY_SYMBOL() != nil && item.KEY_SYMBOL() != nil:- if err := table.DropIndex(PrimaryKeyName, l.databaseState.ctx.CheckIntegrity, nil); err != nil {+ if err := table.DropIndex(PrimaryKeyName, nil); err != nil {l.err = errreturn}// drop key/index.case item.KeyOrIndex() != nil && item.IndexRef() != nil:_, _, indexName := mysqlparser.NormalizeIndexRef(item.IndexRef())- if err := table.DropIndex(indexName, l.databaseState.ctx.CheckIntegrity, nil); err != nil {+ if err := table.DropIndex(indexName, nil); err != nil {l.err = errreturn}@@ -343,7 +343,7 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {case item.RENAME_SYMBOL() != nil && item.COLUMN_SYMBOL() != nil:oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())newColumnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())- if err := table.RenameColumn(oldColumnName, newColumnName, l.databaseState.ctx.CheckIntegrity); err != nil {+ if err := table.RenameColumn(oldColumnName, newColumnName); err != nil {l.err = errreturn}@@ -376,7 +376,7 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {case item.RENAME_SYMBOL() != nil && item.KeyOrIndex() != nil && item.IndexRef() != nil && item.IndexName() != nil:_, _, oldIndexName := mysqlparser.NormalizeIndexRef(item.IndexRef())newIndexName := mysqlparser.NormalizeIndexName(item.IndexName())- if err := table.RenameIndex(oldIndexName, newIndexName, l.databaseState.ctx.CheckIntegrity, nil); err != nil {+ if err := table.RenameIndex(oldIndexName, newIndexName, nil); err != nil {l.err = errreturn}@@ -406,7 +406,7 @@ func (l *mysqlListener) EnterDropIndex(ctx *mysql.DropIndexContext) {}_, _, indexName := mysqlparser.NormalizeIndexRef(ctx.IndexRef())- if err := table.DropIndex(indexName, l.databaseState.ctx.CheckIntegrity, nil); err != nil {+ if err := table.DropIndex(indexName, nil); err != nil {l.err = err}}@@ -547,11 +547,8 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemen}table, exists := schema.getTable(oldTableName)if !exists {- if schema.ctx.CheckIntegrity {- l.err = NewTableNotExistsError(oldTableName)- return- }- table = schema.createIncompleteTable(oldTableName)+ l.err = NewTableNotExistsError(oldTableName)+ return}if _, exists := schema.getTable(newTableName); exists {l.err = NewTableExistsError(newTableName)@@ -562,7 +559,7 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemenschema.tableSet[table.name] = table} else if l.databaseState.mysqlMoveToOtherDatabase(pair) {_, exists := schema.getTable(oldTableName)- if !exists && schema.ctx.CheckIntegrity {+ if !exists {l.err = NewTableNotExistsError(oldTableName)return}@@ -644,13 +641,10 @@ func (d *DatabaseState) mysqlTheCurrentDatabase(renamePair mysql.IRenamePairContreturn true}-func (t *TableState) mysqlChangeIndexVisibility(ctx *FinderContext, indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {+func (t *TableState) mysqlChangeIndexVisibility(_ *FinderContext, indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {index, exists := t.indexSet[strings.ToLower(indexName)]if !exists {- if ctx.CheckIntegrity {- return NewIndexNotExistsError(t.name, indexName)- }- index = t.createIncompleteIndex(indexName)+ return NewIndexNotExistsError(t.name, indexName)}switch {case visibility.VISIBLE_SYMBOL() != nil:@@ -663,7 +657,7 @@ func (t *TableState) mysqlChangeIndexVisibility(ctx *FinderContext, indexName streturn nil}-func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterListItemContext) *WalkThroughError {+func (t *TableState) mysqlAlterColumn(_ *FinderContext, itemDef mysql.IAlterListItemContext) *WalkThroughError {if itemDef.ColumnInternalRef() == nil {// should not reach here.return nil@@ -671,10 +665,7 @@ func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterLicolumnName := mysqlparser.NormalizeMySQLColumnInternalRef(itemDef.ColumnInternalRef())colState, exists := t.columnSet[strings.ToLower(columnName)]if !exists {- if ctx.CheckIntegrity {- return NewColumnNotExistsError(t.name, columnName)- }- colState = t.createIncompleteColumn(columnName)+ return NewColumnNotExistsError(t.name, columnName)}switch {@@ -734,22 +725,7 @@ func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterLi}func (t *TableState) mysqlChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {- if ctx.CheckIntegrity {- return t.mysqlCompleteTableChangeColumn(ctx, oldColumnName, newColumnName, fieldDef, position)- }- return t.mysqlIncompleteTableChangeColumn(ctx, oldColumnName, newColumnName, fieldDef, position)-}--// mysqlIncompleteTableChangeColumn changes column definition.-// It does not maintain the position of the column.-func (t *TableState) mysqlIncompleteTableChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {- delete(t.columnSet, strings.ToLower(oldColumnName))-- // rename column from indexSet- t.renameColumnInIndexKey(oldColumnName, newColumnName)-- // create a new column in columnSet- return t.mysqlCreateColumn(ctx, newColumnName, fieldDef, position)+ return t.mysqlCompleteTableChangeColumn(ctx, oldColumnName, newColumnName, fieldDef, position)}// mysqlCompleteTableChangeColumn changes column definition.@@ -855,10 +831,7 @@ func (d *DatabaseState) mysqlFindTableState(databaseName, tableName string) (*Tatable, exists := schema.getTable(tableName)if !exists {- if schema.ctx.CheckIntegrity {- return nil, NewTableNotExistsError(tableName)- }- table = schema.createIncompleteTable(tableName)+ return nil, NewTableNotExistsError(tableName)}return table, nil@@ -965,23 +938,20 @@ func (t *TableState) mysqlValidateKeyListVariants(ctx *FinderContext, keyList myreturn nil}-func (t *TableState) mysqlValidateColumnList(ctx *FinderContext, columnList []string, primary bool, isSpatial bool) *WalkThroughError {+func (t *TableState) mysqlValidateColumnList(_ *FinderContext, columnList []string, primary bool, isSpatial bool) *WalkThroughError {for _, columnName := range columnList {column, exists := t.columnSet[strings.ToLower(columnName)]if !exists {- if ctx.CheckIntegrity {- return NewColumnNotExistsError(t.name, columnName)- }- } else {- if primary {- column.nullable = newFalsePointer()- }- if isSpatial && column.nullable != nil && *column.nullable {- return &WalkThroughError{- Type: ErrorTypeSpatialIndexKeyNullable,- // The error content comes from MySQL.- Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),- }+ return NewColumnNotExistsError(t.name, columnName)+ }+ if primary {+ column.nullable = newFalsePointer()+ }+ if isSpatial && column.nullable != nil && *column.nullable {+ return &WalkThroughError{+ Type: ErrorTypeSpatialIndexKeyNullable,+ // The error content comes from MySQL.+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),}}}@@ -1050,7 +1020,7 @@ func mysqlGetIndexType(tableConstraint mysql.ITableConstraintDefContext) stringreturn "BTREE"}-func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {+func (t *TableState) mysqlCreateColumn(_ *FinderContext, columnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {if _, exists := t.columnSet[strings.ToLower(columnName)]; exists {return &WalkThroughError{Type: ErrorTypeColumnExists,@@ -1060,7 +1030,7 @@ func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fi// todo: handle position.pos := len(t.columnSet) + 1- if position != nil && ctx.CheckIntegrity {+ if position != nil {var err *WalkThroughErrorpos, err = t.mysqlReorderColumn(position)if err != nil {diff --git a/backend/plugin/advisor/catalog/walk_through_for_tidb.go b/backend/plugin/advisor/catalog/walk_through_for_tidb.goindex 6c027f829b..b17cd9b647 100644--- a/backend/plugin/advisor/catalog/walk_through_for_tidb.go+++ b/backend/plugin/advisor/catalog/walk_through_for_tidb.go@@ -85,10 +85,7 @@ func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) *WalkThroughE}table, exists := schema.getTable(oldTableName)if !exists {- if schema.ctx.CheckIntegrity {- return NewTableNotExistsError(oldTableName)- }- table = schema.createIncompleteTable(oldTableName)+ return NewTableNotExistsError(oldTableName)}if _, exists := schema.getTable(newTableName); exists {return NewTableExistsError(newTableName)@@ -98,7 +95,7 @@ func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) *WalkThroughEschema.tableSet[table.name] = table} else if d.moveToOtherDatabase(tableToTable) {_, exists := schema.getTable(tableToTable.OldTable.Name.O)- if !exists && schema.ctx.CheckIntegrity {+ if !exists {return NewTableNotExistsError(tableToTable.OldTable.Name.O)}delete(schema.tableSet, tableToTable.OldTable.Name.O)@@ -172,10 +169,7 @@ func (d *DatabaseState) findTableState(tableName *tidbast.TableName) (*TableStattable, exists := schema.getTable(tableName.Name.O)if !exists {- if schema.ctx.CheckIntegrity {- return nil, NewTableNotExistsError(tableName.Name.O)- }- table = schema.createIncompleteTable(tableName.Name.O)+ return nil, NewTableNotExistsError(tableName.Name.O)}return table, nil@@ -187,7 +181,7 @@ func (d *DatabaseState) dropIndex(node *tidbast.DropIndexStmt) *WalkThroughErrorreturn err}- return table.DropIndex(node.IndexName, d.ctx.CheckIntegrity, nil)+ return table.DropIndex(node.IndexName, nil)}func (d *DatabaseState) createIndex(node *tidbast.CreateIndexStmt) *WalkThroughError {@@ -263,15 +257,15 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErrreturn err}case tidbast.AlterTableDropColumn:- if err := table.DropColumn(spec.OldColumnName.Name.O, d.ctx.CheckIntegrity, nil); err != nil {+ if err := table.DropColumn(spec.OldColumnName.Name.O, nil); err != nil {return err}case tidbast.AlterTableDropPrimaryKey:- if err := table.DropIndex(PrimaryKeyName, d.ctx.CheckIntegrity, nil); err != nil {+ if err := table.DropIndex(PrimaryKeyName, nil); err != nil {return err}case tidbast.AlterTableDropIndex:- if err := table.DropIndex(spec.Name, d.ctx.CheckIntegrity, nil); err != nil {+ if err := table.DropIndex(spec.Name, nil); err != nil {return err}case tidbast.AlterTableDropForeignKey:@@ -285,7 +279,7 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErrreturn err}case tidbast.AlterTableRenameColumn:- if err := table.RenameColumn(spec.OldColumnName.Name.O, spec.NewColumnName.Name.O, d.ctx.CheckIntegrity); err != nil {+ if err := table.RenameColumn(spec.OldColumnName.Name.O, spec.NewColumnName.Name.O); err != nil {return err}case tidbast.AlterTableRenameTable:@@ -298,7 +292,7 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErrreturn err}case tidbast.AlterTableRenameIndex:- if err := table.RenameIndex(spec.FromKey.O, spec.ToKey.O, d.ctx.CheckIntegrity, nil); err != nil {+ if err := table.RenameIndex(spec.FromKey.O, spec.ToKey.O, nil); err != nil {return err}case tidbast.AlterTableIndexInvisible:@@ -313,13 +307,10 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErrreturn nil}-func (t *TableState) changeIndexVisibility(ctx *FinderContext, indexName string, visibility tidbast.IndexVisibility) *WalkThroughError {+func (t *TableState) changeIndexVisibility(_ *FinderContext, indexName string, visibility tidbast.IndexVisibility) *WalkThroughError {index, exists := t.indexSet[strings.ToLower(indexName)]if !exists {- if ctx.CheckIntegrity {- return NewIndexNotExistsError(t.name, indexName)- }- index = t.createIncompleteIndex(indexName)+ return NewIndexNotExistsError(t.name, indexName)}switch visibility {case tidbast.IndexVisibilityVisible:@@ -332,14 +323,11 @@ func (t *TableState) changeIndexVisibility(ctx *FinderContext, indexName string,return nil}-func (t *TableState) changeColumnDefault(ctx *FinderContext, column *tidbast.ColumnDef) *WalkThroughError {+func (t *TableState) changeColumnDefault(_ *FinderContext, column *tidbast.ColumnDef) *WalkThroughError {columnName := column.Name.Name.LcolState, exists := t.columnSet[columnName]if !exists {- if ctx.CheckIntegrity {- return NewColumnNotExistsError(t.name, columnName)- }- colState = t.createIncompleteColumn(columnName)+ return NewColumnNotExistsError(t.name, columnName)}if len(column.Options) == 1 {@@ -452,23 +440,8 @@ func (t *TableState) completeTableChangeColumn(ctx *FinderContext, oldName strinreturn t.createColumn(ctx, newColumn, position)}-// incompleteTableChangeColumn changes column definition.-// It does not maintain the position of the column.-func (t *TableState) incompleteTableChangeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {- delete(t.columnSet, strings.ToLower(oldName))-- // rename column from indexSet- t.renameColumnInIndexKey(oldName, newColumn.Name.Name.O)-- // create a new column in columnSet- return t.createColumn(ctx, newColumn, position)-}-func (t *TableState) changeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {- if ctx.CheckIntegrity {- return t.completeTableChangeColumn(ctx, oldName, newColumn, position)- }- return t.incompleteTableChangeColumn(ctx, oldName, newColumn, position)+ return t.completeTableChangeColumn(ctx, oldName, newColumn, position)}// reorderColumn reorders the columns for new column and returns the new column position.@@ -519,7 +492,7 @@ func (d *DatabaseState) dropTable(node *tidbast.DropTableStmt) *WalkThroughErrortable, exists := schema.getTable(name.Name.O)if !exists {- if node.IfExists || !d.ctx.CheckIntegrity {+ if node.IfExists {return nil}return &WalkThroughError{@@ -669,7 +642,7 @@ func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Coreturn nil}-func (t *TableState) validateAndGetKeyStringList(ctx *FinderContext, keyList []*tidbast.IndexPartSpecification, primary bool, isSpatial bool) ([]string, *WalkThroughError) {+func (t *TableState) validateAndGetKeyStringList(_ *FinderContext, keyList []*tidbast.IndexPartSpecification, primary bool, isSpatial bool) ([]string, *WalkThroughError) {var res []stringfor _, key := range keyList {if key.Expr != nil {@@ -682,19 +655,16 @@ func (t *TableState) validateAndGetKeyStringList(ctx *FinderContext, keyList []*columnName := key.Column.Name.Lcolumn, exists := t.columnSet[columnName]if !exists {- if ctx.CheckIntegrity {- return nil, NewColumnNotExistsError(t.name, columnName)- }- } else {- if primary {- column.nullable = newFalsePointer()- }- if isSpatial && column.nullable != nil && *column.nullable {- return nil, &WalkThroughError{- Type: ErrorTypeSpatialIndexKeyNullable,- // The error content comes from MySQL.- Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),- }+ return nil, NewColumnNotExistsError(t.name, columnName)+ }+ if primary {+ column.nullable = newFalsePointer()+ }+ if isSpatial && column.nullable != nil && *column.nullable {+ return nil, &WalkThroughError{+ Type: ErrorTypeSpatialIndexKeyNullable,+ // The error content comes from MySQL.+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.name),}}@@ -740,7 +710,7 @@ func checkDefault(columnName string, columnType *types.FieldType, value tidbast.}//nolint:revive-func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {+func (t *TableState) createColumn(_ *FinderContext, column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {if _, exists := t.columnSet[column.Name.Name.L]; exists {return &WalkThroughError{Type: ErrorTypeColumnExists,@@ -749,7 +719,7 @@ func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef,}pos := len(t.columnSet) + 1- if position != nil && ctx.CheckIntegrity {+ if position != nil {var err *WalkThroughErrorpos, err = t.reorderColumn(position)if err != nil {diff --git a/backend/plugin/advisor/catalog/walk_through_test.go b/backend/plugin/advisor/catalog/walk_through_test.goindex c7cc7a26ba..324d178f6c 100644--- a/backend/plugin/advisor/catalog/walk_through_test.go+++ b/backend/plugin/advisor/catalog/walk_through_test.go@@ -54,16 +54,6 @@ func TestMySQLWalkThrough(t *testing.T) {}}-func TestMySQLWalkThroughForIncomplete(t *testing.T) {- tests := []string{- "mysql_walk_through_for_incomplete",- }-- for _, test := range tests {- runWalkThroughTest(t, test, storepb.Engine_MYSQL, nil)- }-}-func TestPostgreSQLWalkThrough(t *testing.T) {originDatabase := &storepb.DatabaseSchemaMetadata{Name: "postgres",@@ -196,9 +186,9 @@ func runWalkThroughTest(t *testing.T, file string, engineType storepb.Engine, orfor _, test := range tests {var state *DatabaseStateif originDatabase != nil {- state = NewDatabaseState(originDatabase, &FinderContext{CheckIntegrity: true, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})+ state = NewDatabaseState(originDatabase, &FinderContext{EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})} else {- state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, &FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})+ state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, &FinderContext{EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})}asts, _ := sm.GetASTsForChecks(engineType, test.Statement)@@ -245,9 +235,9 @@ func runANTLRWalkThroughTest(t *testing.T, file string, engineType storepb.Enginfor _, test := range tests {var state *DatabaseStateif originDatabase != nil {- state = NewDatabaseState(originDatabase, &FinderContext{CheckIntegrity: true, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})+ state = NewDatabaseState(originDatabase, &FinderContext{EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})} else {- state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, &FinderContext{CheckIntegrity: false, EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})+ state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, &FinderContext{EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})}// Parse using ANTLR parser instead of legacy parserdiff --git a/backend/plugin/advisor/pg/test_helper.go b/backend/plugin/advisor/pg/test_helper.goindex d55e0a324b..916616b21a 100644--- a/backend/plugin/advisor/pg/test_helper.go+++ b/backend/plugin/advisor/pg/test_helper.go@@ -59,7 +59,7 @@ func RunANTLRAdvisorRuleTest(t *testing.T, rule advisor.SQLReviewRuleType, dbTyp}database := advisor.MockPostgreSQLDatabase- ctx := &catalog.FinderContext{CheckIntegrity: true, EngineType: dbType}+ ctx := &catalog.FinderContext{EngineType: dbType}originCatalog := catalog.NewDatabaseState(database, ctx)finalCatalog := catalog.NewDatabaseState(database, ctx)diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex cc71c24dbf..312795e750 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -200,7 +200,7 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.Eif dbType == storepb.Engine_POSTGRES {database = MockPostgreSQLDatabase}- ctx := &catalog.FinderContext{CheckIntegrity: true, EngineType: dbType}+ ctx := &catalog.FinderContext{EngineType: dbType}originCatalog := catalog.NewDatabaseState(database, ctx)finalCatalog := catalog.NewDatabaseState(database, ctx)