Skip to content

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

refactor: use hierarchical access pattern for DatabaseMetadata (#18115)

Danny Xud464678e3c04
diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go b/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go
index 594579aa8b..3b3fbdf24f 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go
@@ -63,18 +63,18 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(_ context.Context, checkCtx advi
// ColumnDisallowChangingTypeRule checks for disallow changing column type.
type ColumnDisallowChangingTypeRule struct {
BaseRule
- text string
- originCatalog *model.DatabaseMetadata
+ text string
+ originalMetadata *model.DatabaseMetadata
}
// NewColumnDisallowChangingTypeRule creates a new ColumnDisallowChangingTypeRule.
-func NewColumnDisallowChangingTypeRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *ColumnDisallowChangingTypeRule {
+func NewColumnDisallowChangingTypeRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *ColumnDisallowChangingTypeRule {
return &ColumnDisallowChangingTypeRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- originCatalog: originCatalog,
+ originalMetadata: originalMetadata,
}
}
@@ -170,8 +170,7 @@ func normalizeColumnType(tp string) string {
func (r *ColumnDisallowChangingTypeRule) changeColumnType(tableName, columnName string, dataType mysql.IDataTypeContext) {
tp := dataType.GetParser().GetTokenStream().GetTextFromRuleContext(dataType)
- column := r.originCatalog.GetColumn("", tableName, columnName)
-
+ column := r.originalMetadata.GetSchema("").GetTable(tableName).GetColumn(columnName)
if column == nil {
return
}
diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_drop_in_index.go b/backend/plugin/advisor/mysql/rule_column_disallow_drop_in_index.go
index 70d78a3aef..80e4a73da7 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_drop_in_index.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_drop_in_index.go
@@ -61,19 +61,19 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(_ context.Context, checkCtx advis
// ColumnDisallowDropInIndexRule checks for disallow DROP COLUMN in index.
type ColumnDisallowDropInIndexRule struct {
BaseRule
- tables tableState
- originCatalog *model.DatabaseMetadata
+ tables tableState
+ originalMetadata *model.DatabaseMetadata
}
// NewColumnDisallowDropInIndexRule creates a new ColumnDisallowDropInIndexRule.
-func NewColumnDisallowDropInIndexRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *ColumnDisallowDropInIndexRule {
+func NewColumnDisallowDropInIndexRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *ColumnDisallowDropInIndexRule {
return &ColumnDisallowDropInIndexRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- tables: make(tableState),
- originCatalog: originCatalog,
+ tables: make(tableState),
+ originalMetadata: originalMetadata,
}
}
@@ -158,13 +158,12 @@ func (r *ColumnDisallowDropInIndexRule) checkAlterTable(ctx *mysql.AlterTableCon
continue
}
- index := r.originCatalog.Index("", tableName)
-
- if index != nil {
+ table := r.originalMetadata.GetSchema("").GetTable(tableName)
+ if table != nil {
if r.tables[tableName] == nil {
r.tables[tableName] = make(columnSet)
}
- for _, indexColumn := range index {
+ for _, indexColumn := range table.ListIndexes() {
for _, column := range indexColumn.ExpressionList() {
r.tables[tableName][column] = true
}
diff --git a/backend/plugin/advisor/mysql/rule_column_no_null.go b/backend/plugin/advisor/mysql/rule_column_no_null.go
index dbfbda466e..960316e18e 100644
--- a/backend/plugin/advisor/mysql/rule_column_no_null.go
+++ b/backend/plugin/advisor/mysql/rule_column_no_null.go
@@ -135,8 +135,15 @@ func (r *ColumnNoNullRule) generateAdvice() {
})
for _, column := range columnList {
- dbState := r.finalCatalog
- col := dbState.GetColumn("", column.tableName, column.columnName)
+ schema, _ := r.finalCatalog.GetSchema("")
+ if schema == nil {
+ continue
+ }
+ table, _ := schema.GetTable(column.tableName)
+ if table == nil {
+ continue
+ }
+ col, _ := table.GetColumn(column.columnName)
if col != nil && col.Nullable() {
r.AddAdvice(&storepb.Advice{
Status: r.level,
diff --git a/backend/plugin/advisor/mysql/rule_index_pk_type.go b/backend/plugin/advisor/mysql/rule_index_pk_type.go
index 94a324823a..3391e947aa 100644
--- a/backend/plugin/advisor/mysql/rule_index_pk_type.go
+++ b/backend/plugin/advisor/mysql/rule_index_pk_type.go
@@ -63,19 +63,19 @@ func (*IndexPkTypeAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([
type IndexPkTypeRule struct {
BaseRule
line map[string]int
- originCatalog *model.DatabaseMetadata
+ originalMetadata *model.DatabaseMetadata
tablesNewColumns tableColumnTypes
}
// NewIndexPkTypeRule creates a new IndexPkTypeRule.
-func NewIndexPkTypeRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *IndexPkTypeRule {
+func NewIndexPkTypeRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *IndexPkTypeRule {
return &IndexPkTypeRule{
BaseRule: BaseRule{
level: level,
title: title,
},
line: make(map[string]int),
- originCatalog: originCatalog,
+ originalMetadata: originalMetadata,
tablesNewColumns: make(tableColumnTypes),
}
}
@@ -238,7 +238,7 @@ func (r *IndexPkTypeRule) getPKColumnType(tableName string, columnName string) (
if columnType, ok := r.tablesNewColumns.get(tableName, columnName); ok {
return columnType, nil
}
- column := r.originCatalog.GetColumn("", tableName, columnName)
+ column := r.originalMetadata.GetSchema("").GetTable(tableName).GetColumn(columnName)
if column != nil {
return column.Type, nil
}
diff --git a/backend/plugin/advisor/mysql/rule_index_primary_key_type_allowlist.go b/backend/plugin/advisor/mysql/rule_index_primary_key_type_allowlist.go
index 7ba99df599..0f8e7a5df8 100644
--- a/backend/plugin/advisor/mysql/rule_index_primary_key_type_allowlist.go
+++ b/backend/plugin/advisor/mysql/rule_index_primary_key_type_allowlist.go
@@ -70,19 +70,19 @@ func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx ad
type IndexPrimaryKeyTypeAllowlistRule struct {
BaseRule
allowlist map[string]bool
- originCatalog *model.DatabaseMetadata
+ originalMetadata *model.DatabaseMetadata
tablesNewColumns tableColumnTypes
}
// NewIndexPrimaryKeyTypeAllowlistRule creates a new IndexPrimaryKeyTypeAllowlistRule.
-func NewIndexPrimaryKeyTypeAllowlistRule(level storepb.Advice_Status, title string, allowlist map[string]bool, originCatalog *model.DatabaseMetadata) *IndexPrimaryKeyTypeAllowlistRule {
+func NewIndexPrimaryKeyTypeAllowlistRule(level storepb.Advice_Status, title string, allowlist map[string]bool, originalMetadata *model.DatabaseMetadata) *IndexPrimaryKeyTypeAllowlistRule {
return &IndexPrimaryKeyTypeAllowlistRule{
BaseRule: BaseRule{
level: level,
title: title,
},
allowlist: allowlist,
- originCatalog: originCatalog,
+ originalMetadata: originalMetadata,
tablesNewColumns: make(tableColumnTypes),
}
}
@@ -252,7 +252,7 @@ func (r *IndexPrimaryKeyTypeAllowlistRule) getPKColumnType(tableName string, col
if columnType, ok := r.tablesNewColumns.get(tableName, columnName); ok {
return columnType, nil
}
- column := r.originCatalog.GetColumn("", tableName, columnName)
+ column := r.originalMetadata.GetSchema("").GetTable(tableName).GetColumn(columnName)
if column != nil {
return column.Type, nil
}
diff --git a/backend/plugin/advisor/mysql/rule_index_type_no_blob.go b/backend/plugin/advisor/mysql/rule_index_type_no_blob.go
index efa1d5ef70..da4671daf0 100644
--- a/backend/plugin/advisor/mysql/rule_index_type_no_blob.go
+++ b/backend/plugin/advisor/mysql/rule_index_type_no_blob.go
@@ -62,18 +62,18 @@ func (*IndexTypeNoBlobAdvisor) Check(_ context.Context, checkCtx advisor.Context
// IndexTypeNoBlobRule checks for index type no blob.
type IndexTypeNoBlobRule struct {
BaseRule
- originCatalog *model.DatabaseMetadata
+ originalMetadata *model.DatabaseMetadata
tablesNewColumns tableColumnTypes
}
// NewIndexTypeNoBlobRule creates a new IndexTypeNoBlobRule.
-func NewIndexTypeNoBlobRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *IndexTypeNoBlobRule {
+func NewIndexTypeNoBlobRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *IndexTypeNoBlobRule {
return &IndexTypeNoBlobRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- originCatalog: originCatalog,
+ originalMetadata: originalMetadata,
tablesNewColumns: make(tableColumnTypes),
}
}
@@ -295,7 +295,7 @@ func (r *IndexTypeNoBlobRule) getColumnType(tableName string, columnName string)
if columnType, ok := r.tablesNewColumns.get(tableName, columnName); ok {
return columnType, nil
}
- column := r.originCatalog.GetColumn("", tableName, columnName)
+ column := r.originalMetadata.GetSchema("").GetTable(tableName).GetColumn(columnName)
if column != nil {
return column.Type, nil
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_index_convention.go b/backend/plugin/advisor/mysql/rule_naming_index_convention.go
index d2cbd1588e..dc921e922b 100644
--- a/backend/plugin/advisor/mysql/rule_naming_index_convention.go
+++ b/backend/plugin/advisor/mysql/rule_naming_index_convention.go
@@ -74,24 +74,24 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.C
// NamingIndexConventionRule checks for index naming convention.
type NamingIndexConventionRule struct {
BaseRule
- text string
- format string
- maxLength int
- templateList []string
- originCatalog *model.DatabaseMetadata
+ text string
+ format string
+ maxLength int
+ templateList []string
+ originalMetadata *model.DatabaseMetadata
}
// NewNamingIndexConventionRule creates a new NamingIndexConventionRule.
-func NewNamingIndexConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originCatalog *model.DatabaseMetadata) *NamingIndexConventionRule {
+func NewNamingIndexConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originalMetadata *model.DatabaseMetadata) *NamingIndexConventionRule {
return &NamingIndexConventionRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originCatalog: originCatalog,
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: originalMetadata,
}
}
@@ -187,7 +187,7 @@ func (r *NamingIndexConventionRule) checkAlterTable(ctx *mysql.AlterTableContext
case alterListItem.RENAME_SYMBOL() != nil && alterListItem.KeyOrIndex() != nil && alterListItem.IndexRef() != nil && alterListItem.IndexName() != nil:
_, _, oldIndexName := mysqlparser.NormalizeIndexRef(alterListItem.IndexRef())
newIndexName := mysqlparser.NormalizeIndexName(alterListItem.IndexName())
- _, indexState := r.originCatalog.GetIndex("", tableName, oldIndexName)
+ indexState := r.originalMetadata.GetSchema("").GetTable(tableName).GetIndex(oldIndexName)
if indexState == nil {
continue
}
diff --git a/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go b/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go
index e67968a5ab..6534a2842a 100644
--- a/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go
+++ b/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go
@@ -74,24 +74,24 @@ type ukIndexMetaData struct {
// NamingUKConventionRule checks for unique key naming convention.
type NamingUKConventionRule struct {
BaseRule
- text string
- format string
- maxLength int
- templateList []string
- originCatalog *model.DatabaseMetadata
+ text string
+ format string
+ maxLength int
+ templateList []string
+ originalMetadata *model.DatabaseMetadata
}
// NewNamingUKConventionRule creates a new NamingUKConventionRule.
-func NewNamingUKConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originCatalog *model.DatabaseMetadata) *NamingUKConventionRule {
+func NewNamingUKConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originalMetadata *model.DatabaseMetadata) *NamingUKConventionRule {
return &NamingUKConventionRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originCatalog: originCatalog,
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: originalMetadata,
}
}
@@ -188,12 +188,8 @@ func (r *NamingUKConventionRule) checkAlterTable(ctx *mysql.AlterTableContext) {
case alterListItem.RENAME_SYMBOL() != nil && alterListItem.KeyOrIndex() != nil && alterListItem.IndexRef() != nil && alterListItem.IndexName() != nil:
_, _, oldIndexName := mysqlparser.NormalizeIndexRef(alterListItem.IndexRef())
newIndexName := mysqlparser.NormalizeIndexName(alterListItem.IndexName())
- indexStateMap := r.originCatalog.Index("", tableName)
- if indexStateMap == nil {
- continue
- }
- indexState, ok := indexStateMap[oldIndexName]
- if !ok {
+ indexState := r.originalMetadata.GetSchema("").GetTable(tableName).GetIndex(oldIndexName)
+ if indexState == nil {
continue
}
if !indexState.Unique() {
diff --git a/backend/plugin/advisor/mysql/rule_table_require_pk.go b/backend/plugin/advisor/mysql/rule_table_require_pk.go
index 1fb458c449..4203ab9809 100644
--- a/backend/plugin/advisor/mysql/rule_table_require_pk.go
+++ b/backend/plugin/advisor/mysql/rule_table_require_pk.go
@@ -69,21 +69,21 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)
// TableRequirePKRule checks table requires PK.
type TableRequirePKRule struct {
BaseRule
- tables map[string]columnSet
- line map[string]int
- originCatalog *model.DatabaseMetadata
+ tables map[string]columnSet
+ line map[string]int
+ originalMetadata *model.DatabaseMetadata
}
// NewTableRequirePKRule creates a new TableRequirePKRule.
-func NewTableRequirePKRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *TableRequirePKRule {
+func NewTableRequirePKRule(level storepb.Advice_Status, title string, originalMetadata *model.DatabaseMetadata) *TableRequirePKRule {
return &TableRequirePKRule{
BaseRule: BaseRule{
level: level,
title: title,
},
- tables: make(map[string]columnSet),
- line: make(map[string]int),
- originCatalog: originCatalog,
+ tables: make(map[string]columnSet),
+ line: make(map[string]int),
+ originalMetadata: originalMetadata,
}
}
@@ -251,7 +251,7 @@ func (r *TableRequirePKRule) changeColumn(tableName string, oldColumn string, ne
func (r *TableRequirePKRule) dropColumn(tableName string, columnName string) bool {
if _, ok := r.tables[tableName]; !ok {
- _, pk := r.originCatalog.GetIndex("", tableName, primaryKeyName)
+ pk := r.originalMetadata.GetSchema("").GetTable(tableName).GetIndex(primaryKeyName)
if pk == nil {
return false
}
diff --git a/backend/plugin/advisor/pg/advisor_column_no_null.go b/backend/plugin/advisor/pg/advisor_column_no_null.go
index 36f6df5c1b..81472330f3 100644
--- a/backend/plugin/advisor/pg/advisor_column_no_null.go
+++ b/backend/plugin/advisor/pg/advisor_column_no_null.go
@@ -45,8 +45,8 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (
level: level,
title: string(checkCtx.Rule.Type),
},
- originCatalog: checkCtx.OriginalMetadata,
- nullableColumns: make(columnMap),
+ originalMetadata: checkCtx.OriginalMetadata,
+ nullableColumns: make(columnMap),
}
checker := NewGenericChecker([]Rule{rule})
@@ -74,8 +74,8 @@ type columnMap map[columnName]int
type columnNoNullRule struct {
BaseRule
- originCatalog *model.DatabaseMetadata
- nullableColumns columnMap
+ originalMetadata *model.DatabaseMetadata
+ nullableColumns columnMap
}
func (*columnNoNullRule) Name() string {
@@ -307,8 +307,15 @@ func (r *columnNoNullRule) removeColumnByTableConstraint(schema, table string, c
if existingIndex.Name() != nil {
indexName := pg.NormalizePostgreSQLName(existingIndex.Name())
// Try to find index in catalog
- if r.originCatalog != nil {
- _, index := r.originCatalog.GetIndex(schema, table, indexName)
+ if r.originalMetadata != nil {
+ dbSchema := r.originalMetadata.GetSchema(schema)
+ var index *model.IndexMetadata
+ if dbSchema != nil {
+ dbTable := dbSchema.GetTable(table)
+ if dbTable != nil {
+ index = dbTable.GetIndex(indexName)
+ }
+ }
if index != nil {
for _, expression := range index.ExpressionList() {
r.removeColumn(schema, table, expression)
diff --git a/backend/plugin/advisor/pg/advisor_naming_index_convention.go b/backend/plugin/advisor/pg/advisor_naming_index_convention.go
index a1f26530f9..20769694a8 100644
--- a/backend/plugin/advisor/pg/advisor_naming_index_convention.go
+++ b/backend/plugin/advisor/pg/advisor_naming_index_convention.go
@@ -50,10 +50,10 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.C
level: level,
title: string(checkCtx.Rule.Type),
},
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originCatalog: checkCtx.OriginalMetadata,
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: checkCtx.OriginalMetadata,
}
checker := NewGenericChecker([]Rule{rule})
@@ -65,10 +65,10 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.C
type namingIndexConventionRule struct {
BaseRule
- format string
- maxLength int
- templateList []string
- originCatalog *model.DatabaseMetadata
+ format string
+ maxLength int
+ templateList []string
+ originalMetadata *model.DatabaseMetadata
}
// Name returns the rule name.
@@ -172,7 +172,7 @@ func (r *namingIndexConventionRule) handleRenamestmt(ctx antlr.ParserRuleContext
newIndexName := pgparser.NormalizePostgreSQLName(allNames[0])
// Look up the index in catalog to determine if it's a regular index (not unique, not PK)
- if r.originCatalog != nil && oldIndexName != "" {
+ if r.originalMetadata != nil && oldIndexName != "" {
tableName, index := r.findIndex("", "", oldIndexName)
if index != nil {
// Only check if it's a regular index (not unique, not primary)
@@ -234,8 +234,24 @@ func (r *namingIndexConventionRule) checkIndexName(indexName, tableName string,
// findIndex returns index found in catalogs, nil if not found.
func (r *namingIndexConventionRule) findIndex(schemaName string, tableName string, indexName string) (string, *model.IndexMetadata) {
- if r.originCatalog == nil {
+ if r.originalMetadata == nil {
return "", nil
}
- return r.originCatalog.GetIndex(normalizeSchemaName(schemaName), tableName, indexName)
+ schema := r.originalMetadata.GetSchema(normalizeSchemaName(schemaName))
+ if schema == nil {
+ return "", nil
+ }
+ if tableName != "" {
+ index := schema.GetTable(tableName).GetIndex(indexName)
+ if index != nil {
+ return tableName, index
+ }
+ return "", nil
+ }
+ // tableName is empty, search all tables
+ index := schema.GetIndex(indexName)
+ if index != nil {
+ return index.GetTableProto().Name, index
+ }
+ return "", nil
}
diff --git a/backend/plugin/advisor/pg/advisor_naming_primary_key_convention.go b/backend/plugin/advisor/pg/advisor_naming_primary_key_convention.go
index 6d50a5dc5d..7e63e943e9 100644
--- a/backend/plugin/advisor/pg/advisor_naming_primary_key_convention.go
+++ b/backend/plugin/advisor/pg/advisor_naming_primary_key_convention.go
@@ -50,11 +50,11 @@ func (*NamingPKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont
level: level,
title: string(checkCtx.Rule.Type),
},
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originCatalog: checkCtx.OriginalMetadata,
- statementsText: checkCtx.Statements,
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: checkCtx.OriginalMetadata,
+ statementsText: checkCtx.Statements,
}
checker := NewGenericChecker([]Rule{rule})
@@ -67,11 +67,11 @@ func (*NamingPKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont
type namingPKConventionRule struct {
BaseRule
- format string
- maxLength int
- templateList []string
- originCatalog *model.DatabaseMetadata
- statementsText string
+ format string
+ maxLength int
+ templateList []string
+ originalMetadata *model.DatabaseMetadata
+ statementsText string
}
type pkMetaData struct {
@@ -191,12 +191,12 @@ func (r *namingPKConventionRule) handleRenamestmt(ctx *parser.RenamestmtContext)
newName := pgparser.NormalizePostgreSQLName(allNames[len(allNames)-1])
// Check if old constraint is a primary key using catalog
- if r.originCatalog != nil {
+ if r.originalMetadata != nil {
normalizedSchema := schemaName
if normalizedSchema == "" {
normalizedSchema = "public"
}
- _, index := r.originCatalog.GetIndex(normalizedSchema, tableName, oldName)
+ index := r.originalMetadata.GetSchema(normalizedSchema).GetTable(tableName).GetIndex(oldName)
if index != nil && index.Primary() {
metaData := map[string]string{
advisor.ColumnListTemplateToken: strings.Join(index.ExpressionList(), "_"),
@@ -234,14 +234,22 @@ func (r *namingPKConventionRule) handleRenamestmt(ctx *parser.RenamestmtContext)
newIndexName := pgparser.NormalizePostgreSQLName(allNames[len(allNames)-1])
// Check if this index is a primary key using catalog
- if r.originCatalog != nil {
+ if r.originalMetadata != nil {
normalizedSchema := schemaName
if normalizedSchema == "" {
normalizedSchema = "public"
}
// "ALTER INDEX name RENAME TO new_name" doesn't specify table name
// Empty table name for ALTER INDEX
- tableName, index := r.originCatalog.GetIndex(normalizedSchema, "", oldIndexName)
+ schema := r.originalMetadata.GetSchema(normalizedSchema)
+ var tableName string
+ var index *model.IndexMetadata
+ if schema != nil {
+ index = schema.GetIndex(oldIndexName)
+ if index != nil {
+ tableName = index.GetTableProto().Name
+ }
+ }
if index != nil && index.Primary() {
metaData := map[string]string{
advisor.ColumnListTemplateToken: strings.Join(index.ExpressionList(), "_"),
@@ -307,12 +315,19 @@ func (r *namingPKConventionRule) getPKMetaDataFromTableConstraint(constraint par
// Extract index name properly
indexName := pgparser.NormalizePostgreSQLName(elem.Existingindex().Name())
// Find the index in catalog to get column list
- if r.originCatalog != nil && indexName != "" {
+ if r.originalMetadata != nil && indexName != "" {
normalizedSchema := schemaName
if normalizedSchema == "" {
normalizedSchema = "public"
}
- _, index := r.originCatalog.GetIndex(normalizedSchema, tableName, indexName)
+ schema := r.originalMetadata.GetSchema(normalizedSchema)
+ var index *model.IndexMetadata
+ if schema != nil {
+ table := schema.GetTable(tableName)
+ if table != nil {
+ index = table.GetIndex(indexName)
+ }
+ }
if index != nil {
columnList = index.ExpressionList()
}
diff --git a/backend/plugin/advisor/pg/advisor_naming_unique_key_convention.go b/backend/plugin/advisor/pg/advisor_naming_unique_key_convention.go
index b1b8f73959..732ab247c4 100644
--- a/backend/plugin/advisor/pg/advisor_naming_unique_key_convention.go
+++ b/backend/plugin/advisor/pg/advisor_naming_unique_key_convention.go
@@ -50,10 +50,10 @@ func (*NamingUKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont
level: level,
title: string(checkCtx.Rule.Type),
},
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originCatalog: checkCtx.OriginalMetadata,
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: checkCtx.OriginalMetadata,
}
checker := NewGenericChecker([]Rule{rule})
@@ -65,10 +65,10 @@ func (*NamingUKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont
type namingUKConventionRule struct {
BaseRule
- format string
- maxLength int
- templateList []string
- originCatalog *model.DatabaseMetadata
+ format string
+ maxLength int
+ templateList []string
+ originalMetadata *model.DatabaseMetadata
}
//nolint:unused
@@ -235,7 +235,7 @@ func (r *namingUKConventionRule) handleRenamestmt(ctx *parser.RenamestmtContext)
newIndexName := pgparser.NormalizePostgreSQLName(allNames[0])
// Look up the index in catalog to determine if it's a unique key
- if r.originCatalog != nil && oldIndexName != "" {
+ if r.originalMetadata != nil && oldIndexName != "" {
tableName, index := r.findIndex("", "", oldIndexName)
if index != nil && index.Unique() && !index.Primary() {
r.checkUniqueKeyName(newIndexName, tableName, map[string]string{
@@ -247,7 +247,7 @@ func (r *namingUKConventionRule) handleRenamestmt(ctx *parser.RenamestmtContext)
}
// Check for ALTER TABLE ... RENAME CONSTRAINT
- if ctx.CONSTRAINT() != nil && ctx.TO() != nil && r.originCatalog != nil {
+ if ctx.CONSTRAINT() != nil && ctx.TO() != nil && r.originalMetadata != nil {
allNames := ctx.AllName()
if len(allNames) >= 2 {
oldConstraintName := pgparser.NormalizePostgreSQLName(allNames[0])
@@ -300,10 +300,16 @@ func (r *namingUKConventionRule) checkTableConstraint(constraint parser.ITableco
} else if elem.Existingindex() != nil && elem.Existingindex().Name() != nil {
// Handle UNIQUE USING INDEX - the column list is in the existing index
indexName := pgparser.NormalizePostgreSQLName(elem.Existingindex().Name())
- foundTableName, index := r.findIndex("", tableName, indexName)
+ schema := r.originalMetadata.GetSchema(normalizeSchemaName(""))
+ var index *model.IndexMetadata
+ if schema != nil {
+ table := schema.GetTable(tableName)
+ if table != nil {
+ index = table.GetIndex(indexName)
+ }
+ }
if index != nil {
columnList = index.ExpressionList()
- tableName = foundTableName
}
}
@@ -408,8 +414,27 @@ func (r *namingUKConventionRule) getTemplateRegexp(tokens map[string]string) (*r
// findIndex returns index found in catalogs, nil if not found.
func (r *namingUKConventionRule) findIndex(schemaName string, tableName string, indexName string) (string, *model.IndexMetadata) {
- if r.originCatalog == nil {
+ if r.originalMetadata == nil {
return "", nil
}
- return r.originCatalog.GetIndex(normalizeSchemaName(schemaName), tableName, indexName)
+ schema := r.originalMetadata.GetSchema(normalizeSchemaName(schemaName))
+ if schema == nil {
+ return "", nil
+ }
+ if tableName != "" {
+ table := schema.GetTable(tableName)
+ if table != nil {
+ index := table.GetIndex(indexName)
+ if index != nil {
+ return tableName, index
+ }
+ }
+ return "", nil
+ }
+ // tableName is empty, search all tables
+ index := schema.GetIndex(indexName)
+ if index != nil {
+ return index.GetTableProto().Name, index
+ }
+ return "", nil
}
diff --git a/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go b/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go
index d5b97bfbba..4a46fd214f 100644
--- a/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go
+++ b/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go
@@ -43,9 +43,9 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(_ context.Context, checkCtx advi
return nil, err
}
checker := &columnDisallowChangingTypeChecker{
- level: level,
- title: string(checkCtx.Rule.Type),
- originCatalog: checkCtx.OriginalMetadata,
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ originalMetadata: checkCtx.OriginalMetadata,
}
for _, stmt := range stmtList {
@@ -58,12 +58,12 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(_ context.Context, checkCtx advi
}
type columnDisallowChangingTypeChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- text string
- line int
- originCatalog *model.DatabaseMetadata
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ text string
+ line int
+ originalMetadata *model.DatabaseMetadata
}
// Enter implements the ast.Visitor interface.
@@ -131,8 +131,7 @@ func normalizeColumnType(tp string) string {
}
func (checker *columnDisallowChangingTypeChecker) changeColumnType(tableName string, columName string, newType string) bool {
- column := checker.originCatalog.GetColumn("", tableName, columName)
-
+ column := checker.originalMetadata.GetSchema("").GetTable(tableName).GetColumn(columName)
if column == nil {
return false
}
diff --git a/backend/plugin/advisor/tidb/advisor_column_disallow_drop_in_index.go b/backend/plugin/advisor/tidb/advisor_column_disallow_drop_in_index.go
index c889e77db7..7f1c3979d2 100644
--- a/backend/plugin/advisor/tidb/advisor_column_disallow_drop_in_index.go
+++ b/backend/plugin/advisor/tidb/advisor_column_disallow_drop_in_index.go
@@ -43,10 +43,10 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(_ context.Context, checkCtx advis
}
checker := &columnDisallowDropInIndexChecker{
- level: level,
- title: string(checkCtx.Rule.Type),
- tables: make(tableState),
- originCatalog: checkCtx.OriginalMetadata,
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ tables: make(tableState),
+ originalMetadata: checkCtx.OriginalMetadata,
}
for _, stmt := range stmtList {
@@ -59,13 +59,13 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(_ context.Context, checkCtx advis
}
type columnDisallowDropInIndexChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- text string
- tables tableState // the variable mean whether the column in index.
- originCatalog *model.DatabaseMetadata
- line int
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ text string
+ tables tableState // the variable mean whether the column in index.
+ originalMetadata *model.DatabaseMetadata
+ line int
}
func (checker *columnDisallowDropInIndexChecker) Enter(in ast.Node) (ast.Node, bool) {
@@ -88,8 +88,7 @@ func (checker *columnDisallowDropInIndexChecker) dropColumn(in ast.Node) (ast.No
if spec.Tp == ast.AlterTableDropColumn {
table := node.Table.Name.O
- tableMetadata := checker.originCatalog.GetTable("", table)
-
+ tableMetadata := checker.originalMetadata.GetSchema("").GetTable(table)
if tableMetadata != nil {
if checker.tables[table] == nil {
checker.tables[table] = make(columnSet)
diff --git a/backend/plugin/advisor/tidb/advisor_column_no_null.go b/backend/plugin/advisor/tidb/advisor_column_no_null.go
index 85769bdf7b..9a168357bd 100644
--- a/backend/plugin/advisor/tidb/advisor_column_no_null.go
+++ b/backend/plugin/advisor/tidb/advisor_column_no_null.go
@@ -83,7 +83,15 @@ func (checker *columnNoNullChecker) generateAdvice() []*storepb.Advice {
})
for _, column := range columnList {
- col := checker.finalCatalog.GetColumn("", column.tableName, column.columnName)
+ schema, _ := checker.finalCatalog.GetSchema("")
+ if schema == nil {
+ continue
+ }
+ table, _ := schema.GetTable(column.tableName)
+ if table == nil {
+ continue
+ }
+ col, _ := table.GetColumn(column.columnName)
if col != nil && col.Nullable() {
checker.adviceList = append(checker.adviceList, &storepb.Advice{
Status: checker.level,
diff --git a/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go b/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go
index 475978548d..c404b18cd3 100644
--- a/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go
+++ b/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go
@@ -41,9 +41,9 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(_ context.Context, checkCtx adviso
}
checker := &allowDropEmptyDBChecker{
- level: level,
- title: string(checkCtx.Rule.Type),
- originCatalog: checkCtx.OriginalMetadata,
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ originalMetadata: checkCtx.OriginalMetadata,
}
for _, stmtNode := range root {
(stmtNode).Accept(checker)
@@ -53,24 +53,24 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(_ context.Context, checkCtx adviso
}
type allowDropEmptyDBChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- originCatalog *model.DatabaseMetadata
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ originalMetadata *model.DatabaseMetadata
}
// 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.originCatalog.DatabaseName() != node.Name.O {
+ if v.originalMetadata.DatabaseName() != node.Name.O {
v.adviceList = append(v.adviceList, &storepb.Advice{
Status: v.level,
Code: code.NotCurrentDatabase.Int32(),
Title: v.title,
- Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", node.Name, v.originCatalog.DatabaseName()),
+ Content: fmt.Sprintf("Database `%s` that is trying to be deleted is not the current database `%s`", node.Name, v.originalMetadata.DatabaseName()),
StartPosition: common.ConvertANTLRLineToPosition(node.OriginTextPosition()),
})
- } else if !v.originCatalog.HasNoTable() {
+ } else if !v.originalMetadata.HasNoTable() {
v.adviceList = append(v.adviceList, &storepb.Advice{
Status: v.level,
Code: code.DatabaseNotEmpty.Int32(),
diff --git a/backend/plugin/advisor/tidb/advisor_index_pk_type.go b/backend/plugin/advisor/tidb/advisor_index_pk_type.go
index e4360e1a84..4957e4d324 100644
--- a/backend/plugin/advisor/tidb/advisor_index_pk_type.go
+++ b/backend/plugin/advisor/tidb/advisor_index_pk_type.go
@@ -47,7 +47,7 @@ func (*IndexPkTypeAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([
level: level,
title: string(checkCtx.Rule.Type),
line: make(map[string]int),
- originCatalog: checkCtx.OriginalMetadata,
+ originalMetadata: checkCtx.OriginalMetadata,
tablesNewColumns: make(map[string]columnNameToColumnDef),
}
@@ -88,7 +88,7 @@ type indexPkTypeChecker struct {
level storepb.Advice_Status
title string
line map[string]int
- originCatalog *model.DatabaseMetadata
+ originalMetadata *model.DatabaseMetadata
tablesNewColumns tableNewColumn
}
@@ -220,7 +220,7 @@ func (v *indexPkTypeChecker) getPKColumnType(tableName string, columnName string
if colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {
return v.getIntOrBigIntStr(colDef.Tp), nil
}
- column := v.originCatalog.GetColumn("", tableName, columnName)
+ column := v.originalMetadata.GetSchema("").GetTable(tableName).GetColumn(columnName)
if column != nil {
return column.Type, nil
}
diff --git a/backend/plugin/advisor/tidb/advisor_index_primary_key_type_allowlist.go b/backend/plugin/advisor/tidb/advisor_index_primary_key_type_allowlist.go
index 245c0b121c..c444ca817e 100644
--- a/backend/plugin/advisor/tidb/advisor_index_primary_key_type_allowlist.go
+++ b/backend/plugin/advisor/tidb/advisor_index_primary_key_type_allowlist.go
@@ -55,7 +55,7 @@ func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx ad
level: level,
title: string(checkCtx.Rule.Type),
allowlist: allowlist,
- originCatalog: checkCtx.OriginalMetadata,
+ originalMetadata: checkCtx.OriginalMetadata,
tablesNewColumns: make(map[string]columnNameToColumnDef),
}
@@ -75,7 +75,7 @@ type indexPrimaryKeyTypeAllowlistChecker struct {
text string
line int
allowlist map[string]bool
- originCatalog *model.DatabaseMetadata
+ originalMetadata *model.DatabaseMetadata
tablesNewColumns tableNewColumn
}
@@ -201,7 +201,7 @@ func (v *indexPrimaryKeyTypeAllowlistChecker) getPKColumnType(tableName string,
if colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {
return tidbparser.TypeString(colDef.Tp.GetType()), nil
}
- column := v.originCatalog.GetColumn("", tableName, columnName)
+ column := v.originalMetadata.GetSchema("").GetTable(tableName).GetColumn(columnName)
if column != nil {
return column.Type, nil
}
diff --git a/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go b/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go
index 4676940c1c..c710bf728d 100644
--- a/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go
+++ b/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go
@@ -47,7 +47,7 @@ func (*IndexTypeNoBlobAdvisor) Check(_ context.Context, checkCtx advisor.Context
checker := &indexTypeNoBlobChecker{
level: level,
title: string(checkCtx.Rule.Type),
- originCatalog: checkCtx.OriginalMetadata,
+ originalMetadata: checkCtx.OriginalMetadata,
tablesNewColumns: make(map[string]columnNameToColumnDef),
}
@@ -66,7 +66,7 @@ type indexTypeNoBlobChecker struct {
title string
text string
line int
- originCatalog *model.DatabaseMetadata
+ originalMetadata *model.DatabaseMetadata
tablesNewColumns tableNewColumn
}
@@ -223,7 +223,7 @@ func (v *indexTypeNoBlobChecker) getColumnType(tableName string, columnName stri
if colDef, ok := v.tablesNewColumns.get(tableName, columnName); ok {
return v.getBlobStr(colDef.Tp), nil
}
- column := v.originCatalog.GetColumn("", tableName, columnName)
+ column := v.originalMetadata.GetSchema("").GetTable(tableName).GetColumn(columnName)
if column != nil {
return column.Type, nil
}
diff --git a/backend/plugin/advisor/tidb/advisor_naming_index_convention.go b/backend/plugin/advisor/tidb/advisor_naming_index_convention.go
index 9512a13782..4ce7dad920 100644
--- a/backend/plugin/advisor/tidb/advisor_naming_index_convention.go
+++ b/backend/plugin/advisor/tidb/advisor_naming_index_convention.go
@@ -46,12 +46,12 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.C
return nil, err
}
checker := &namingIndexConventionChecker{
- level: level,
- title: string(checkCtx.Rule.Type),
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originCatalog: checkCtx.OriginalMetadata,
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: checkCtx.OriginalMetadata,
}
for _, stmtNode := range root {
(stmtNode).Accept(checker)
@@ -61,13 +61,13 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.C
}
type namingIndexConventionChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- format string
- maxLength int
- templateList []string
- originCatalog *model.DatabaseMetadata
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ format string
+ maxLength int
+ templateList []string
+ originalMetadata *model.DatabaseMetadata
}
// Enter implements the ast.Visitor interface.
@@ -151,7 +151,11 @@ func (checker *namingIndexConventionChecker) getMetaDataList(in ast.Node) []*ind
for _, spec := range node.Specs {
switch spec.Tp {
case ast.AlterTableRenameIndex:
- _, index := checker.originCatalog.GetIndex("", node.Table.Name.String(), spec.FromKey.String())
+ schema := checker.originalMetadata.GetSchema("")
+ var index *model.IndexMetadata
+ if schema != nil {
+ index = schema.GetIndex(spec.FromKey.String())
+ }
if index == nil {
continue
}
diff --git a/backend/plugin/advisor/tidb/advisor_naming_unique_key_convention.go b/backend/plugin/advisor/tidb/advisor_naming_unique_key_convention.go
index 8dd3ea5bf6..5a09231fbe 100644
--- a/backend/plugin/advisor/tidb/advisor_naming_unique_key_convention.go
+++ b/backend/plugin/advisor/tidb/advisor_naming_unique_key_convention.go
@@ -45,12 +45,12 @@ func (*NamingUKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont
return nil, err
}
checker := &namingUKConventionChecker{
- level: level,
- title: string(checkCtx.Rule.Type),
- format: format,
- maxLength: maxLength,
- templateList: templateList,
- originCatalog: checkCtx.OriginalMetadata,
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ format: format,
+ maxLength: maxLength,
+ templateList: templateList,
+ originalMetadata: checkCtx.OriginalMetadata,
}
for _, stmtNode := range root {
(stmtNode).Accept(checker)
@@ -60,13 +60,13 @@ func (*NamingUKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont
}
type namingUKConventionChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- format string
- maxLength int
- templateList []string
- originCatalog *model.DatabaseMetadata
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ format string
+ maxLength int
+ templateList []string
+ originalMetadata *model.DatabaseMetadata
}
// Enter implements the ast.Visitor interface.
@@ -142,7 +142,11 @@ func (checker *namingUKConventionChecker) getMetaDataList(in ast.Node) []*indexM
for _, spec := range node.Specs {
switch spec.Tp {
case ast.AlterTableRenameIndex:
- _, index := checker.originCatalog.GetIndex("", node.Table.Name.String(), spec.FromKey.String())
+ schema := checker.originalMetadata.GetSchema("")
+ var index *model.IndexMetadata
+ if schema != nil {
+ index = schema.GetIndex(spec.FromKey.String())
+ }
if index == nil {
continue
}
diff --git a/backend/plugin/advisor/tidb/advisor_table_require_pk.go b/backend/plugin/advisor/tidb/advisor_table_require_pk.go
index c4053da1d9..c5a3d3a4bd 100644
--- a/backend/plugin/advisor/tidb/advisor_table_require_pk.go
+++ b/backend/plugin/advisor/tidb/advisor_table_require_pk.go
@@ -45,12 +45,12 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)
return nil, err
}
checker := &tableRequirePKChecker{
- level: level,
- title: string(checkCtx.Rule.Type),
- tables: make(tablePK),
- line: make(map[string]int),
- originCatalog: checkCtx.OriginalMetadata,
- finalCatalog: checkCtx.FinalCatalog,
+ level: level,
+ title: string(checkCtx.Rule.Type),
+ tables: make(tablePK),
+ line: make(map[string]int),
+ originalMetadata: checkCtx.OriginalMetadata,
+ finalCatalog: checkCtx.FinalCatalog,
}
for _, stmtNode := range root {
@@ -61,13 +61,13 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)
}
type tableRequirePKChecker struct {
- adviceList []*storepb.Advice
- level storepb.Advice_Status
- title string
- tables tablePK
- line map[string]int
- originCatalog *model.DatabaseMetadata
- finalCatalog *catalog.DatabaseState
+ adviceList []*storepb.Advice
+ level storepb.Advice_Status
+ title string
+ tables tablePK
+ line map[string]int
+ originalMetadata *model.DatabaseMetadata
+ finalCatalog *catalog.DatabaseState
}
// Enter implements the ast.Visitor interface.
@@ -183,11 +183,14 @@ func (v *tableRequirePKChecker) createTableLike(node *ast.CreateTableStmt) {
}
v.tables[table] = newColumnSet(columns)
} else {
- referTableMetadata := v.originCatalog.GetTable("", referTableName)
- if referTableMetadata != nil {
- primaryKey := referTableMetadata.GetPrimaryKey()
- if primaryKey != nil {
- v.tables[table] = newColumnSet(primaryKey.ExpressionList())
+ schema := v.originalMetadata.GetSchema("")
+ if schema != nil {
+ referTableMetadata := schema.GetTable(referTableName)
+ if referTableMetadata != nil {
+ primaryKey := referTableMetadata.GetPrimaryKey()
+ if primaryKey != nil {
+ v.tables[table] = newColumnSet(primaryKey.ExpressionList())
+ }
}
}
}
@@ -195,7 +198,7 @@ func (v *tableRequirePKChecker) createTableLike(node *ast.CreateTableStmt) {
func (v *tableRequirePKChecker) dropColumn(table string, column string) bool {
if _, ok := v.tables[table]; !ok {
- _, pk := v.originCatalog.GetIndex("", table, primaryKeyName)
+ pk := v.originalMetadata.GetSchema("").GetTable(table).GetIndex(primaryKeyName)
if pk == nil {
return false
}
diff --git a/backend/plugin/parser/plsql/resource_change.go b/backend/plugin/parser/plsql/resource_change.go
index dcf07c2bf3..768894bf04 100644
--- a/backend/plugin/parser/plsql/resource_change.go
+++ b/backend/plugin/parser/plsql/resource_change.go
@@ -202,11 +202,11 @@ func (l *plsqlChangedResourceExtractListener) EnterDrop_index(ctx *parser.Drop_i
if foundSchema == nil {
return
}
- indexes := foundSchema.GetIndexes(index)
- if len(indexes) == 0 {
+ foundIndex := foundSchema.GetIndex(index)
+ if foundIndex == nil {
return
}
- foundTable := indexes[0].GetTableProto().GetName()
+ foundTable := foundIndex.GetTableProto().GetName()
l.changedResources.AddTable(
schema,
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 9cc18da73e..57ec37de09 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -432,9 +432,9 @@ func (d *DatabaseMetadata) SearchIndex(searchPath []string, name string) (string
if schema == nil {
continue
}
- indexes := schema.GetIndexes(name)
- if len(indexes) > 0 {
- return schema.proto.Name, indexes[0] // Return the first index found.
+ index := schema.GetIndex(name)
+ if index != nil {
+ return schema.proto.Name, index
}
}
return "", nil
@@ -470,86 +470,6 @@ func (d *DatabaseMetadata) SearchExternalTable(searchPath []string, name string)
return "", nil
}
-// Convenience methods for advisor rules
-
-// GetColumn gets a column from the database by schema, table, and column name.
-// Returns nil if the column is not found.
-func (d *DatabaseMetadata) GetColumn(schemaName, tableName, columnName string) *storepb.ColumnMetadata {
- schema := d.GetSchema(schemaName)
- if schema == nil {
- return nil
- }
- table := schema.GetTable(tableName)
- if table == nil {
- return nil
- }
- return table.GetColumn(columnName)
-}
-
-// GetIndex gets an index from the database by schema, table, and index name.
-// Returns the table name and index metadata, or ("", nil) if not found.
-func (d *DatabaseMetadata) GetIndex(schemaName, tableName, indexName string) (string, *IndexMetadata) {
- schema := d.GetSchema(schemaName)
- if schema == nil {
- return "", nil
- }
-
- if tableName != "" {
- // Look for index in specific table
- table := schema.GetTable(tableName)
- if table == nil {
- return "", nil
- }
- index := table.GetIndex(indexName)
- if index != nil {
- return tableName, index
- }
- return "", nil
- }
-
- // Search all tables in the schema for the index
- for _, table := range schema.proto.Tables {
- tableMetadata := schema.GetTable(table.Name)
- if tableMetadata == nil {
- continue
- }
- index := tableMetadata.GetIndex(indexName)
- if index != nil {
- return table.Name, index
- }
- }
-
- return "", nil
-}
-
-// GetTable gets a table from the database by schema and table name.
-// Returns nil if the table is not found.
-func (d *DatabaseMetadata) GetTable(schemaName, tableName string) *TableMetadata {
- schema := d.GetSchema(schemaName)
- if schema == nil {
- return nil
- }
- return schema.GetTable(tableName)
-}
-
-// Index returns all indexes for a table as a map[indexName]*IndexMetadata.
-// Returns nil if the schema or table is not found.
-func (d *DatabaseMetadata) Index(schemaName, tableName string) map[string]*IndexMetadata {
- table := d.GetTable(schemaName, tableName)
- if table == nil {
- return nil
- }
-
- indexes := table.ListIndexes()
- result := make(map[string]*IndexMetadata)
- for _, index := range indexes {
- if index.proto != nil {
- result[index.proto.Name] = index
- }
- }
- return result
-}
-
// DatabaseName returns the name of the database.
func (d *DatabaseMetadata) DatabaseName() string {
if d.proto == nil {
@@ -725,6 +645,9 @@ func (s *SchemaMetadata) GetOwner() string {
// GetTable gets the schema by name.
func (s *SchemaMetadata) GetTable(name string) *TableMetadata {
+ if s == nil {
+ return nil
+ }
var nameID string
if s.isObjectCaseSensitive {
nameID = name
@@ -734,14 +657,18 @@ func (s *SchemaMetadata) GetTable(name string) *TableMetadata {
return s.internalTables[nameID]
}
-func (s *SchemaMetadata) GetIndexes(name string) []*IndexMetadata {
- var result []*IndexMetadata
+// GetIndex gets the index by name.
+// Index names are unique within a schema in most databases.
+func (s *SchemaMetadata) GetIndex(name string) *IndexMetadata {
+ if s == nil {
+ return nil
+ }
for _, table := range s.internalTables {
if index := table.GetIndex(name); index != nil {
- result = append(result, index)
+ return index
}
}
- return result
+ return nil
}
// GetView gets the view by name.
@@ -1113,6 +1040,9 @@ func (t *TableMetadata) GetTableComment() string {
// GetColumn gets the column by name.
func (t *TableMetadata) GetColumn(name string) *storepb.ColumnMetadata {
+ if t == nil {
+ return nil
+ }
var nameID string
if t.isDetailCaseSensitive {
nameID = name
@@ -1123,6 +1053,9 @@ func (t *TableMetadata) GetColumn(name string) *storepb.ColumnMetadata {
}
func (t *TableMetadata) GetIndex(name string) *IndexMetadata {
+ if t == nil {
+ return nil
+ }
var nameID string
if t.isDetailCaseSensitive {
nameID = name