Skip to content

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

refactor: remove FinderContext and simplify catalog state (#18102)

Danny Xu3135b1b650a1
diff --git a/backend/plugin/advisor/catalog/catalog.go b/backend/plugin/advisor/catalog/catalog.go
index 9590489644..33a1daabe6 100644
--- a/backend/plugin/advisor/catalog/catalog.go
+++ b/backend/plugin/advisor/catalog/catalog.go
@@ -24,8 +24,8 @@ func NewCatalog(ctx context.Context, s *store.Store, instanceID, databaseName st
}
dbMetadata = databaseMeta.GetMetadata()
}
- finderCtx := &FinderContext{EngineType: engineType, IgnoreCaseSensitive: !isCaseSensitive}
- origin = NewDatabaseState(dbMetadata, finderCtx)
- final = NewDatabaseState(dbMetadata, finderCtx)
+ ignoreCaseSensitive := !isCaseSensitive
+ origin = NewDatabaseState(dbMetadata, ignoreCaseSensitive, engineType)
+ final = NewDatabaseState(dbMetadata, ignoreCaseSensitive, engineType)
return origin, final, nil
}
diff --git a/backend/plugin/advisor/catalog/finder.go b/backend/plugin/advisor/catalog/finder.go
deleted file mode 100644
index 85d80da473..0000000000
--- a/backend/plugin/advisor/catalog/finder.go
+++ /dev/null
@@ -1,18 +0,0 @@
-package catalog
-
-import (
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
-)
-
-// FinderContext is the context for finder.
-type FinderContext struct {
- // EngineType is the engine type for database engine.
- EngineType storepb.Engine
-
- // Ignore case sensitive is the policy for identifier name comparison case-sensitive.
- // It has different behavior for different database engine.
- // MySQL:
- // This controls the following identifier comparisons:
- // Database, Table
- IgnoreCaseSensitive bool
-}
diff --git a/backend/plugin/advisor/catalog/state.go b/backend/plugin/advisor/catalog/state.go
index 89e67494bd..f0faff6fc0 100644
--- a/backend/plugin/advisor/catalog/state.go
+++ b/backend/plugin/advisor/catalog/state.go
@@ -12,19 +12,18 @@ import (
)
// NewDatabaseState creates a new database state from schema metadata and context.
-func NewDatabaseState(d *storepb.DatabaseSchemaMetadata, context *FinderContext) *DatabaseState {
+func NewDatabaseState(d *storepb.DatabaseSchemaMetadata, ignoreCaseSensitive bool, engineType storepb.Engine) *DatabaseState {
database := &DatabaseState{
- ctx: context,
- name: d.Name,
- characterSet: d.CharacterSet,
- collation: d.Collation,
- dbType: context.EngineType,
- schemaSet: make(schemaStateMap),
- usable: true,
+ ignoreCaseSensitive: ignoreCaseSensitive,
+ name: d.Name,
+ characterSet: d.CharacterSet,
+ collation: d.Collation,
+ dbType: engineType,
+ schemaSet: make(schemaStateMap),
}
for _, schema := range d.Schemas {
- database.schemaSet[schema.Name] = newSchemaState(schema, database.ctx)
+ database.schemaSet[schema.Name] = newSchemaState(schema, database.ignoreCaseSensitive, database.dbType)
}
for _, schema := range d.Schemas {
@@ -45,17 +44,17 @@ func NewDatabaseState(d *storepb.DatabaseSchemaMetadata, context *FinderContext)
return database
}
-func newSchemaState(s *storepb.SchemaMetadata, context *FinderContext) *SchemaState {
+func newSchemaState(s *storepb.SchemaMetadata, ignoreCaseSensitive bool, engineType storepb.Engine) *SchemaState {
schema := &SchemaState{
- ctx: context,
- name: s.Name,
- tableSet: make(tableStateMap),
- viewSet: make(viewStateMap),
- identifierMap: make(identifierMap),
+ ignoreCaseSensitive: ignoreCaseSensitive,
+ name: s.Name,
+ tableSet: make(tableStateMap),
+ viewSet: make(viewStateMap),
+ identifierMap: make(identifierMap),
}
for _, table := range s.Tables {
- tableState := newTableState(table, context)
+ tableState := newTableState(table, engineType)
schema.tableSet[table.Name] = tableState
schema.identifierMap[table.Name] = true
@@ -81,7 +80,7 @@ func newViewState(v *storepb.ViewMetadata) *ViewState {
}
}
-func newTableState(t *storepb.TableMetadata, context *FinderContext) *TableState {
+func newTableState(t *storepb.TableMetadata, engineType storepb.Engine) *TableState {
table := &TableState{
name: t.Name,
engine: newStringPointer(t.Engine),
@@ -94,7 +93,7 @@ func newTableState(t *storepb.TableMetadata, context *FinderContext) *TableState
for i, column := range t.Columns {
columnName := column.Name
- switch context.EngineType {
+ switch engineType {
case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:
columnName = strings.ToLower(columnName)
default:
@@ -105,7 +104,7 @@ func newTableState(t *storepb.TableMetadata, context *FinderContext) *TableState
for _, index := range t.Indexes {
indexName := index.Name
- switch context.EngineType {
+ switch engineType {
case storepb.Engine_MYSQL, storepb.Engine_TIDB, storepb.Engine_MARIADB, storepb.Engine_OCEANBASE:
indexName = strings.ToLower(indexName)
default:
@@ -153,14 +152,13 @@ func newIndexState(i *storepb.IndexMetadata) *IndexState {
// DatabaseState is the state for walk-through.
type DatabaseState struct {
- ctx *FinderContext
- name string
- characterSet string
- collation string
- dbType storepb.Engine
- schemaSet schemaStateMap
- deleted bool
- usable bool
+ ignoreCaseSensitive bool
+ name string
+ characterSet string
+ collation string
+ dbType storepb.Engine
+ schemaSet schemaStateMap
+ deleted bool
}
// Index returns the index map of the table.
@@ -172,11 +170,6 @@ func (d *DatabaseState) Index(schemaName string, tableName string) *IndexStateMa
return schema.Index(tableName)
}
-// Usable returns the usable of the database state.
-func (d *DatabaseState) Usable() bool {
- return d.usable
-}
-
// HasNoTable returns true if the current database has no table.
func (d *DatabaseState) HasNoTable() bool {
for _, schema := range d.schemaSet {
@@ -251,7 +244,7 @@ func (d *DatabaseState) GetPrimaryKey(schemaName string, tableName string) *Inde
continue
}
for _, table := range schema.tableSet {
- if !compareIdentifier(table.name, tableName, schema.ctx.IgnoreCaseSensitive) {
+ if !compareIdentifier(table.name, tableName, schema.ignoreCaseSensitive) {
continue
}
for _, index := range table.indexSet {
@@ -328,10 +321,10 @@ type identifierMap map[string]bool
// SchemaState is the state for walk-through.
type SchemaState struct {
- ctx *FinderContext
- name string
- tableSet tableStateMap
- viewSet viewStateMap
+ ignoreCaseSensitive bool
+ name string
+ tableSet tableStateMap
+ viewSet viewStateMap
// PostgreSQL specific fields
// All relation names in PostgreSQL must be distinct in schema level.
@@ -599,11 +592,11 @@ func newBoolPointer(v bool) *bool {
// createSchemaState creates a new schema state with the given name.
func (d *DatabaseState) createSchemaState(schemaName string) *SchemaState {
schema := &SchemaState{
- ctx: d.ctx,
- name: schemaName,
- tableSet: make(tableStateMap),
- viewSet: make(viewStateMap),
- identifierMap: make(identifierMap),
+ ignoreCaseSensitive: d.ignoreCaseSensitive,
+ name: schemaName,
+ tableSet: make(tableStateMap),
+ viewSet: make(viewStateMap),
+ identifierMap: make(identifierMap),
}
d.schemaSet[schemaName] = schema
return schema
diff --git a/backend/plugin/advisor/catalog/walk_through.go b/backend/plugin/advisor/catalog/walk_through.go
index 425de41072..4904bba91d 100644
--- a/backend/plugin/advisor/catalog/walk_through.go
+++ b/backend/plugin/advisor/catalog/walk_through.go
@@ -205,7 +205,7 @@ func compareIdentifier(a, b string, ignoreCaseSensitive bool) bool {
// isCurrentDatabase returns true if the given database is the current database of the state.
func (d *DatabaseState) isCurrentDatabase(database string) bool {
- return compareIdentifier(d.name, database, d.ctx.IgnoreCaseSensitive)
+ return compareIdentifier(d.name, database, d.ignoreCaseSensitive)
}
// getTable returns the table with the given name if it exists in the schema.
@@ -214,7 +214,7 @@ func (d *DatabaseState) isCurrentDatabase(database string) bool {
//nolint:revive
func (s *SchemaState) getTable(table string) (*TableState, bool) {
for k, v := range s.tableSet {
- if compareIdentifier(k, table, s.ctx.IgnoreCaseSensitive) {
+ if compareIdentifier(k, table, s.ignoreCaseSensitive) {
return v, true
}
}
@@ -223,7 +223,7 @@ func (s *SchemaState) getTable(table string) (*TableState, bool) {
}
//nolint:revive
-func (s *SchemaState) renameTable(_ *FinderContext, oldName string, newName string) *WalkThroughError {
+func (s *SchemaState) renameTable(oldName string, newName string) *WalkThroughError {
if oldName == newName {
return nil
}
@@ -251,10 +251,10 @@ func (s *SchemaState) renameTable(_ *FinderContext, oldName string, newName stri
func (d *DatabaseState) createSchema() *SchemaState {
schema := &SchemaState{
- ctx: d.ctx,
- name: "",
- tableSet: make(tableStateMap),
- viewSet: make(viewStateMap),
+ ignoreCaseSensitive: d.ignoreCaseSensitive,
+ name: "",
+ tableSet: make(tableStateMap),
+ viewSet: make(viewStateMap),
}
d.schemaSet[""] = schema
@@ -328,11 +328,11 @@ func (d *DatabaseState) getSchema(schemaName string) (*SchemaState, *WalkThrough
}
}
schema = &SchemaState{
- ctx: d.ctx,
- name: publicSchemaName,
- tableSet: make(tableStateMap),
- viewSet: make(viewStateMap),
- identifierMap: make(identifierMap),
+ ignoreCaseSensitive: d.ignoreCaseSensitive,
+ name: publicSchemaName,
+ tableSet: make(tableStateMap),
+ viewSet: make(viewStateMap),
+ identifierMap: make(identifierMap),
}
d.schemaSet[publicSchemaName] = schema
}
diff --git a/backend/plugin/advisor/catalog/walk_through_for_mysql.go b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
index 72b5f35a15..164750caa8 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_mysql.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_mysql.go
@@ -151,13 +151,13 @@ func (l *mysqlListener) EnterCreateTable(ctx *mysql.CreateTableContext) {
hasAutoIncrement = true
}
_, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- if err := table.mysqlCreateColumn(l.databaseState.ctx, columnName, tableElement.ColumnDefinition().FieldDefinition(), nil /* position */); err != nil {
+ if err := table.mysqlCreateColumn(columnName, tableElement.ColumnDefinition().FieldDefinition(), nil /* position */); err != nil {
err.Line = l.baseLine + tableElement.GetStart().GetLine()
l.err = err
return
}
case tableElement.TableConstraintDef() != nil:
- if err := table.mysqlCreateConstraint(l.databaseState.ctx, tableElement.TableConstraintDef()); err != nil {
+ if err := table.mysqlCreateConstraint(tableElement.TableConstraintDef()); err != nil {
err.Line = tableElement.GetStart().GetLine()
l.err = err
return
@@ -270,7 +270,7 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {
// add single column.
case item.Identifier() != nil && item.FieldDefinition() != nil:
columnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- if err := table.mysqlCreateColumn(l.databaseState.ctx, columnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); err != nil {
+ if err := table.mysqlCreateColumn(columnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); err != nil {
l.err = err
return
}
@@ -281,14 +281,14 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {
continue
}
_, _, columnName := mysqlparser.NormalizeMySQLColumnName(tableElement.ColumnDefinition().ColumnName())
- if err := table.mysqlCreateColumn(l.databaseState.ctx, columnName, tableElement.ColumnDefinition().FieldDefinition(), nil); err != nil {
+ if err := table.mysqlCreateColumn(columnName, tableElement.ColumnDefinition().FieldDefinition(), nil); err != nil {
l.err = err
return
}
}
// add constraint.
case item.TableConstraintDef() != nil:
- if err := table.mysqlCreateConstraint(l.databaseState.ctx, item.TableConstraintDef()); err != nil {
+ if err := table.mysqlCreateConstraint(item.TableConstraintDef()); err != nil {
l.err = err
return
}
@@ -327,7 +327,7 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {
// modify column.
case item.MODIFY_SYMBOL() != nil && item.ColumnInternalRef() != nil:
columnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
- if err := table.mysqlChangeColumn(l.databaseState.ctx, columnName, columnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); err != nil {
+ if err := table.mysqlChangeColumn(columnName, columnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); err != nil {
l.err = err
return
}
@@ -335,7 +335,7 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {
case item.CHANGE_SYMBOL() != nil && item.ColumnInternalRef() != nil && item.Identifier() != nil:
oldColumnName := mysqlparser.NormalizeMySQLColumnInternalRef(item.ColumnInternalRef())
newColumnName := mysqlparser.NormalizeMySQLIdentifier(item.Identifier())
- if err := table.mysqlChangeColumn(l.databaseState.ctx, oldColumnName, newColumnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); err != nil {
+ if err := table.mysqlChangeColumn(oldColumnName, newColumnName, item.FieldDefinition(), positionFromPlaceContext(item.Place())); err != nil {
l.err = err
return
}
@@ -351,14 +351,14 @@ func (l *mysqlListener) EnterAlterTable(ctx *mysql.AlterTableContext) {
switch {
// alter column.
case item.ColumnInternalRef() != nil:
- if err := table.mysqlAlterColumn(l.databaseState.ctx, item); err != nil {
+ if err := table.mysqlAlterColumn(item); err != nil {
l.err = err
return
}
// alter index visibility.
case item.INDEX_SYMBOL() != nil && item.IndexRef() != nil && item.Visibility() != nil:
_, _, indexName := mysqlparser.NormalizeIndexRef(item.IndexRef())
- if err := table.mysqlChangeIndexVisibility(l.databaseState.ctx, indexName, item.Visibility()); err != nil {
+ if err := table.mysqlChangeIndexVisibility(indexName, item.Visibility()); err != nil {
l.err = err
return
}
@@ -457,7 +457,7 @@ func (l *mysqlListener) EnterCreateIndex(ctx *mysql.CreateIndexContext) {
if ctx.CreateIndexTarget() == nil || ctx.CreateIndexTarget().KeyListVariants() == nil {
return
}
- if err := table.mysqlValidateKeyListVariants(l.databaseState.ctx, ctx.CreateIndexTarget().KeyListVariants(), false /* primary */, isSpatial); err != nil {
+ if err := table.mysqlValidateKeyListVariants(ctx.CreateIndexTarget().KeyListVariants(), false /* primary */, isSpatial); err != nil {
l.err = err
return
}
@@ -542,7 +542,7 @@ func (l *mysqlListener) EnterRenameTableStatement(ctx *mysql.RenameTableStatemen
_, newTableName := mysqlparser.NormalizeMySQLTableName(pair.TableName())
if l.databaseState.mysqlTheCurrentDatabase(pair) {
- if compareIdentifier(oldTableName, newTableName, l.databaseState.ctx.IgnoreCaseSensitive) {
+ if compareIdentifier(oldTableName, newTableName, l.databaseState.ignoreCaseSensitive) {
return
}
table, exists := schema.getTable(oldTableName)
@@ -641,7 +641,7 @@ func (d *DatabaseState) mysqlTheCurrentDatabase(renamePair mysql.IRenamePairCont
return true
}
-func (t *TableState) mysqlChangeIndexVisibility(_ *FinderContext, indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {
+func (t *TableState) mysqlChangeIndexVisibility(indexName string, visibility mysql.IVisibilityContext) *WalkThroughError {
index, exists := t.indexSet[strings.ToLower(indexName)]
if !exists {
return NewIndexNotExistsError(t.name, indexName)
@@ -657,7 +657,7 @@ func (t *TableState) mysqlChangeIndexVisibility(_ *FinderContext, indexName stri
return nil
}
-func (t *TableState) mysqlAlterColumn(_ *FinderContext, itemDef mysql.IAlterListItemContext) *WalkThroughError {
+func (t *TableState) mysqlAlterColumn(itemDef mysql.IAlterListItemContext) *WalkThroughError {
if itemDef.ColumnInternalRef() == nil {
// should not reach here.
return nil
@@ -724,8 +724,8 @@ func (t *TableState) mysqlAlterColumn(_ *FinderContext, itemDef mysql.IAlterList
return nil
}
-func (t *TableState) mysqlChangeColumn(ctx *FinderContext, oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
- return t.mysqlCompleteTableChangeColumn(ctx, oldColumnName, newColumnName, fieldDef, position)
+func (t *TableState) mysqlChangeColumn(oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
+ return t.mysqlCompleteTableChangeColumn(oldColumnName, newColumnName, fieldDef, position)
}
// mysqlCompleteTableChangeColumn changes column definition.
@@ -733,7 +733,7 @@ func (t *TableState) mysqlChangeColumn(ctx *FinderContext, oldColumnName string,
// 1. drop column from tableState.columnSet, but do not drop column from indexSet.
// 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 {
+func (t *TableState) mysqlCompleteTableChangeColumn(oldColumnName string, newColumnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
column, exists := t.columnSet[strings.ToLower(oldColumnName)]
if !exists {
return NewColumnNotExistsError(t.name, oldColumnName)
@@ -772,7 +772,7 @@ func (t *TableState) mysqlCompleteTableChangeColumn(ctx *FinderContext, oldColum
t.renameColumnInIndexKey(oldColumnName, newColumnName)
// create a new column in columnSet
- return t.mysqlCreateColumn(ctx, newColumnName, fieldDef, position)
+ return t.mysqlCreateColumn(newColumnName, fieldDef, position)
}
type columnPositionType int
@@ -837,7 +837,7 @@ func (d *DatabaseState) mysqlFindTableState(databaseName, tableName string) (*Ta
return table, nil
}
-func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mysql.ITableConstraintDefContext) *WalkThroughError {
+func (t *TableState) mysqlCreateConstraint(constraintDef mysql.ITableConstraintDefContext) *WalkThroughError {
if constraintDef.GetType_() != nil {
switch constraintDef.GetType_().GetTokenType() {
// PRIMARY KEY.
@@ -846,7 +846,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
// never reach here.
return nil
}
- if err := t.mysqlValidateKeyListVariants(ctx, constraintDef.KeyListVariants(), true /* primary */, false /* isSpatial*/); err != nil {
+ if err := t.mysqlValidateKeyListVariants(constraintDef.KeyListVariants(), true /* primary */, false /* isSpatial*/); err != nil {
return err
}
keyList := mysqlparser.NormalizeKeyListVariants(constraintDef.KeyListVariants())
@@ -859,7 +859,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
// never reach here.
return nil
}
- if err := t.mysqlValidateKeyListVariants(ctx, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial */); err != nil {
+ if err := t.mysqlValidateKeyListVariants(constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial */); err != nil {
return err
}
@@ -877,7 +877,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
// never reach here.
return nil
}
- if err := t.mysqlValidateKeyListVariants(ctx, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial*/); err != nil {
+ if err := t.mysqlValidateKeyListVariants(constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial*/); err != nil {
return err
}
@@ -898,7 +898,7 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
// never reach here.
return nil
}
- if err := t.mysqlValidateKeyListVariants(ctx, constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial*/); err != nil {
+ if err := t.mysqlValidateKeyListVariants(constraintDef.KeyListVariants(), false /* primary */, false /* isSpatial*/); err != nil {
return err
}
indexName := ""
@@ -922,23 +922,23 @@ func (t *TableState) mysqlCreateConstraint(ctx *FinderContext, constraintDef mys
}
// mysqlValidateKeyListVariants validates the key list variants.
-func (t *TableState) mysqlValidateKeyListVariants(ctx *FinderContext, keyList mysql.IKeyListVariantsContext, primary bool, isSpatial bool) *WalkThroughError {
+func (t *TableState) mysqlValidateKeyListVariants(keyList mysql.IKeyListVariantsContext, primary bool, isSpatial bool) *WalkThroughError {
if keyList.KeyList() != nil {
columns := mysqlparser.NormalizeKeyList(keyList.KeyList())
- if err := t.mysqlValidateColumnList(ctx, columns, primary, isSpatial); err != nil {
+ if err := t.mysqlValidateColumnList(columns, primary, isSpatial); err != nil {
return err
}
}
if keyList.KeyListWithExpression() != nil {
expressions := mysqlparser.NormalizeKeyListWithExpression(keyList.KeyListWithExpression())
- if err := t.mysqlValidateExpressionList(ctx, expressions, primary, isSpatial); err != nil {
+ if err := t.mysqlValidateExpressionList(expressions, primary, isSpatial); err != nil {
return err
}
}
return nil
}
-func (t *TableState) mysqlValidateColumnList(_ *FinderContext, columnList []string, primary bool, isSpatial bool) *WalkThroughError {
+func (t *TableState) mysqlValidateColumnList(columnList []string, primary bool, isSpatial bool) *WalkThroughError {
for _, columnName := range columnList {
column, exists := t.columnSet[strings.ToLower(columnName)]
if !exists {
@@ -960,7 +960,7 @@ func (t *TableState) mysqlValidateColumnList(_ *FinderContext, columnList []stri
// mysqlValidateExpressionList validates the expression list.
// TODO: update expression validation.
-func (t *TableState) mysqlValidateExpressionList(_ *FinderContext, expressionList []string, primary bool, isSpatial bool) *WalkThroughError {
+func (t *TableState) mysqlValidateExpressionList(expressionList []string, primary bool, isSpatial bool) *WalkThroughError {
for _, expression := range expressionList {
column, exists := t.columnSet[strings.ToLower(expression)]
// If expression is not a column, we do not need to validate it.
@@ -1020,7 +1020,7 @@ func mysqlGetIndexType(tableConstraint mysql.ITableConstraintDefContext) string
return "BTREE"
}
-func (t *TableState) mysqlCreateColumn(_ *FinderContext, columnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
+func (t *TableState) mysqlCreateColumn(columnName string, fieldDef mysql.IFieldDefinitionContext, position *mysqlColumnPosition) *WalkThroughError {
if _, exists := t.columnSet[strings.ToLower(columnName)]; exists {
return &WalkThroughError{
Type: ErrorTypeColumnExists,
diff --git a/backend/plugin/advisor/catalog/walk_through_for_tidb.go b/backend/plugin/advisor/catalog/walk_through_for_tidb.go
index b17cd9b647..785adbc177 100644
--- a/backend/plugin/advisor/catalog/walk_through_for_tidb.go
+++ b/backend/plugin/advisor/catalog/walk_through_for_tidb.go
@@ -80,7 +80,7 @@ func (d *DatabaseState) renameTable(node *tidbast.RenameTableStmt) *WalkThroughE
oldTableName := tableToTable.OldTable.Name.O
newTableName := tableToTable.NewTable.Name.O
if d.theCurrentDatabase(tableToTable) {
- if compareIdentifier(oldTableName, newTableName, d.ctx.IgnoreCaseSensitive) {
+ if compareIdentifier(oldTableName, newTableName, d.ignoreCaseSensitive) {
return nil
}
table, exists := schema.getTable(oldTableName)
@@ -207,7 +207,7 @@ func (d *DatabaseState) createIndex(node *tidbast.CreateIndexStmt) *WalkThroughE
// Other index key types
}
- keyList, err := table.validateAndGetKeyStringList(d.ctx, node.IndexPartSpecifications, false /* primary */, isSpatial)
+ keyList, err := table.validateAndGetKeyStringList(node.IndexPartSpecifications, false /* primary */, isSpatial)
if err != nil {
return err
}
@@ -242,18 +242,18 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
if len(spec.NewColumns) == 1 {
pos = spec.Position
}
- if err := table.createColumn(d.ctx, column, pos); err != nil {
+ if err := table.createColumn(column, pos); err != nil {
return err
}
}
// MySQL can add table constraints in ALTER TABLE ADD COLUMN statements.
for _, constraint := range spec.NewConstraints {
- if err := table.createConstraint(d.ctx, constraint); err != nil {
+ if err := table.createConstraint(constraint); err != nil {
return err
}
}
case tidbast.AlterTableAddConstraint:
- if err := table.createConstraint(d.ctx, spec.Constraint); err != nil {
+ if err := table.createConstraint(spec.Constraint); err != nil {
return err
}
case tidbast.AlterTableDropColumn:
@@ -271,11 +271,11 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
case tidbast.AlterTableDropForeignKey:
// we do not deal with DROP FOREIGN KEY statements.
case tidbast.AlterTableModifyColumn:
- if err := table.changeColumn(d.ctx, spec.NewColumns[0].Name.Name.O, spec.NewColumns[0], spec.Position); err != nil {
+ if err := table.changeColumn(spec.NewColumns[0].Name.Name.O, spec.NewColumns[0], spec.Position); err != nil {
return err
}
case tidbast.AlterTableChangeColumn:
- if err := table.changeColumn(d.ctx, spec.OldColumnName.Name.O, spec.NewColumns[0], spec.Position); err != nil {
+ if err := table.changeColumn(spec.OldColumnName.Name.O, spec.NewColumns[0], spec.Position); err != nil {
return err
}
case tidbast.AlterTableRenameColumn:
@@ -284,11 +284,11 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
}
case tidbast.AlterTableRenameTable:
schema := d.schemaSet[""]
- if err := schema.renameTable(d.ctx, table.name, spec.NewTable.Name.O); err != nil {
+ if err := schema.renameTable(table.name, spec.NewTable.Name.O); err != nil {
return err
}
case tidbast.AlterTableAlterColumn:
- if err := table.changeColumnDefault(d.ctx, spec.NewColumns[0]); err != nil {
+ if err := table.changeColumnDefault(spec.NewColumns[0]); err != nil {
return err
}
case tidbast.AlterTableRenameIndex:
@@ -296,7 +296,7 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
return err
}
case tidbast.AlterTableIndexInvisible:
- if err := table.changeIndexVisibility(d.ctx, spec.IndexName.O, spec.Visibility); err != nil {
+ if err := table.changeIndexVisibility(spec.IndexName.O, spec.Visibility); err != nil {
return err
}
default:
@@ -307,7 +307,7 @@ func (d *DatabaseState) alterTable(node *tidbast.AlterTableStmt) *WalkThroughErr
return nil
}
-func (t *TableState) changeIndexVisibility(_ *FinderContext, indexName string, visibility tidbast.IndexVisibility) *WalkThroughError {
+func (t *TableState) changeIndexVisibility(indexName string, visibility tidbast.IndexVisibility) *WalkThroughError {
index, exists := t.indexSet[strings.ToLower(indexName)]
if !exists {
return NewIndexNotExistsError(t.name, indexName)
@@ -323,7 +323,7 @@ func (t *TableState) changeIndexVisibility(_ *FinderContext, indexName string, v
return nil
}
-func (t *TableState) changeColumnDefault(_ *FinderContext, column *tidbast.ColumnDef) *WalkThroughError {
+func (t *TableState) changeColumnDefault(column *tidbast.ColumnDef) *WalkThroughError {
columnName := column.Name.Name.L
colState, exists := t.columnSet[columnName]
if !exists {
@@ -389,7 +389,7 @@ func (t *TableState) renameColumnInIndexKey(oldName string, newName string) {
// 1. drop column from tableState.columnSet, but do not drop column from indexSet.
// 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 {
+func (t *TableState) completeTableChangeColumn(oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
column, exists := t.columnSet[strings.ToLower(oldName)]
if !exists {
return NewColumnNotExistsError(t.name, oldName)
@@ -437,11 +437,11 @@ func (t *TableState) completeTableChangeColumn(ctx *FinderContext, oldName strin
t.renameColumnInIndexKey(oldName, newColumn.Name.Name.O)
// create a new column in columnSet
- return t.createColumn(ctx, newColumn, position)
+ return t.createColumn(newColumn, position)
}
-func (t *TableState) changeColumn(ctx *FinderContext, oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
- return t.completeTableChangeColumn(ctx, oldName, newColumn, position)
+func (t *TableState) changeColumn(oldName string, newColumn *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
+ return t.completeTableChangeColumn(oldName, newColumn, position)
}
// reorderColumn reorders the columns for new column and returns the new column position.
@@ -581,14 +581,14 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughE
}
hasAutoIncrement = true
}
- if err := table.createColumn(d.ctx, column, nil /* position */); err != nil {
+ if err := table.createColumn(column, nil /* position */); err != nil {
err.Line = column.OriginTextPosition()
return err
}
}
for _, constraint := range node.Constraints {
- if err := table.createConstraint(d.ctx, constraint); err != nil {
+ if err := table.createConstraint(constraint); err != nil {
err.Line = constraint.OriginTextPosition()
return err
}
@@ -597,10 +597,10 @@ func (d *DatabaseState) createTable(node *tidbast.CreateTableStmt) *WalkThroughE
return nil
}
-func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Constraint) *WalkThroughError {
+func (t *TableState) createConstraint(constraint *tidbast.Constraint) *WalkThroughError {
switch constraint.Tp {
case tidbast.ConstraintPrimaryKey:
- keyList, err := t.validateAndGetKeyStringList(ctx, constraint.Keys, true /* primary */, false /* isSpatial */)
+ keyList, err := t.validateAndGetKeyStringList(constraint.Keys, true /* primary */, false /* isSpatial */)
if err != nil {
return err
}
@@ -608,7 +608,7 @@ func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Co
return err
}
case tidbast.ConstraintKey, tidbast.ConstraintIndex:
- keyList, err := t.validateAndGetKeyStringList(ctx, constraint.Keys, false /* primary */, false /* isSpatial */)
+ keyList, err := t.validateAndGetKeyStringList(constraint.Keys, false /* primary */, false /* isSpatial */)
if err != nil {
return err
}
@@ -616,7 +616,7 @@ func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Co
return err
}
case tidbast.ConstraintUniq, tidbast.ConstraintUniqKey, tidbast.ConstraintUniqIndex:
- keyList, err := t.validateAndGetKeyStringList(ctx, constraint.Keys, false /* primary */, false /* isSpatial */)
+ keyList, err := t.validateAndGetKeyStringList(constraint.Keys, false /* primary */, false /* isSpatial */)
if err != nil {
return err
}
@@ -626,7 +626,7 @@ func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Co
case tidbast.ConstraintForeignKey:
// we do not deal with FOREIGN KEY constraints
case tidbast.ConstraintFulltext:
- keyList, err := t.validateAndGetKeyStringList(ctx, constraint.Keys, false /* primary */, false /* isSpatial */)
+ keyList, err := t.validateAndGetKeyStringList(constraint.Keys, false /* primary */, false /* isSpatial */)
if err != nil {
return err
}
@@ -642,7 +642,7 @@ func (t *TableState) createConstraint(ctx *FinderContext, constraint *tidbast.Co
return nil
}
-func (t *TableState) validateAndGetKeyStringList(_ *FinderContext, keyList []*tidbast.IndexPartSpecification, primary bool, isSpatial bool) ([]string, *WalkThroughError) {
+func (t *TableState) validateAndGetKeyStringList(keyList []*tidbast.IndexPartSpecification, primary bool, isSpatial bool) ([]string, *WalkThroughError) {
var res []string
for _, key := range keyList {
if key.Expr != nil {
@@ -710,7 +710,7 @@ func checkDefault(columnName string, columnType *types.FieldType, value tidbast.
}
//nolint:revive
-func (t *TableState) createColumn(_ *FinderContext, column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
+func (t *TableState) createColumn(column *tidbast.ColumnDef, position *tidbast.ColumnPosition) *WalkThroughError {
if _, exists := t.columnSet[column.Name.Name.L]; exists {
return &WalkThroughError{
Type: ErrorTypeColumnExists,
diff --git a/backend/plugin/advisor/catalog/walk_through_test.go b/backend/plugin/advisor/catalog/walk_through_test.go
index 324d178f6c..5ded0dfa75 100644
--- a/backend/plugin/advisor/catalog/walk_through_test.go
+++ b/backend/plugin/advisor/catalog/walk_through_test.go
@@ -186,9 +186,9 @@ func runWalkThroughTest(t *testing.T, file string, engineType storepb.Engine, or
for _, test := range tests {
var state *DatabaseState
if originDatabase != nil {
- state = NewDatabaseState(originDatabase, &FinderContext{EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
+ state = NewDatabaseState(originDatabase, test.IgnoreCaseSensitive, engineType)
} else {
- state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, &FinderContext{EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
+ state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, test.IgnoreCaseSensitive, engineType)
}
asts, _ := sm.GetASTsForChecks(engineType, test.Statement)
@@ -235,9 +235,9 @@ func runANTLRWalkThroughTest(t *testing.T, file string, engineType storepb.Engin
for _, test := range tests {
var state *DatabaseState
if originDatabase != nil {
- state = NewDatabaseState(originDatabase, &FinderContext{EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
+ state = NewDatabaseState(originDatabase, test.IgnoreCaseSensitive, engineType)
} else {
- state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, &FinderContext{EngineType: engineType, IgnoreCaseSensitive: test.IgnoreCaseSensitive})
+ state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, test.IgnoreCaseSensitive, engineType)
}
// Parse using ANTLR parser instead of legacy parser
diff --git a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go b/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
index 5024a6fdfb..f23962c2d3 100644
--- a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
+++ b/backend/plugin/advisor/pg/advisor_index_total_number_limit.go
@@ -49,14 +49,10 @@ func (*IndexTotalNumberLimitAdvisor) Check(_ context.Context, checkCtx advisor.C
tableLine: make(tableLineMap),
}
- if rule.finalCatalog.Usable() {
- checker := NewGenericChecker([]Rule{rule})
- antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
- rule.generateAdvice()
- return checker.GetAdviceList(), nil
- }
-
- return nil, nil
+ checker := NewGenericChecker([]Rule{rule})
+ antlr.ParseTreeWalkerDefault.Walk(checker, tree.Tree)
+ rule.generateAdvice()
+ return checker.GetAdviceList(), nil
}
type tableLine struct {
diff --git a/backend/plugin/advisor/pg/test_helper.go b/backend/plugin/advisor/pg/test_helper.go
index 916616b21a..359893bbe3 100644
--- a/backend/plugin/advisor/pg/test_helper.go
+++ b/backend/plugin/advisor/pg/test_helper.go
@@ -59,9 +59,8 @@ func RunANTLRAdvisorRuleTest(t *testing.T, rule advisor.SQLReviewRuleType, dbTyp
}
database := advisor.MockPostgreSQLDatabase
- ctx := &catalog.FinderContext{EngineType: dbType}
- originCatalog := catalog.NewDatabaseState(database, ctx)
- finalCatalog := catalog.NewDatabaseState(database, ctx)
+ originCatalog := catalog.NewDatabaseState(database, false /* ignoreCaseSensitive */, dbType)
+ finalCatalog := catalog.NewDatabaseState(database, false /* ignoreCaseSensitive */, dbType)
// Get default payload, or use empty string for test-only rules
payload, err := advisor.SetDefaultSQLReviewRulePayload(rule, dbType)
diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.go
index 312795e750..53d711f75a 100644
--- a/backend/plugin/advisor/utils_for_tests.go
+++ b/backend/plugin/advisor/utils_for_tests.go
@@ -200,9 +200,8 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E
if dbType == storepb.Engine_POSTGRES {
database = MockPostgreSQLDatabase
}
- ctx := &catalog.FinderContext{EngineType: dbType}
- originCatalog := catalog.NewDatabaseState(database, ctx)
- finalCatalog := catalog.NewDatabaseState(database, ctx)
+ originCatalog := catalog.NewDatabaseState(database, false /* ignoreCaseSensitive */, dbType)
+ finalCatalog := catalog.NewDatabaseState(database, false /* ignoreCaseSensitive */, dbType)
payload, err := SetDefaultSQLReviewRulePayload(rule, dbType)
require.NoError(t, err)