Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

fix: check column name case insensitive in walk-through (#9549)

rebelicef178e0d56b94
diff --git a/backend/plugin/advisor/catalog/state.go b/backend/plugin/advisor/catalog/state.go
index 9edee15b15..4f73864528 100644
--- a/backend/plugin/advisor/catalog/state.go
+++ b/backend/plugin/advisor/catalog/state.go
@@ -54,7 +54,7 @@ func newSchemaState(s *storepb.SchemaMetadata, context *FinderContext) *SchemaSt
}
for _, table := range s.Tables {
- tableState := newTableState(table)
+ tableState := newTableState(table, context)
schema.tableSet[table.Name] = tableState
schema.identifierMap[table.Name] = true
@@ -80,7 +80,7 @@ func newViewState(v *storepb.ViewMetadata) *ViewState {
}
}
-func newTableState(t *storepb.TableMetadata) *TableState {
+func newTableState(t *storepb.TableMetadata, context *FinderContext) *TableState {
table := &TableState{
name: t.Name,
engine: newStringPointer(t.Engine),
@@ -92,11 +92,21 @@ func newTableState(t *storepb.TableMetadata) *TableState {
}
for i, column := range t.Columns {
- table.columnSet[column.Name] = newColumnState(column, i+1)
+ columnName := column.Name
+ switch context.EngineType {
+ case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB:
+ columnName = strings.ToLower(columnName)
+ }
+ table.columnSet[columnName] = newColumnState(column, i+1)
}
for _, index := range t.Indexes {
- table.indexSet[index.Name] = newIndexState(index)
+ indexName := index.Name
+ switch context.EngineType {
+ case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB:
+ indexName = strings.ToLower(indexName)
+ }
+ table.indexSet[indexName] = newIndexState(index)
}
return table
@@ -202,6 +212,10 @@ type IndexFind struct {
// FindIndex finds the index.
func (d *DatabaseState) FindIndex(find *IndexFind) (string, *IndexState) {
+ switch d.dbType {
+ case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB:
+ find.IndexName = strings.ToLower(find.IndexName)
+ }
// There are two cases to find a index:
// 1. find an index in specific table. e.g. MySQL and TiDB.
// 2. find an index in the schema. e.g. PostgreSQL.
@@ -301,6 +315,10 @@ type ColumnFind struct {
// FindColumn finds the column.
func (d *DatabaseState) FindColumn(find *ColumnFind) *ColumnState {
+ switch d.dbType {
+ case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB:
+ find.ColumnName = strings.ToLower(find.ColumnName)
+ }
schema, exists := d.schemaSet[find.SchemaName]
if !exists {
return nil
diff --git a/backend/plugin/advisor/catalog/walk_through.go b/backend/plugin/advisor/catalog/walk_through.go
index 5e1bbaf4a5..570f42c88c 100644
--- a/backend/plugin/advisor/catalog/walk_through.go
+++ b/backend/plugin/advisor/catalog/walk_through.go
@@ -560,7 +560,7 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
}
func (t *TableState) changeIndexVisibility(ctx *FinderContext, indexName string, visibility tidbast.IndexVisibility) *WalkThroughError {
- index, exists := t.indexSet[indexName]
+ index, exists := t.indexSet[strings.ToLower(indexName)]
if !exists {
if ctx.CheckIntegrity {
return NewIndexNotExistsError(t.name, indexName)
@@ -590,7 +590,7 @@ func (t *TableState) renameIndex(ctx *FinderContext, oldName string, newName str
}
}
- index, exists := t.indexSet[oldName]
+ index, exists := t.indexSet[strings.ToLower(oldName)]
if !exists {
if ctx.CheckIntegrity {
return NewIndexNotExistsError(t.name, oldName)
@@ -598,13 +598,13 @@ func (t *TableState) renameIndex(ctx *FinderContext, oldName string, newName str
index = t.createIncompleteIndex(oldName)
}
- if _, exists := t.indexSet[newName]; exists {
+ if _, exists := t.indexSet[strings.ToLower(newName)]; exists {
return NewIndexExistsError(t.name, newName)
}
index.name = newName
- delete(t.indexSet, oldName)
- t.indexSet[newName] = index
+ delete(t.indexSet, strings.ToLower(oldName))
+ t.indexSet[strings.ToLower(newName)] = index
return nil
}
@@ -612,12 +612,12 @@ func (t *TableState) createIncompleteIndex(name string) *IndexState {
index := &IndexState{
name: name,
}
- t.indexSet[name] = index
+ t.indexSet[strings.ToLower(name)] = index
return index
}
func (t *TableState) changeColumnDefault(ctx *FinderContext, column *tidbast.ColumnDef) *WalkThroughError {
- columnName := column.Name.Name.O
+ columnName := column.Name.Name.L
colState, exists := t.columnSet[columnName]
if !exists {
if ctx.CheckIntegrity {
@@ -705,11 +705,11 @@ func (s *SchemaState) createIncompleteTable(name string) *TableState {
}
func (t *TableState) renameColumn(ctx *FinderContext, oldName string, newName string) *WalkThroughError {
- if oldName == newName {
+ if strings.EqualFold(oldName, newName) {
return nil
}
- column, exists := t.columnSet[oldName]
+ column, exists := t.columnSet[strings.ToLower(oldName)]
if !exists {
if ctx.CheckIntegrity {
return &WalkThroughError{
@@ -720,7 +720,7 @@ func (t *TableState) renameColumn(ctx *FinderContext, oldName string, newName st
column = t.createIncompleteColumn(oldName)
}
- if _, exists := t.columnSet[newName]; exists {
+ if _, exists := t.columnSet[strings.ToLower(newName)]; exists {
return &WalkThroughError{
Type: ErrorTypeColumnExists,
Content: fmt.Sprintf("Column `%s` already exists in table `%s", newName, t.name),
@@ -728,8 +728,8 @@ func (t *TableState) renameColumn(ctx *FinderContext, oldName string, newName st
}
column.name = newName
- delete(t.columnSet, oldName)
- t.columnSet[newName] = column
+ delete(t.columnSet, strings.ToLower(oldName))
+ t.columnSet[strings.ToLower(newName)] = column
t.renameColumnInIndexKey(oldName, newName)
return nil
@@ -739,17 +739,17 @@ func (t *TableState) createIncompleteColumn(name string) *ColumnState {
column := &ColumnState{
name: name,
}
- t.columnSet[name] = column
+ t.columnSet[strings.ToLower(name)] = column
return column
}
func (t *TableState) renameColumnInIndexKey(oldName string, newName string) {
- if oldName == newName {
+ if strings.EqualFold(oldName, newName) {
return
}
for _, index := range t.indexSet {
for i, key := range index.expressionList {
- if key == oldName {
+ if strings.EqualFold(key, oldName) {
index.expressionList[i] = newName
}
}
@@ -762,7 +762,7 @@ func (t *TableState) renameColumnInIndexKey(oldName string, newName string) {
// 2. rename column from indexSet.
// 3. create a new column in columnSet.
func (t *TableState) completeTableChangeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
- column, exists := t.columnSet[oldName]
+ column, exists := t.columnSet[strings.ToLower(oldName)]
if !exists {
return NewColumnNotExistsError(t.name, oldName)
}
@@ -793,7 +793,7 @@ func (t *TableState) completeTableChangeColumn(ctx *FinderContext, oldName strin
*col.position--
}
}
- delete(t.columnSet, column.name)
+ delete(t.columnSet, strings.ToLower(column.name))
// rename column from indexSet
t.renameColumnInIndexKey(oldName, newColumn.Name.Name.O)
@@ -805,7 +805,7 @@ func (t *TableState) completeTableChangeColumn(ctx *FinderContext, oldName strin
// 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, oldName)
+ delete(t.columnSet, strings.ToLower(oldName))
// rename column from indexSet
t.renameColumnInIndexKey(oldName, newColumn.Name.Name.O)
@@ -823,8 +823,8 @@ func (t *TableState) changeColumn(ctx *FinderContext, oldName string, newColumn
func (t *TableState) dropIndex(ctx *FinderContext, indexName string) *WalkThroughError {
if ctx.CheckIntegrity {
- if _, exists := t.indexSet[indexName]; !exists {
- if indexName == PrimaryKeyName {
+ 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),
@@ -834,12 +834,12 @@ func (t *TableState) dropIndex(ctx *FinderContext, indexName string) *WalkThroug
}
}
- delete(t.indexSet, indexName)
+ delete(t.indexSet, strings.ToLower(indexName))
return nil
}
func (t *TableState) completeTableDropColumn(columnName string) *WalkThroughError {
- column, exists := t.columnSet[columnName]
+ column, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
return NewColumnNotExistsError(t.name, columnName)
}
@@ -858,7 +858,7 @@ func (t *TableState) completeTableDropColumn(columnName string) *WalkThroughErro
index.dropColumn(columnName)
// If all columns that make up an index are dropped, the index is dropped as well.
if len(index.expressionList) == 0 {
- delete(t.indexSet, index.name)
+ delete(t.indexSet, strings.ToLower(index.name))
}
}
@@ -869,7 +869,7 @@ func (t *TableState) completeTableDropColumn(columnName string) *WalkThroughErro
}
}
- delete(t.columnSet, columnName)
+ delete(t.columnSet, strings.ToLower(columnName))
return nil
}
@@ -882,11 +882,11 @@ func (t *TableState) incompleteTableDropColumn(columnName string) *WalkThroughEr
index.dropColumn(columnName)
// If all columns that make up an index are dropped, the index is dropped as well.
if len(index.expressionList) == 0 {
- delete(t.indexSet, index.name)
+ delete(t.indexSet, strings.ToLower(index.name))
}
}
- delete(t.columnSet, columnName)
+ delete(t.columnSet, strings.ToLower(columnName))
return nil
}
@@ -903,7 +903,7 @@ func (idx *IndexState) dropColumn(columnName string) {
}
var newKeyList []string
for _, key := range idx.expressionList {
- if key != columnName {
+ if !strings.EqualFold(key, columnName) {
newKeyList = append(newKeyList, key)
}
}
@@ -922,7 +922,7 @@ func (t *TableState) reorderColumn(position *tidbast.ColumnPosition) (int, *Walk
}
return 1, nil
case tidbast.ColumnPositionAfter:
- columnName := position.RelativeColumn.Name.O
+ columnName := position.RelativeColumn.Name.L
column, exist := t.columnSet[columnName]
if !exist {
return 0, NewColumnNotExistsError(t.name, columnName)
@@ -1111,7 +1111,7 @@ func (t *TableState) validateAndGetKeyStringList(ctx *FinderContext, keyList []*
}
res = append(res, str)
} else {
- columnName := key.Column.Name.O
+ columnName := key.Column.Name.L
column, exists := t.columnSet[columnName]
if !exists {
if ctx.CheckIntegrity {
@@ -1170,7 +1170,7 @@ func checkDefault(columnName string, columnType *types.FieldType, value tidbast.
}
func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
- if _, exists := t.columnSet[column.Name.Name.O]; exists {
+ if _, exists := t.columnSet[column.Name.Name.L]; exists {
return &WalkThroughError{
Type: ErrorTypeColumnExists,
Content: fmt.Sprintf("Column `%s` already exists in table `%s`", column.Name.Name.O, t.name),
@@ -1188,7 +1188,7 @@ func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef,
vTrue := true
col := &ColumnState{
- name: column.Name.Name.O,
+ name: column.Name.Name.L,
position: &pos,
defaultValue: nil,
nullable: &vTrue,
@@ -1269,7 +1269,7 @@ func (t *TableState) createColumn(ctx *FinderContext, column *tidbast.ColumnDef,
}
}
- t.columnSet[col.name] = col
+ t.columnSet[strings.ToLower(col.name)] = col
return nil
}
@@ -1281,7 +1281,7 @@ func (t *TableState) createIndex(name string, keyList []string, unique bool, tp
}
}
if name != "" {
- if _, exists := t.indexSet[name]; exists {
+ if _, exists := t.indexSet[strings.ToLower(name)]; exists {
return NewIndexExistsError(t.name, name)
}
} else {
@@ -1291,7 +1291,7 @@ func (t *TableState) createIndex(name string, keyList []string, unique bool, tp
if suffix > 1 {
name = fmt.Sprintf("%s_%d", keyList[0], suffix)
}
- if _, exists := t.indexSet[name]; !exists {
+ if _, exists := t.indexSet[strings.ToLower(name)]; !exists {
break
}
suffix++
@@ -1312,12 +1312,12 @@ func (t *TableState) createIndex(name string, keyList []string, unique bool, tp
index.visible = newFalsePointer()
}
- t.indexSet[name] = index
+ t.indexSet[strings.ToLower(name)] = index
return nil
}
func (t *TableState) createPrimaryKey(keys []string, tp string) *WalkThroughError {
- if _, exists := t.indexSet[PrimaryKeyName]; exists {
+ if _, exists := t.indexSet[strings.ToLower(PrimaryKeyName)]; exists {
return &WalkThroughError{
Type: ErrorTypePrimaryKeyExists,
Content: fmt.Sprintf("Primary key exists in table `%s`", t.name),
@@ -1333,7 +1333,7 @@ func (t *TableState) createPrimaryKey(keys []string, tp string) *WalkThroughErro
visible: newTruePointer(),
comment: newEmptyStringPointer(),
}
- t.indexSet[pk.name] = pk
+ t.indexSet[strings.ToLower(pk.name)] = pk
return nil
}
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
index aa1d78d8b1..435e44e651 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql_v2.go
@@ -573,7 +573,7 @@ func (d *DatabaseState) mysqlTheCurrentDatabase(renamePair mysql.IRenamePairCont
}
func (t *TableState) mysqlChangeIndexVisibility(ctx *FinderContext, indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {
- index, exists := t.indexSet[indexName]
+ index, exists := t.indexSet[strings.ToLower(indexName)]
if !exists {
if ctx.CheckIntegrity {
return NewIndexNotExistsError(t.name, indexName)
@@ -595,7 +595,7 @@ func (t *TableState) mysqlAlterColumn(ctx *FinderContext, itemDef mysql.IAlterLi
return nil
}
columnName := mysqlparser.NormalizeMySQLColumnInternalRef(itemDef.ColumnInternalRef())
- colState, exists := t.columnSet[columnName]
+ colState, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
if ctx.CheckIntegrity {
return NewColumnNotExistsError(t.name, columnName)
@@ -663,7 +663,7 @@ func (t *TableState) mysqlChangeColumn(ctx *FinderContext, oldColumnName string,
// 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, oldColumnName)
+ delete(t.columnSet, strings.ToLower(oldColumnName))
// rename column from indexSet
t.renameColumnInIndexKey(oldColumnName, newColumnName)
@@ -678,7 +678,7 @@ func (t *TableState) mysqlIncompleteTableChangeColumn(ctx *FinderContext, oldCol
// 2. rename column from indexSet.
// 3. create a new column in columnSet.
func (t *TableState) mysqlCompleteTableChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
- column, exists := t.columnSet[oldColumnName]
+ column, exists := t.columnSet[strings.ToLower(oldColumnName)]
if !exists {
return NewColumnNotExistsError(t.name, oldColumnName)
}
@@ -710,7 +710,7 @@ func (t *TableState) mysqlCompleteTableChangeColumn(ctx *FinderContext, oldColum
*col.position--
}
}
- delete(t.columnSet, column.name)
+ delete(t.columnSet, strings.ToLower(column.name))
// rename column from indexSet
t.renameColumnInIndexKey(oldColumnName, newColumnName)
@@ -873,7 +873,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
func (t *TableState) mysqlValidateKeyStringList(ctx *FinderContext, keyList []string, primary bool, isSpatial bool) *WalkThroughError {
for _, columnName := range keyList {
- column, exists := t.columnSet[columnName]
+ column, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
if ctx.CheckIntegrity {
return NewColumnNotExistsError(t.name, columnName)
@@ -930,7 +930,7 @@ func mysqlGetIndexType(tableConstraint mysql.ITableConstraintDefContext) string
}
func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
- if _, exists := t.columnSet[columnName]; exists {
+ if _, exists := t.columnSet[strings.ToLower(columnName)]; exists {
return &WalkThroughError{
Type: ErrorTypeColumnExists,
Content: fmt.Sprintf("Column `%s` already exists in table `%s`", columnName, t.name),
@@ -1022,13 +1022,13 @@ func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fi
// the key attribute for in a column meaning primary key.
col.nullable = newFalsePointer()
// we need to check the key type which generated by tidb parser.
- if err := t.mysqlCreatePrimaryKey([]string{col.name}, "BTREE"); err != nil {
+ if err := t.mysqlCreatePrimaryKey([]string{strings.ToLower(col.name)}, "BTREE"); err != nil {
return err
}
// unique key.
case mysql.MySQLParserUNIQUE_SYMBOL:
// unique index.
- if err := t.mysqlCreateIndex("", []string{col.name}, true /* unique */, "BTREE", mysql.NewEmptyTableConstraintDefContext(), mysql.NewEmptyCreateIndexContext()); err != nil {
+ if err := t.mysqlCreateIndex("", []string{strings.ToLower(col.name)}, true /* unique */, "BTREE", mysql.NewEmptyTableConstraintDefContext(), mysql.NewEmptyCreateIndexContext()); err != nil {
return err
}
// auto_increment.
@@ -1052,7 +1052,7 @@ func (t *TableState) mysqlCreateColumn(ctx *FinderContext, columnName string, fi
}
}
- t.columnSet[col.name] = col
+ t.columnSet[strings.ToLower(col.name)] = col
return nil
}
@@ -1067,7 +1067,7 @@ func (t *TableState) mysqlReorderColumn(position *mysqlColumnPosition) (int, *Wa
}
return 1, nil
case ColumnPositionAfter:
- columnName := position.relativeColumn
+ columnName := strings.ToLower(position.relativeColumn)
column, exist := t.columnSet[columnName]
if !exist {
return 0, NewColumnNotExistsError(t.name, columnName)
@@ -1094,7 +1094,7 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
}
// construct a index name if name is empty.
if name != "" {
- if _, exists := t.indexSet[name]; exists {
+ if _, exists := t.indexSet[strings.ToLower(name)]; exists {
return NewIndexExistsError(t.name, name)
}
} else {
@@ -1104,7 +1104,7 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
if suffix > 1 {
name = fmt.Sprintf("%s_%d", keyList[0], suffix)
}
- if _, exists := t.indexSet[name]; !exists {
+ if _, exists := t.indexSet[strings.ToLower(name)]; !exists {
break
}
suffix++
@@ -1190,12 +1190,12 @@ func (t *TableState) mysqlCreateIndex(name string, keyList []string, unique bool
}
}
- t.indexSet[name] = index
+ t.indexSet[strings.ToLower(name)] = index
return nil
}
func (t *TableState) mysqlCreatePrimaryKey(keys []string, tp string) *WalkThroughError {
- if _, exists := t.indexSet[PrimaryKeyName]; exists {
+ if _, exists := t.indexSet[strings.ToLower(PrimaryKeyName)]; exists {
return &WalkThroughError{
Type: ErrorTypePrimaryKeyExists,
Content: fmt.Sprintf("Primary key exists in table `%s`", t.name),
@@ -1211,7 +1211,7 @@ func (t *TableState) mysqlCreatePrimaryKey(keys []string, tp string) *WalkThroug
visible: newTruePointer(),
comment: newEmptyStringPointer(),
}
- t.indexSet[pk.name] = pk
+ t.indexSet[strings.ToLower(pk.name)] = pk
return nil
}