Skip to content

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

refactor: merge Config and Metadata hierarchies into unified structure (#18154)

Danny Xuee7ba1226528
diff --git a/backend/api/v1/query_result_masker.go b/backend/api/v1/query_result_masker.go
index 1543478a6d..3ac289dfd6 100644
--- a/backend/api/v1/query_result_masker.go
+++ b/backend/api/v1/query_result_masker.go
@@ -344,12 +344,9 @@ func (s *QueryResultMasker) getColumnForColumnResource(ctx context.Context, inst
if column == nil {
return nil, nil, nil
}
- columnMetadata = column
+ columnMetadata = column.GetProto()
- var columnConfig *storepb.ColumnCatalog
- schemaConfig := dbMetadata.GetSchemaConfig(sourceColumn.Schema)
- tableConfig := schemaConfig.GetTableConfig(sourceColumn.Table)
- columnConfig = tableConfig.GetColumnConfig(sourceColumn.Column)
+ columnConfig := column.GetCatalog()
return columnMetadata, columnConfig, nil
}
diff --git a/backend/api/v1/sql_service.go b/backend/api/v1/sql_service.go
index e9507448e5..0347a4e773 100644
--- a/backend/api/v1/sql_service.go
+++ b/backend/api/v1/sql_service.go
@@ -1758,19 +1758,31 @@ func (*SQLService) DiffMetadata(_ context.Context, req *connect.Request[v1pb.Dif
func sanitizeCommentForSchemaMetadata(dbMetadata *storepb.DatabaseSchemaMetadata, dbModelConfig *model.DatabaseMetadata, classificationFromConfig bool) {
for _, schema := range dbMetadata.Schemas {
- schemaConfig := dbModelConfig.GetSchemaConfig(schema.Name)
+ schemaMetadata := dbModelConfig.GetSchemaMetadata(schema.Name)
for _, table := range schema.Tables {
- tableConfig := schemaConfig.GetTableConfig(table.Name)
classificationID := ""
if !classificationFromConfig {
- classificationID = tableConfig.Classification
+ if schemaMetadata != nil {
+ if tableMetadata := schemaMetadata.GetTable(table.Name); tableMetadata != nil {
+ if tableCatalog := tableMetadata.GetCatalog(); tableCatalog != nil {
+ classificationID = tableCatalog.Classification
+ }
+ }
+ }
}
table.Comment = common.GetCommentFromClassificationAndUserComment(classificationID, table.UserComment)
for _, col := range table.Columns {
- columnConfig := tableConfig.GetColumnConfig(col.Name)
classificationID := ""
if !classificationFromConfig {
- classificationID = columnConfig.Classification
+ if schemaMetadata != nil {
+ if tableMetadata := schemaMetadata.GetTable(table.Name); tableMetadata != nil {
+ if columnMetadata := tableMetadata.GetColumn(col.Name); columnMetadata != nil {
+ if columnCatalog := columnMetadata.GetCatalog(); columnCatalog != nil {
+ classificationID = columnCatalog.Classification
+ }
+ }
+ }
+ }
}
col.Comment = common.GetCommentFromClassificationAndUserComment(classificationID, col.UserComment)
}
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 5362f1baf6..045b38c1d2 100644
--- a/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go
+++ b/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go
@@ -175,7 +175,7 @@ func (r *ColumnDisallowChangingTypeRule) changeColumnType(tableName, columnName
return
}
- if normalizeColumnType(column.Type) != normalizeColumnType(tp) {
+ if normalizeColumnType(column.GetProto().Type) != normalizeColumnType(tp) {
r.AddAdvice(&storepb.Advice{
Status: r.level,
Code: code.ChangeColumnType.Int32(),
diff --git a/backend/plugin/advisor/mysql/rule_column_no_null.go b/backend/plugin/advisor/mysql/rule_column_no_null.go
index 53a091d3e8..32ef3f3111 100644
--- a/backend/plugin/advisor/mysql/rule_column_no_null.go
+++ b/backend/plugin/advisor/mysql/rule_column_no_null.go
@@ -144,7 +144,7 @@ func (r *ColumnNoNullRule) generateAdvice() {
continue
}
col := table.GetColumn(column.columnName)
- if col != nil && col.Nullable {
+ if col != nil && col.GetProto().Nullable {
r.AddAdvice(&storepb.Advice{
Status: r.level,
Code: code.ColumnCannotNull.Int32(),
diff --git a/backend/plugin/advisor/mysql/rule_index_pk_type.go b/backend/plugin/advisor/mysql/rule_index_pk_type.go
index 2aba4132a5..c43284bd83 100644
--- a/backend/plugin/advisor/mysql/rule_index_pk_type.go
+++ b/backend/plugin/advisor/mysql/rule_index_pk_type.go
@@ -240,7 +240,7 @@ func (r *IndexPkTypeRule) getPKColumnType(tableName string, columnName string) (
}
column := r.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)
if column != nil {
- return column.Type, nil
+ return column.GetProto().Type, nil
}
return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
}
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 3cdca5e648..0cf85b35bd 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
@@ -254,7 +254,7 @@ func (r *IndexPrimaryKeyTypeAllowlistRule) getPKColumnType(tableName string, col
}
column := r.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)
if column != nil {
- return column.Type, nil
+ return column.GetProto().Type, nil
}
return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
}
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 dc87ce71b0..3eef6f76dd 100644
--- a/backend/plugin/advisor/mysql/rule_index_type_no_blob.go
+++ b/backend/plugin/advisor/mysql/rule_index_type_no_blob.go
@@ -297,7 +297,7 @@ func (r *IndexTypeNoBlobRule) getColumnType(tableName string, columnName string)
}
column := r.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)
if column != nil {
- return column.Type, nil
+ return column.GetProto().Type, nil
}
return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
}
diff --git a/backend/plugin/advisor/mysql/rule_statement_join_strict_column_attrs.go b/backend/plugin/advisor/mysql/rule_statement_join_strict_column_attrs.go
index 748dc09eaa..eb487422c6 100644
--- a/backend/plugin/advisor/mysql/rule_statement_join_strict_column_attrs.go
+++ b/backend/plugin/advisor/mysql/rule_statement_join_strict_column_attrs.go
@@ -230,7 +230,7 @@ func (r *StatementJoinStrictColumnAttrsRule) checkColumnAttrs(leftColumnAttr *Co
if leftColumn == nil || rightColumn == nil {
return
}
- if leftColumn.Type != rightColumn.Type || leftColumn.CharacterSet != rightColumn.CharacterSet || leftColumn.Collation != rightColumn.Collation {
+ if leftColumn.GetProto().Type != rightColumn.GetProto().Type || leftColumn.GetProto().CharacterSet != rightColumn.GetProto().CharacterSet || leftColumn.GetProto().Collation != rightColumn.GetProto().Collation {
r.AddAdvice(&storepb.Advice{
Status: r.level,
Code: code.StatementJoinColumnAttrsNotMatch.Int32(),
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 3f55f7d99b..d25891f028 100644
--- a/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go
+++ b/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go
@@ -136,5 +136,5 @@ func (checker *columnDisallowChangingTypeChecker) changeColumnType(tableName str
return false
}
- return normalizeColumnType(column.Type) != normalizeColumnType(newType)
+ return normalizeColumnType(column.GetProto().Type) != normalizeColumnType(newType)
}
diff --git a/backend/plugin/advisor/tidb/advisor_column_no_null.go b/backend/plugin/advisor/tidb/advisor_column_no_null.go
index 93ba08f8b4..7317465793 100644
--- a/backend/plugin/advisor/tidb/advisor_column_no_null.go
+++ b/backend/plugin/advisor/tidb/advisor_column_no_null.go
@@ -92,7 +92,7 @@ func (checker *columnNoNullChecker) generateAdvice() []*storepb.Advice {
continue
}
col := table.GetColumn(column.columnName)
- if col != nil && col.Nullable {
+ if col != nil && col.GetProto().Nullable {
checker.adviceList = append(checker.adviceList, &storepb.Advice{
Status: checker.level,
Code: code.ColumnCannotNull.Int32(),
diff --git a/backend/plugin/advisor/tidb/advisor_index_pk_type.go b/backend/plugin/advisor/tidb/advisor_index_pk_type.go
index aaa9f16a14..58e2256037 100644
--- a/backend/plugin/advisor/tidb/advisor_index_pk_type.go
+++ b/backend/plugin/advisor/tidb/advisor_index_pk_type.go
@@ -222,7 +222,7 @@ func (v *indexPkTypeChecker) getPKColumnType(tableName string, columnName string
}
column := v.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)
if column != nil {
- return column.Type, nil
+ return column.GetProto().Type, nil
}
return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
}
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 3f29b39555..b1238f0fee 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
@@ -203,7 +203,7 @@ func (v *indexPrimaryKeyTypeAllowlistChecker) getPKColumnType(tableName string,
}
column := v.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)
if column != nil {
- return column.Type, nil
+ return column.GetProto().Type, nil
}
return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
}
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 71654055d1..a0a68235ef 100644
--- a/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go
+++ b/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go
@@ -225,7 +225,7 @@ func (v *indexTypeNoBlobChecker) getColumnType(tableName string, columnName stri
}
column := v.originalMetadata.GetSchemaMetadata("").GetTable(tableName).GetColumn(columnName)
if column != nil {
- return column.Type, nil
+ return column.GetProto().Type, nil
}
return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)
}
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.go
index 9dbb27970f..977dd338a2 100644
--- a/backend/plugin/schema/differ.go
+++ b/backend/plugin/schema/differ.go
@@ -645,18 +645,21 @@ func compareColumns(engine storepb.Engine, oldTable, newTable *model.TableMetada
// Check for new and modified columns
for _, newCol := range newColumns {
- oldCol := oldTable.GetColumn(newCol.Name)
- if oldCol == nil {
+ oldColWrapper := oldTable.GetColumn(newCol.Name)
+ if oldColWrapper == nil {
changes = append(changes, &ColumnDiff{
Action: MetadataDiffActionCreate,
NewColumn: newCol,
})
- } else if !columnsEqual(engine, oldCol, newCol) {
- changes = append(changes, &ColumnDiff{
- Action: MetadataDiffActionAlter,
- OldColumn: oldCol,
- NewColumn: newCol,
- })
+ } else {
+ oldCol := oldColWrapper.GetProto()
+ if !columnsEqual(engine, oldCol, newCol) {
+ changes = append(changes, &ColumnDiff{
+ Action: MetadataDiffActionAlter,
+ OldColumn: oldCol,
+ NewColumn: newCol,
+ })
+ }
}
}
diff --git a/backend/plugin/schema/mysql/walk_through.go b/backend/plugin/schema/mysql/walk_through.go
index 0d6f4a2a67..e805b6742e 100644
--- a/backend/plugin/schema/mysql/walk_through.go
+++ b/backend/plugin/schema/mysql/walk_through.go
@@ -902,14 +902,15 @@ func mysqlAlterColumn(table *model.TableMetadata, itemDef mysql.IAlterListItemCo
}
}
+ colProto := col.GetProto()
switch {
case itemDef.SET_SYMBOL() != nil:
switch {
// SET DEFAULT.
case itemDef.DEFAULT_SYMBOL() != nil:
if itemDef.SignedLiteral() != nil && itemDef.SignedLiteral().Literal() != nil && itemDef.SignedLiteral().Literal().NullLiteral() == nil {
- if col.Type != "" {
- switch strings.ToLower(col.Type) {
+ if colProto.Type != "" {
+ switch strings.ToLower(colProto.Type) {
case "blob", "tinyblob", "mediumblob", "longblob",
"text", "tinytext", "mediumtext", "longtext",
"json",
@@ -936,9 +937,9 @@ func mysqlAlterColumn(table *model.TableMetadata, itemDef mysql.IAlterListItemCo
// No default value expression
}
- col.Default = defaultValue
+ colProto.Default = defaultValue
} else {
- if !col.Nullable {
+ if !colProto.Nullable {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.SetNullDefaultForNotNullColumn.Int32(),
@@ -948,14 +949,14 @@ func mysqlAlterColumn(table *model.TableMetadata, itemDef mysql.IAlterListItemCo
}
}
- col.Default = ""
+ colProto.Default = ""
}
// SET VISIBLE/INVISIBLE.
default:
}
case itemDef.DROP_SYMBOL() != nil && itemDef.DEFAULT_SYMBOL() != nil:
// DROP DEFAULT.
- col.Default = ""
+ colProto.Default = ""
default:
// Other ALTER operations
}
@@ -1004,28 +1005,29 @@ func mysqlChangeColumn(table *model.TableMetadata, oldColumnName string, newColu
column = table.GetColumn(newColumnName)
}
+ columnProto := column.GetProto()
// Update column properties from fieldDef
if fieldDef.DataType() != nil {
- column.Type = mysqlparser.NormalizeMySQLDataType(fieldDef.DataType(), true /* compact */)
- column.CharacterSet = mysqlparser.GetCharSetName(fieldDef.DataType())
- column.Collation = mysqlparser.GetCollationName(fieldDef)
+ columnProto.Type = mysqlparser.NormalizeMySQLDataType(fieldDef.DataType(), true /* compact */)
+ columnProto.CharacterSet = mysqlparser.GetCharSetName(fieldDef.DataType())
+ columnProto.Collation = mysqlparser.GetCollationName(fieldDef)
}
// Reset nullable to true, then check for NOT NULL constraint
// This ensures that if the new definition doesn't have NOT NULL, the column becomes nullable
- column.Nullable = true
+ columnProto.Nullable = true
for _, attribute := range fieldDef.AllColumnAttribute() {
if attribute == nil {
continue
}
// Check for NOT NULL constraint
if attribute.NullLiteral() != nil && attribute.NOT_SYMBOL() != nil {
- column.Nullable = false
+ columnProto.Nullable = false
break
}
// Check for PRIMARY KEY or UNIQUE KEY (these imply NOT NULL for primary keys)
if attribute.GetValue() != nil && attribute.GetValue().GetTokenType() == mysql.MySQLParserKEY_SYMBOL {
- column.Nullable = false
+ columnProto.Nullable = false
break
}
}
@@ -1038,7 +1040,7 @@ func mysqlChangeColumn(table *model.TableMetadata, oldColumnName string, newColu
// Find the current position of the column
var currentIdx int
for i, col := range columns {
- if col == column {
+ if col == column.GetProto() {
currentIdx = i
break
}
@@ -1067,9 +1069,9 @@ func mysqlChangeColumn(table *model.TableMetadata, oldColumnName string, newColu
// Insert at newIdx
if newIdx >= len(columns) {
- columns = append(columns, column)
+ columns = append(columns, column.GetProto())
} else {
- columns = append(columns[:newIdx], append([]*storepb.ColumnMetadata{column}, columns[newIdx:]...)...)
+ columns = append(columns[:newIdx], append([]*storepb.ColumnMetadata{column.GetProto()}, columns[newIdx:]...)...)
}
// Update the proto's column list
@@ -1166,7 +1168,7 @@ func mysqlCopyTable(d *model.DatabaseMetadata, databaseName, tableName, referTab
StartPosition: &storepb.Position{Line: 0},
}
}
- if err := newTable.CreateColumn(colCopy); err != nil {
+ if err := newTable.CreateColumn(colCopy, nil /* columnCatalog */); err != nil {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.ColumnExists.Int32(),
@@ -1353,14 +1355,14 @@ func mysqlValidateColumnList(table *model.TableMetadata, columnList []string, pr
}
}
if primary {
- column.Nullable = false
+ column.GetProto().Nullable = false
}
- if isSpatial && column.Nullable {
+ if isSpatial && column.GetProto().Nullable {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.SpatialIndexKeyNullable.Int32(),
- Title: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.Name),
- Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.Name),
+ Title: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name),
+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name),
StartPosition: &storepb.Position{Line: 0},
}
}
@@ -1379,14 +1381,14 @@ func mysqlValidateExpressionList(table *model.TableMetadata, expressionList []st
}
if primary {
- column.Nullable = false
+ column.GetProto().Nullable = false
}
- if isSpatial && column.Nullable {
+ if isSpatial && column.GetProto().Nullable {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.SpatialIndexKeyNullable.Int32(),
- Title: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.Name),
- Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.Name),
+ Title: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name),
+ Content: fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name),
StartPosition: &storepb.Position{Line: 0},
}
}
@@ -1555,7 +1557,7 @@ func mysqlCreateColumn(table *model.TableMetadata, columnName string, fieldDef m
}
}
- if err := table.CreateColumn(col); err != nil {
+ if err := table.CreateColumn(col, nil /* columnCatalog */); err != nil {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.ColumnExists.Int32(),
diff --git a/backend/plugin/schema/pg/walk_through.go b/backend/plugin/schema/pg/walk_through.go
index 614c850fba..4fb8d43cf0 100644
--- a/backend/plugin/schema/pg/walk_through.go
+++ b/backend/plugin/schema/pg/walk_through.go
@@ -291,7 +291,7 @@ func pgCreateColumn(schema *model.SchemaMetadata, table *model.TableMetadata, co
}
}
// Create the column
- if err := table.CreateColumn(col); err != nil {
+ if err := table.CreateColumn(col, nil /* columnCatalog */); err != nil {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.ColumnExists.Int32(),
@@ -330,7 +330,7 @@ func createTableConstraint(schema *model.SchemaMetadata, table *model.TableMetad
for _, colName := range columnList {
column := table.GetColumn(colName)
if column != nil {
- column.Nullable = false
+ column.GetProto().Nullable = false
} else {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
@@ -826,7 +826,7 @@ func (l *pgCatalogListener) alterTableAlterColumnType(schema *model.SchemaMetada
return
}
// Update column type
- column.Type = typeString
+ column.GetProto().Type = typeString
}
// alterTableAddColumn handles ADD COLUMN command.
@@ -957,7 +957,7 @@ func (l *pgCatalogListener) alterTableAddColumn(schema *model.SchemaMetadata, ta
}
}
// Create the column
- if err := table.CreateColumn(col); err != nil {
+ if err := table.CreateColumn(col, nil /* columnCatalog */); err != nil {
l.advice = &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.ColumnExists.Int32(),
@@ -1019,7 +1019,7 @@ func (l *pgCatalogListener) alterTableSetDefault(table *model.TableMetadata, col
}
return
}
- column.Default = defaultValue
+ column.GetProto().Default = defaultValue
}
// alterTableDropDefault handles ALTER COLUMN DROP DEFAULT command.
@@ -1035,7 +1035,7 @@ func (l *pgCatalogListener) alterTableDropDefault(table *model.TableMetadata, co
}
return
}
- column.Default = ""
+ column.GetProto().Default = ""
}
// alterTableSetNotNull handles ALTER COLUMN SET NOT NULL command.
@@ -1051,7 +1051,7 @@ func (l *pgCatalogListener) alterTableSetNotNull(table *model.TableMetadata, col
}
return
}
- column.Nullable = false
+ column.GetProto().Nullable = false
}
// renameTable handles RENAME TO for tables.
diff --git a/backend/plugin/schema/tidb/walk_through.go b/backend/plugin/schema/tidb/walk_through.go
index acc6d876a0..b7cdfdab0f 100644
--- a/backend/plugin/schema/tidb/walk_through.go
+++ b/backend/plugin/schema/tidb/walk_through.go
@@ -566,11 +566,12 @@ func tidbChangeColumnDefault(t *model.TableMetadata, column *tidbast.ColumnDef)
}
}
+ colProto := col.GetProto()
if len(column.Options) == 1 {
// SET DEFAULT
if column.Options[0].Expr.GetType().GetType() != mysql.TypeNull {
- if col.Type != "" {
- switch strings.ToLower(col.Type) {
+ if colProto.Type != "" {
+ switch strings.ToLower(colProto.Type) {
case "blob", "tinyblob", "mediumblob", "longblob",
"text", "tinytext", "mediumtext", "longtext",
"json",
@@ -599,9 +600,9 @@ func tidbChangeColumnDefault(t *model.TableMetadata, column *tidbast.ColumnDef)
StartPosition: &storepb.Position{Line: 0},
}
}
- col.Default = defaultValue
+ colProto.Default = defaultValue
} else {
- if !col.Nullable {
+ if !colProto.Nullable {
content := fmt.Sprintf("Invalid default value for column `%s`", columnName)
return &storepb.Advice{
Status: storepb.Advice_ERROR,
@@ -611,11 +612,11 @@ func tidbChangeColumnDefault(t *model.TableMetadata, column *tidbast.ColumnDef)
StartPosition: &storepb.Position{Line: 0},
}
}
- col.Default = ""
+ colProto.Default = ""
}
} else {
// DROP DEFAULT
- col.Default = ""
+ colProto.Default = ""
}
return nil
}
@@ -667,7 +668,7 @@ func tidbCompleteTableChangeColumn(t *model.TableMetadata, oldName string, newCo
tableProto := t.GetProto()
var currentIdx int
for i, col := range tableProto.Columns {
- if col == column {
+ if col == column.GetProto() {
currentIdx = i
break
}
@@ -754,12 +755,13 @@ func tidbReorderColumn(t *model.TableMetadata, position *tidbast.ColumnPosition)
StartPosition: &storepb.Position{Line: 0},
}
}
+ columnProto := column.GetProto()
for _, col := range t.GetProto().GetColumns() {
- if col.Position > column.Position {
+ if col.Position > columnProto.Position {
col.Position++
}
}
- return int(column.Position) + 1, nil
+ return int(columnProto.Position) + 1, nil
default:
content := fmt.Sprintf("Unsupported column position type: %d", position.Tp)
return 0, &storepb.Advice{
@@ -862,7 +864,7 @@ func tidbCopyTable(d *model.DatabaseMetadata, node *tidbast.CreateTableStmt) *st
StartPosition: &storepb.Position{Line: 0},
}
}
- if err := newTable.CreateColumn(colCopy); err != nil {
+ if err := newTable.CreateColumn(colCopy, nil /* columnCatalog */); err != nil {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.ColumnExists.Int32(),
@@ -1061,10 +1063,10 @@ func tidbValidateAndGetKeyStringList(t *model.TableMetadata, keyList []*tidbast.
}
}
if primary {
- column.Nullable = false
+ column.GetProto().Nullable = false
}
- if isSpatial && column.Nullable {
- content := fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.Name)
+ if isSpatial && column.GetProto().Nullable {
+ content := fmt.Sprintf("All parts of a SPATIAL index must be NOT NULL, but `%s` is nullable", column.GetProto().Name)
return nil, &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.SpatialIndexKeyNullable.Int32(),
@@ -1246,7 +1248,7 @@ func tidbCreateColumnHelper(t *model.TableMetadata, column *tidbast.ColumnDef, p
}
}
- if err := t.CreateColumn(col); err != nil {
+ if err := t.CreateColumn(col, nil /* columnCatalog */); err != nil {
return &storepb.Advice{
Status: storepb.Advice_ERROR,
Code: code.ColumnExists.Int32(),
diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.go
index f27d3dce23..5b5d17f7d1 100644
--- a/backend/runner/schemasync/syncer.go
+++ b/backend/runner/schemasync/syncer.go
@@ -572,16 +572,22 @@ func buildDatabaseConfigWithClassificationFromComment(dbMetadata *storepb.Databa
var tables []*storepb.TableCatalog
hasSchemaContent := false
- // Get existing schema config to preserve SemanticType and Labels
- schemaConfig := databaseConfig.GetSchemaConfig(schema.Name)
+ // Get existing schema metadata to preserve SemanticType and Labels
+ var schemaMetadata *model.SchemaMetadata
+ if databaseConfig != nil {
+ schemaMetadata = databaseConfig.GetSchemaMetadata(schema.Name)
+ }
for _, table := range schema.Tables {
classification, userComment := common.GetClassificationAndUserComment(table.Comment, classificationConfig)
table.UserComment = userComment
- // Get existing table config
- tableConfig := schemaConfig.GetTableConfig(table.Name)
+ // Get existing table metadata and catalog
+ var tableMetadata *model.TableMetadata
+ if schemaMetadata != nil {
+ tableMetadata = schemaMetadata.GetTable(table.Name)
+ }
var columns []*storepb.ColumnCatalog
hasTableContent := false
@@ -597,7 +603,15 @@ func buildDatabaseConfigWithClassificationFromComment(dbMetadata *storepb.Databa
col.UserComment = colUserComment
// Get existing column config to preserve SemanticType and Labels
- existingColumnConfig := tableConfig.GetColumnConfig(col.Name)
+ var existingColumnConfig *storepb.ColumnCatalog
+ if tableMetadata != nil {
+ if columnMetadata := tableMetadata.GetColumn(col.Name); columnMetadata != nil {
+ existingColumnConfig = columnMetadata.GetCatalog()
+ }
+ }
+ if existingColumnConfig == nil {
+ existingColumnConfig = &storepb.ColumnCatalog{}
+ }
// Add column config if it has any meaningful data
if colClassification != "" || existingColumnConfig.GetSemanticType() != "" || len(existingColumnConfig.GetLabels()) > 0 {
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index dc921ee120..1eabb7e4a9 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -24,11 +24,10 @@ type DatabaseMetadata struct {
// Metadata fields (formerly in DatabaseMetadata)
searchPath []string
internal map[string]*SchemaMetadata
- configInternal map[string]*SchemaConfig
linkedDatabase map[string]*storepb.LinkedDatabaseMetadata
}
-// SchemaMetadata is the metadata for a schema.
+// SchemaMetadata is the unified metadata for a schema, combining proto metadata and catalog config.
type SchemaMetadata struct {
isObjectCaseSensitive bool
isDetailCaseSensitive bool
@@ -40,29 +39,21 @@ type SchemaMetadata struct {
internalSequences map[string]*storepb.SequenceMetadata
internalPackages map[string]*storepb.PackageMetadata
- proto *storepb.SchemaMetadata
+ proto *storepb.SchemaMetadata
+ config *storepb.SchemaCatalog
}
-// SchemaConfig is the config for a schema.
-type SchemaConfig struct {
- internal map[string]*TableConfig
-}
-
-// TableMetadata is the metadata for a table.
+// TableMetadata is the unified metadata for a table, combining proto metadata and catalog config.
type TableMetadata struct {
// If partitionOf is not nil, it means this table is a partition table.
partitionOf *TableMetadata
isDetailCaseSensitive bool
- internalColumn map[string]*storepb.ColumnMetadata
+ internalColumn map[string]*ColumnMetadata
internalIndexes map[string]*IndexMetadata
- proto *storepb.TableMetadata
-}
-// TableConfig is the config for a table.
-type TableConfig struct {
- Classification string
- internal map[string]*storepb.ColumnCatalog
+ proto *storepb.TableMetadata
+ config *storepb.TableCatalog
}
// ExternalTableMetadata is the metadata for a external table.
@@ -77,6 +68,12 @@ type IndexMetadata struct {
proto *storepb.IndexMetadata
}
+// ColumnMetadata is the unified metadata for a column, combining proto metadata and catalog config.
+type ColumnMetadata struct {
+ proto *storepb.ColumnMetadata
+ config *storepb.ColumnCatalog
+}
+
// normalizeNameByCaseSensitivity normalizes a name based on case sensitivity.
// If caseSensitive is true, returns the name as-is; otherwise returns lowercase.
func normalizeNameByCaseSensitivity(name string, caseSensitive bool) string {
@@ -103,11 +100,29 @@ func NewDatabaseMetadata(
searchPath: normalizeSearchPath(metadata.SearchPath),
internal: make(map[string]*SchemaMetadata),
linkedDatabase: make(map[string]*storepb.LinkedDatabaseMetadata),
- configInternal: make(map[string]*SchemaConfig),
+ }
+
+ // Build a map of schema catalogs for quick lookup
+ schemaCatalogMap := make(map[string]*storepb.SchemaCatalog)
+ if config != nil {
+ for _, schemaCatalog := range config.Schemas {
+ schemaCatalogMap[schemaCatalog.Name] = schemaCatalog
+ }
}
// Build schema metadata maps
for _, s := range metadata.Schemas {
+ // Get matching schema catalog if it exists
+ schemaCatalog := schemaCatalogMap[s.Name]
+
+ // Build a map of table catalogs for this schema
+ tableCatalogMap := make(map[string]*storepb.TableCatalog)
+ if schemaCatalog != nil {
+ for _, tableCatalog := range schemaCatalog.Tables {
+ tableCatalogMap[tableCatalog.Name] = tableCatalog
+ }
+ }
+
schemaMetadata := &SchemaMetadata{
isObjectCaseSensitive: isObjectCaseSensitive,
isDetailCaseSensitive: isDetailCaseSensitive,
@@ -119,9 +134,11 @@ func NewDatabaseMetadata(
internalPackages: make(map[string]*storepb.PackageMetadata),
internalSequences: make(map[string]*storepb.SequenceMetadata),
proto: s,
+ config: schemaCatalog,
}
for _, table := range s.Tables {
- tables, names := buildTablesMetadata(table, isDetailCaseSensitive)
+ tableCatalog := tableCatalogMap[table.Name]
+ tables, names := buildTablesMetadata(table, tableCatalog, isDetailCaseSensitive)
for i, table := range tables {
tableID := normalizeNameByCaseSensitivity(names[i], isObjectCaseSensitive)
schemaMetadata.internalTables[tableID] = table
@@ -169,26 +186,6 @@ func NewDatabaseMetadata(
dbMetadata.linkedDatabase[dbLinkID] = dbLink
}
- // Build config maps
- if config != nil {
- for _, schemaProto := range config.Schemas {
- schemaConfig := &SchemaConfig{
- internal: make(map[string]*TableConfig),
- }
- for _, table := range schemaProto.Tables {
- tableConfig := &TableConfig{
- Classification: table.Classification,
- internal: make(map[string]*storepb.ColumnCatalog),
- }
- for _, column := range table.Columns {
- tableConfig.internal[column.Name] = column
- }
- schemaConfig.internal[table.Name] = tableConfig
- }
- dbMetadata.configInternal[schemaProto.Name] = schemaConfig
- }
- }
-
return dbMetadata
}
@@ -306,73 +303,47 @@ func (d *DatabaseMetadata) DropSchema(schemaName string) error {
return nil
}
-// Config methods (formerly from DatabaseConfig)
-func (d *DatabaseMetadata) GetSchemaConfig(name string) *SchemaConfig {
- if d == nil {
- return nil
- }
- if config := d.configInternal[name]; config != nil {
- return config
- }
- return &SchemaConfig{
- internal: make(map[string]*TableConfig),
- }
-}
-
func (d *DatabaseMetadata) BuildDatabaseConfig() *storepb.DatabaseConfig {
if d == nil {
return nil
}
config := &storepb.DatabaseConfig{Name: d.config.GetName()}
- for schemaName, sConfig := range d.configInternal {
- schemaConfig := &storepb.SchemaCatalog{Name: schemaName}
+ for _, schemaMetadata := range d.internal {
+ schemaCatalog := schemaMetadata.GetCatalog()
+ if schemaCatalog == nil {
+ continue
+ }
- for tableName, tConfig := range sConfig.internal {
- tableConfig := &storepb.TableCatalog{Name: tableName, Classification: tConfig.Classification}
+ // Reconstruct the schema catalog with updated table/column catalogs
+ reconstructedSchema := &storepb.SchemaCatalog{Name: schemaMetadata.proto.Name}
- for colName, colConfig := range tConfig.internal {
- tableConfig.Columns = append(tableConfig.Columns, &storepb.ColumnCatalog{
- Name: colName,
- SemanticType: colConfig.SemanticType,
- Labels: colConfig.Labels,
- Classification: colConfig.Classification,
- })
+ for _, tableMetadata := range schemaMetadata.internalTables {
+ tableCatalog := tableMetadata.GetCatalog()
+ if tableCatalog == nil {
+ continue
}
- schemaConfig.Tables = append(schemaConfig.Tables, tableConfig)
- }
- config.Schemas = append(config.Schemas, schemaConfig)
- }
- return config
-}
+ // Reconstruct the table catalog with updated column catalogs
+ reconstructedTable := &storepb.TableCatalog{
+ Name: tableMetadata.proto.Name,
+ Classification: tableCatalog.Classification,
+ }
-// GetTableConfig gets the table config by name.
-// If not found, returns a new empty table config.
-func (s *SchemaConfig) GetTableConfig(name string) *TableConfig {
- if s == nil {
- return nil
- }
- if config := s.internal[name]; config != nil {
- return config
- }
- return &TableConfig{
- internal: make(map[string]*storepb.ColumnCatalog),
- }
-}
+ for _, columnMetadata := range tableMetadata.internalColumn {
+ columnCatalog := columnMetadata.GetCatalog()
+ if columnCatalog != nil {
+ reconstructedTable.Columns = append(reconstructedTable.Columns, columnCatalog)
+ }
+ }
-// GetColumnConfig gets the column config by name.
-// If not found, returns a new empty column config.
-func (t *TableConfig) GetColumnConfig(name string) *storepb.ColumnCatalog {
- if t == nil {
- return nil
- }
- if config := t.internal[name]; config != nil {
- return config
- }
- return &storepb.ColumnCatalog{
- Name: name,
+ reconstructedSchema.Tables = append(reconstructedSchema.Tables, reconstructedTable)
+ }
+
+ config.Schemas = append(config.Schemas, reconstructedSchema)
}
+
+ return config
}
// GetTable gets the schema by name.
@@ -471,6 +442,11 @@ func (s *SchemaMetadata) GetProto() *storepb.SchemaMetadata {
return s.proto
}
+// GetCatalog gets the catalog of SchemaMetadata.
+func (s *SchemaMetadata) GetCatalog() *storepb.SchemaCatalog {
+ return s.config
+}
+
// ListTableNames lists the table names.
func (s *SchemaMetadata) ListTableNames() []string {
var result []string
@@ -547,7 +523,7 @@ func (s *SchemaMetadata) CreateTable(tableName string) (*TableMetadata, error) {
// Create TableMetadata wrapper
tableMeta := &TableMetadata{
isDetailCaseSensitive: s.isDetailCaseSensitive,
- internalColumn: make(map[string]*storepb.ColumnMetadata),
+ internalColumn: make(map[string]*ColumnMetadata),
internalIndexes: make(map[string]*IndexMetadata),
proto: newTableProto,
}
@@ -750,21 +726,35 @@ func (s *SchemaMetadata) GetDependentViews(tableName string, columnName string)
return dependentViews
}
-func buildTablesMetadata(table *storepb.TableMetadata, isDetailCaseSensitive bool) ([]*TableMetadata, []string) {
+func buildTablesMetadata(table *storepb.TableMetadata, tableCatalog *storepb.TableCatalog, isDetailCaseSensitive bool) ([]*TableMetadata, []string) {
if table == nil {
return nil, nil
}
+
+ // Build a map of column catalogs
+ columnCatalogMap := make(map[string]*storepb.ColumnCatalog)
+ if tableCatalog != nil {
+ for _, columnCatalog := range tableCatalog.Columns {
+ columnCatalogMap[columnCatalog.Name] = columnCatalog
+ }
+ }
+
var result []*TableMetadata
var name []string
tableMetadata := &TableMetadata{
isDetailCaseSensitive: isDetailCaseSensitive,
- internalColumn: make(map[string]*storepb.ColumnMetadata),
+ internalColumn: make(map[string]*ColumnMetadata),
internalIndexes: make(map[string]*IndexMetadata),
proto: table,
+ config: tableCatalog,
}
for _, column := range table.Columns {
+ columnCatalog := columnCatalogMap[column.Name]
columnID := normalizeNameByCaseSensitivity(column.Name, isDetailCaseSensitive)
- tableMetadata.internalColumn[columnID] = column
+ tableMetadata.internalColumn[columnID] = &ColumnMetadata{
+ proto: column,
+ config: columnCatalog,
+ }
}
indexes := buildIndexesMetadata(table)
for _, index := range indexes {
@@ -775,7 +765,7 @@ func buildTablesMetadata(table *storepb.TableMetadata, isDetailCaseSensitive boo
name = append(name, table.Name)
if table.Partitions != nil {
- partitionTables, partitionNames := buildTablesMetadataRecursive(table.Columns, table.Partitions, tableMetadata, table, isDetailCaseSensitive)
+ partitionTables, partitionNames := buildTablesMetadataRecursive(table.Columns, columnCatalogMap, table.Partitions, tableMetadata, table, isDetailCaseSensitive)
result = append(result, partitionTables...)
name = append(name, partitionNames...)
}
@@ -801,7 +791,7 @@ func buildIndexesMetadata(table *storepb.TableMetadata) []*IndexMetadata {
// buildTablesMetadataRecursive builds the partition tables recursively,
// returns the table metadata and the partition names, the length of them must be the same.
-func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, partitions []*storepb.TablePartitionMetadata, root *TableMetadata, proto *storepb.TableMetadata, isDetailCaseSensitive bool) ([]*TableMetadata, []string) {
+func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, columnCatalogMap map[string]*storepb.ColumnCatalog, partitions []*storepb.TablePartitionMetadata, root *TableMetadata, proto *storepb.TableMetadata, isDetailCaseSensitive bool) ([]*TableMetadata, []string) {
if partitions == nil {
return nil, nil
}
@@ -812,17 +802,21 @@ func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, part
for _, partition := range partitions {
partitionMetadata := &TableMetadata{
partitionOf: root,
- internalColumn: make(map[string]*storepb.ColumnMetadata),
+ internalColumn: make(map[string]*ColumnMetadata),
proto: proto,
}
for _, column := range originalColumn {
+ columnCatalog := columnCatalogMap[column.Name]
columnID := normalizeNameByCaseSensitivity(column.Name, isDetailCaseSensitive)
- partitionMetadata.internalColumn[columnID] = column
+ partitionMetadata.internalColumn[columnID] = &ColumnMetadata{
+ proto: column,
+ config: columnCatalog,
+ }
}
tables = append(tables, partitionMetadata)
names = append(names, partition.Name)
if partition.Subpartitions != nil {
- subTables, subNames := buildTablesMetadataRecursive(originalColumn, partition.Subpartitions, partitionMetadata, proto, isDetailCaseSensitive)
+ subTables, subNames := buildTablesMetadataRecursive(originalColumn, columnCatalogMap, partition.Subpartitions, partitionMetadata, proto, isDetailCaseSensitive)
tables = append(tables, subTables...)
names = append(names, subNames...)
}
@@ -839,7 +833,7 @@ func (t *TableMetadata) GetTableComment() string {
}
// GetColumn gets the column by name.
-func (t *TableMetadata) GetColumn(name string) *storepb.ColumnMetadata {
+func (t *TableMetadata) GetColumn(name string) *ColumnMetadata {
if t == nil {
return nil
}
@@ -876,9 +870,13 @@ func (t *TableMetadata) GetProto() *storepb.TableMetadata {
return t.proto
}
+func (t *TableMetadata) GetCatalog() *storepb.TableCatalog {
+ return t.config
+}
+
// CreateColumn creates a new column in the table.
// Returns an error if the column already exists.
-func (t *TableMetadata) CreateColumn(columnProto *storepb.ColumnMetadata) error {
+func (t *TableMetadata) CreateColumn(columnProto *storepb.ColumnMetadata, columnCatalog *storepb.ColumnCatalog) error {
// Check if column already exists
if t.GetColumn(columnProto.Name) != nil {
return errors.Errorf("column %q already exists in table %q", columnProto.Name, t.proto.Name)
@@ -887,9 +885,12 @@ func (t *TableMetadata) CreateColumn(columnProto *storepb.ColumnMetadata) error
// Add to proto's column list
t.proto.Columns = append(t.proto.Columns, columnProto)
- // Add to internal map
+ // Create ColumnMetadata wrapper and add to internal map
columnID := normalizeNameByCaseSensitivity(columnProto.Name, t.isDetailCaseSensitive)
- t.internalColumn[columnID] = columnProto
+ t.internalColumn[columnID] = &ColumnMetadata{
+ proto: columnProto,
+ config: columnCatalog,
+ }
return nil
}
@@ -1046,7 +1047,7 @@ func (t *TableMetadata) RenameColumn(oldName string, newName string) error {
delete(t.internalColumn, oldColumnID)
// Update the column name in the proto
- oldColumn.Name = newName
+ oldColumn.proto.Name = newName
// Add back to internal map using new name
newColumnID := normalizeNameByCaseSensitivity(newName, t.isDetailCaseSensitive)
@@ -1088,6 +1089,14 @@ func (i *IndexMetadata) GetTableProto() *storepb.TableMetadata {
return i.tableProto
}
+func (c *ColumnMetadata) GetProto() *storepb.ColumnMetadata {
+ return c.proto
+}
+
+func (c *ColumnMetadata) GetCatalog() *storepb.ColumnCatalog {
+ return c.config
+}
+
// CreateIndex creates a new index in the table.
// Returns an error if the index already exists.
func (t *TableMetadata) CreateIndex(indexProto *storepb.IndexMetadata) error {
diff --git a/backend/store/model/database_test.go b/backend/store/model/database_test.go
index 1c39084a4b..a0c8d3776b 100644
--- a/backend/store/model/database_test.go
+++ b/backend/store/model/database_test.go
@@ -77,7 +77,7 @@ func TestBuildTablesMetadata(t *testing.T) {
a := require.New(t)
for _, tc := range testCases {
- tables, names := buildTablesMetadata(tc.input, true /* isDetailCaseSensitive */)
+ tables, names := buildTablesMetadata(tc.input, nil /* tableCatalog */, true /* isDetailCaseSensitive */)
// The length of the tables should be the same as the length of the names.
a.Equal(len(tables), len(names))
@@ -219,15 +219,15 @@ func TestTableMetadata_CreateColumn(t *testing.T) {
Type: "varchar",
Nullable: true,
}
- err := tableMeta.CreateColumn(columnProto)
+ err := tableMeta.CreateColumn(columnProto, nil /* columnCatalog */)
require.Nil(t, err)
// Verify column is now accessible
retrieved := tableMeta.GetColumn("email")
require.NotNil(t, retrieved)
- require.Equal(t, "email", retrieved.Name)
- require.Equal(t, "varchar", retrieved.Type)
+ require.Equal(t, "email", retrieved.GetProto().Name)
+ require.Equal(t, "varchar", retrieved.GetProto().Type)
}
func TestTableMetadata_CreateColumn_AlreadyExists(t *testing.T) {
@@ -257,7 +257,7 @@ func TestTableMetadata_CreateColumn_AlreadyExists(t *testing.T) {
Name: "id",
Type: "bigint",
}
- err := tableMeta.CreateColumn(columnProto)
+ err := tableMeta.CreateColumn(columnProto, nil /* columnCatalog */)
require.NotNil(t, err)
require.Contains(t, err.Error(), "already exists")
diff --git a/docs/plans/2025-11-16-consolidate-database-schema-models.md b/docs/plans/2025-11-16-consolidate-database-schema-models.md
deleted file mode 100644
index 8211d43d9b..0000000000
--- a/docs/plans/2025-11-16-consolidate-database-schema-models.md
+++ /dev/null
@@ -1,1094 +0,0 @@
-# Consolidate Database Schema Models Implementation Plan
-
-> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.
-
-**Goal:** Consolidate `DatabaseState` and `DatabaseSchema` by adding mutation methods directly to `model.DatabaseMetadata`, eliminating the need for a separate mutable wrapper and allowing all code to use a unified schema API.
-
-**Architecture:** Add mutation methods (CreateTable, DropTable, CreateColumn, etc.) directly to `model.DatabaseMetadata` and related types. Advisors will use `proto.Clone` to create separate origin (immutable) and final (mutable) instances of `DatabaseMetadata`. No separate `DatabaseState` wrapper needed - walk-through code mutates `DatabaseMetadata` directly.
-
-**Tech Stack:** Go, Protocol Buffers (storepb), ANTLR parsers (MySQL, PostgreSQL, TiDB)
-
----
-
-## Task 1: Add CreateTable Method to SchemaMetadata
-
-**Files:**
-- Modify: `backend/store/model/database.go` (add to SchemaMetadata)
-- Test: `backend/store/model/database_test.go`
-
-**Step 1: Write the failing test**
-
-Create or append to `backend/store/model/database_test.go`:
-
-```go
-package model
-
-import (
- "testing"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/plugin/advisor/code"
- "github.com/stretchr/testify/require"
-)
-
-func TestSchemaMetadata_CreateTable(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- },
- }
-
- schema := NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, true)
- dbMeta := schema.GetDatabaseMetadata()
- schemaMeta := dbMeta.GetSchema("public")
-
- // Create a new table
- tableMeta, err := schemaMeta.CreateTable("products")
-
- require.Nil(t, err)
- require.NotNil(t, tableMeta)
- require.Equal(t, "products", tableMeta.GetProto().Name)
-
- // Verify table is now accessible via GetTable
- retrieved := schemaMeta.GetTable("products")
- require.NotNil(t, retrieved)
- require.Equal(t, "products", retrieved.GetProto().Name)
-}
-
-func TestSchemaMetadata_CreateTable_AlreadyExists(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {Name: "users"},
- },
- },
- },
- }
-
- schema := NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, true)
- dbMeta := schema.GetDatabaseMetadata()
- schemaMeta := dbMeta.GetSchema("public")
-
- // Try to create table that already exists
- _, err := schemaMeta.CreateTable("users")
-
- require.NotNil(t, err)
- require.Contains(t, err.Error(), "already exists")
-}
-```
-
-**Step 2: Run test to verify it fails**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/store/model -run "TestSchemaMetadata_CreateTable"`
-
-Expected: FAIL with "schemaMeta.CreateTable undefined"
-
-**Step 3: Add CreateTable method to SchemaMetadata**
-
-Add to `backend/store/model/database.go` in the SchemaMetadata section:
-
-```go
-// CreateTable creates a new table in the schema.
-// Returns the created TableMetadata or an error if the table already exists.
-func (s *SchemaMetadata) CreateTable(tableName string) (*TableMetadata, error) {
- // Check if table already exists
- if s.GetTable(tableName) != nil {
- return nil, fmt.Errorf("table %q already exists in schema %q", tableName, s.proto.Name)
- }
-
- // Create new table proto
- newTableProto := &storepb.TableMetadata{
- Name: tableName,
- Columns: []*storepb.ColumnMetadata{},
- Indexes: []*storepb.IndexMetadata{},
- }
-
- // Add to proto's table list
- s.proto.Tables = append(s.proto.Tables, newTableProto)
-
- // Create TableMetadata wrapper
- tableMeta := &TableMetadata{
- isDetailCaseSensitive: s.isDetailCaseSensitive,
- internalColumn: make(map[string]*storepb.ColumnMetadata),
- internalIndexes: make(map[string]*IndexMetadata),
- columns: []*storepb.ColumnMetadata{},
- proto: newTableProto,
- }
-
- // Add to internal map
- var tableID string
- if s.isObjectCaseSensitive {
- tableID = tableName
- } else {
- tableID = strings.ToLower(tableName)
- }
- s.internalTables[tableID] = tableMeta
-
- return tableMeta, nil
-}
-```
-
-**Step 4: Run test to verify it passes**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/store/model -run "TestSchemaMetadata_CreateTable"`
-
-Expected: PASS
-
-**Step 5: Run linter**
-
-Run: `golangci-lint run --allow-parallel-runners backend/store/model/database.go`
-
-Expected: No issues
-
-**Step 6: Commit**
-
-```bash
-git add backend/store/model/database.go backend/store/model/database_test.go
-git commit -m "feat: add CreateTable method to SchemaMetadata"
-```
-
----
-
-## Task 2: Add DropTable Method to SchemaMetadata
-
-**Files:**
-- Modify: `backend/store/model/database.go`
-- Test: `backend/store/model/database_test.go`
-
-**Step 1: Write the failing test**
-
-Add to `backend/store/model/database_test.go`:
-
-```go
-func TestSchemaMetadata_DropTable(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {Name: "users"},
- {Name: "products"},
- },
- },
- },
- }
-
- schema := NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, true)
- dbMeta := schema.GetDatabaseMetadata()
- schemaMeta := dbMeta.GetSchema("public")
-
- // Drop table
- err := schemaMeta.DropTable("users")
-
- require.Nil(t, err)
-
- // Verify table is gone
- retrieved := schemaMeta.GetTable("users")
- require.Nil(t, retrieved)
-
- // Verify other table still exists
- retrieved = schemaMeta.GetTable("products")
- require.NotNil(t, retrieved)
-}
-
-func TestSchemaMetadata_DropTable_NotExists(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {Name: "public"},
- },
- }
-
- schema := NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, true)
- dbMeta := schema.GetDatabaseMetadata()
- schemaMeta := dbMeta.GetSchema("public")
-
- // Try to drop non-existent table
- err := schemaMeta.DropTable("nonexistent")
-
- require.NotNil(t, err)
- require.Contains(t, err.Error(), "does not exist")
-}
-```
-
-**Step 2: Run test to verify it fails**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/store/model -run "TestSchemaMetadata_DropTable"`
-
-Expected: FAIL with "schemaMeta.DropTable undefined"
-
-**Step 3: Add DropTable method**
-
-Add to `backend/store/model/database.go`:
-
-```go
-// DropTable drops a table from the schema.
-// Returns an error if the table does not exist.
-func (s *SchemaMetadata) DropTable(tableName string) error {
- // Check if table exists
- if s.GetTable(tableName) == nil {
- return fmt.Errorf("table %q does not exist in schema %q", tableName, s.proto.Name)
- }
-
- // Remove from internal map
- var tableID string
- if s.isObjectCaseSensitive {
- tableID = tableName
- } else {
- tableID = strings.ToLower(tableName)
- }
- delete(s.internalTables, tableID)
-
- // Remove from proto's table list
- newTables := make([]*storepb.TableMetadata, 0, len(s.proto.Tables)-1)
- for _, table := range s.proto.Tables {
- if s.isObjectCaseSensitive {
- if table.Name != tableName {
- newTables = append(newTables, table)
- }
- } else {
- if !strings.EqualFold(table.Name, tableName) {
- newTables = append(newTables, table)
- }
- }
- }
- s.proto.Tables = newTables
-
- return nil
-}
-```
-
-**Step 4: Run test to verify it passes**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/store/model -run "TestSchemaMetadata_DropTable"`
-
-Expected: PASS
-
-**Step 5: Run linter**
-
-Run: `golangci-lint run --allow-parallel-runners backend/store/model/database.go`
-
-Expected: No issues
-
-**Step 6: Commit**
-
-```bash
-git add backend/store/model/database.go backend/store/model/database_test.go
-git commit -m "feat: add DropTable method to SchemaMetadata"
-```
-
----
-
-## Task 3: Add CreateColumn Method to TableMetadata
-
-**Files:**
-- Modify: `backend/store/model/database.go`
-- Test: `backend/store/model/database_test.go`
-
-**Step 1: Write the failing test**
-
-Add to `backend/store/model/database_test.go`:
-
-```go
-func TestTableMetadata_CreateColumn(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "int"},
- },
- },
- },
- },
- },
- }
-
- schema := NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, true)
- dbMeta := schema.GetDatabaseMetadata()
- schemaMeta := dbMeta.GetSchema("public")
- tableMeta := schemaMeta.GetTable("users")
-
- // Create a new column
- columnProto := &storepb.ColumnMetadata{
- Name: "email",
- Type: "varchar",
- Nullable: true,
- }
- err := tableMeta.CreateColumn(columnProto)
-
- require.Nil(t, err)
-
- // Verify column is now accessible
- retrieved := tableMeta.GetColumn("email")
- require.NotNil(t, retrieved)
- require.Equal(t, "email", retrieved.Name)
- require.Equal(t, "varchar", retrieved.Type)
-}
-
-func TestTableMetadata_CreateColumn_AlreadyExists(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "int"},
- },
- },
- },
- },
- },
- }
-
- schema := NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, true)
- dbMeta := schema.GetDatabaseMetadata()
- schemaMeta := dbMeta.GetSchema("public")
- tableMeta := schemaMeta.GetTable("users")
-
- // Try to create column that already exists
- columnProto := &storepb.ColumnMetadata{
- Name: "id",
- Type: "bigint",
- }
- err := tableMeta.CreateColumn(columnProto)
-
- require.NotNil(t, err)
- require.Contains(t, err.Error(), "already exists")
-}
-```
-
-**Step 2: Run test to verify it fails**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/store/model -run "TestTableMetadata_CreateColumn"`
-
-Expected: FAIL with "tableMeta.CreateColumn undefined"
-
-**Step 3: Add CreateColumn method**
-
-Add to `backend/store/model/database.go`:
-
-```go
-// CreateColumn creates a new column in the table.
-// Returns an error if the column already exists.
-func (t *TableMetadata) CreateColumn(columnProto *storepb.ColumnMetadata) error {
- // Check if column already exists
- if t.GetColumn(columnProto.Name) != nil {
- return fmt.Errorf("column %q already exists in table %q", columnProto.Name, t.proto.Name)
- }
-
- // Add to proto's column list
- t.proto.Columns = append(t.proto.Columns, columnProto)
-
- // Add to internal map
- var columnID string
- if t.isDetailCaseSensitive {
- columnID = columnProto.Name
- } else {
- columnID = strings.ToLower(columnProto.Name)
- }
- t.internalColumn[columnID] = columnProto
-
- // Add to columns slice
- t.columns = append(t.columns, columnProto)
-
- return nil
-}
-```
-
-**Step 4: Run test to verify it passes**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/store/model -run "TestTableMetadata_CreateColumn"`
-
-Expected: PASS
-
-**Step 5: Run linter**
-
-Run: `golangci-lint run --allow-parallel-runners backend/store/model/database.go`
-
-Expected: No issues
-
-**Step 6: Commit**
-
-```bash
-git add backend/store/model/database.go backend/store/model/database_test.go
-git commit -m "feat: add CreateColumn method to TableMetadata"
-```
-
----
-
-## Task 4: Add DropColumn Method to TableMetadata
-
-**Files:**
-- Modify: `backend/store/model/database.go`
-- Test: `backend/store/model/database_test.go`
-
-**Step 1: Write the failing test**
-
-Add to `backend/store/model/database_test.go`:
-
-```go
-func TestTableMetadata_DropColumn(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {
- Name: "users",
- Columns: []*storepb.ColumnMetadata{
- {Name: "id", Type: "int"},
- {Name: "email", Type: "varchar"},
- {Name: "name", Type: "varchar"},
- },
- },
- },
- },
- },
- }
-
- schema := NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, true)
- dbMeta := schema.GetDatabaseMetadata()
- schemaMeta := dbMeta.GetSchema("public")
- tableMeta := schemaMeta.GetTable("users")
-
- // Drop column
- err := tableMeta.DropColumn("email")
-
- require.Nil(t, err)
-
- // Verify column is gone
- retrieved := tableMeta.GetColumn("email")
- require.Nil(t, retrieved)
-
- // Verify other columns still exist
- require.NotNil(t, tableMeta.GetColumn("id"))
- require.NotNil(t, tableMeta.GetColumn("name"))
-}
-
-func TestTableMetadata_DropColumn_NotExists(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {Name: "users"},
- },
- },
- },
- }
-
- schema := NewDatabaseSchema(metadata, nil, nil, storepb.Engine_POSTGRES, true)
- dbMeta := schema.GetDatabaseMetadata()
- schemaMeta := dbMeta.GetSchema("public")
- tableMeta := schemaMeta.GetTable("users")
-
- // Try to drop non-existent column
- err := tableMeta.DropColumn("nonexistent")
-
- require.NotNil(t, err)
- require.Contains(t, err.Error(), "does not exist")
-}
-```
-
-**Step 2: Run test to verify it fails**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/store/model -run "TestTableMetadata_DropColumn"`
-
-Expected: FAIL with "tableMeta.DropColumn undefined"
-
-**Step 3: Add DropColumn method**
-
-Add to `backend/store/model/database.go`:
-
-```go
-// DropColumn drops a column from the table.
-// Returns an error if the column does not exist.
-func (t *TableMetadata) DropColumn(columnName string) error {
- // Check if column exists
- if t.GetColumn(columnName) == nil {
- return fmt.Errorf("column %q does not exist in table %q", columnName, t.proto.Name)
- }
-
- // Remove from internal map
- var columnID string
- if t.isDetailCaseSensitive {
- columnID = columnName
- } else {
- columnID = strings.ToLower(columnName)
- }
- delete(t.internalColumn, columnID)
-
- // Remove from proto's column list
- newColumns := make([]*storepb.ColumnMetadata, 0, len(t.proto.Columns)-1)
- for _, column := range t.proto.Columns {
- if t.isDetailCaseSensitive {
- if column.Name != columnName {
- newColumns = append(newColumns, column)
- }
- } else {
- if !strings.EqualFold(column.Name, columnName) {
- newColumns = append(newColumns, column)
- }
- }
- }
- t.proto.Columns = newColumns
-
- // Rebuild columns slice
- t.columns = newColumns
-
- return nil
-}
-```
-
-**Step 4: Run test to verify it passes**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/store/model -run "TestTableMetadata_DropColumn"`
-
-Expected: PASS
-
-**Step 5: Run linter**
-
-Run: `golangci-lint run --allow-parallel-runners backend/store/model/database.go`
-
-Expected: No issues
-
-**Step 6: Commit**
-
-```bash
-git add backend/store/model/database.go backend/store/model/database_test.go
-git commit -m "feat: add DropColumn method to TableMetadata"
-```
-
----
-
-## Task 5: Update catalog.NewCatalog to Use Cloning
-
-**Files:**
-- Modify: `backend/plugin/advisor/catalog/catalog.go`
-- Test: `backend/plugin/advisor/catalog/catalog_test.go` (create if needed)
-
-**Step 1: Write the failing test**
-
-Create `backend/plugin/advisor/catalog/catalog_test.go`:
-
-```go
-package catalog
-
-import (
- "context"
- "testing"
-
- storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/store/model"
- "github.com/stretchr/testify/require"
-)
-
-func TestNewCatalogWithMetadata(t *testing.T) {
- metadata := &storepb.DatabaseSchemaMetadata{
- Name: "testdb",
- Schemas: []*storepb.SchemaMetadata{
- {
- Name: "public",
- Tables: []*storepb.TableMetadata{
- {Name: "users"},
- },
- },
- },
- }
-
- origin, final, err := NewCatalogWithMetadata(metadata, storepb.Engine_POSTGRES, true)
-
- require.Nil(t, err)
- require.NotNil(t, origin)
- require.NotNil(t, final)
-
- // Verify both have the table initially
- require.NotNil(t, origin.GetSchema("public").GetTable("users"))
- require.NotNil(t, final.GetSchema("public").GetTable("users"))
-
- // Mutate final
- err = final.GetSchema("public").CreateTable("products")
- require.Nil(t, err)
-
- // Verify origin is unchanged
- require.Nil(t, origin.GetSchema("public").GetTable("products"))
-
- // Verify final has the new table
- require.NotNil(t, final.GetSchema("public").GetTable("products"))
-}
-```
-
-**Step 2: Run test to verify it fails**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/plugin/advisor/catalog -run "TestNewCatalogWithMetadata"`
-
-Expected: FAIL with "undefined: NewCatalogWithMetadata"
-
-**Step 3: Add NewCatalogWithMetadata function**
-
-Modify `backend/plugin/advisor/catalog/catalog.go`:
-
-```go
-// NewCatalogWithMetadata creates origin and final database metadata from schema metadata.
-// Uses proto cloning to create independent copies.
-func NewCatalogWithMetadata(metadata *storepb.DatabaseSchemaMetadata, engineType storepb.Engine, isCaseSensitive bool) (origin *model.DatabaseMetadata, final *model.DatabaseMetadata, err error) {
- // Create origin from original metadata
- originSchema := model.NewDatabaseSchema(metadata, nil, nil, engineType, isCaseSensitive)
- origin = originSchema.GetDatabaseMetadata()
-
- // Clone metadata for final
- clonedMetadata := &storepb.DatabaseSchemaMetadata{}
- if err := clonedMetadata.UnmarshalVT(metadata.MarshalVT()); err != nil {
- return nil, nil, err
- }
-
- finalSchema := model.NewDatabaseSchema(clonedMetadata, nil, nil, engineType, isCaseSensitive)
- final = finalSchema.GetDatabaseMetadata()
-
- return origin, final, nil
-}
-```
-
-Update existing `NewCatalog` to use `NewCatalogWithMetadata`:
-
-```go
-// NewCatalog creates origin and final database catalog states.
-func NewCatalog(ctx context.Context, s *store.Store, instanceID, databaseName string, engineType storepb.Engine, isCaseSensitive bool, overrideDatabaseMetadata *storepb.DatabaseSchemaMetadata) (origin *model.DatabaseMetadata, final *model.DatabaseMetadata, err error) {
- var metadata *storepb.DatabaseSchemaMetadata
-
- if overrideDatabaseMetadata != nil {
- metadata = overrideDatabaseMetadata
- } else {
- databaseMeta, err := s.GetDBSchema(ctx, &store.FindDBSchemaMessage{
- InstanceID: instanceID,
- DatabaseName: databaseName,
- })
- if err != nil {
- return nil, nil, err
- }
- if databaseMeta == nil {
- return nil, nil, nil
- }
- metadata = databaseMeta.GetMetadata()
- }
-
- return NewCatalogWithMetadata(metadata, engineType, isCaseSensitive)
-}
-```
-
-**Step 4: Run test to verify it passes**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/plugin/advisor/catalog -run "TestNewCatalogWithMetadata"`
-
-Expected: PASS
-
-**Step 5: Run linter**
-
-Run: `golangci-lint run --allow-parallel-runners backend/plugin/advisor/catalog/`
-
-Expected: No issues
-
-**Step 6: Commit**
-
-```bash
-git add backend/plugin/advisor/catalog/catalog.go backend/plugin/advisor/catalog/catalog_test.go
-git commit -m "refactor: use DatabaseMetadata with cloning in NewCatalog"
-```
-
----
-
-## Task 6: Update Advisor Context to Use DatabaseMetadata
-
-**Files:**
-- Modify: `backend/plugin/advisor/advisor.go`
-- Test: Verify with existing advisor tests
-
-**Step 1: Update Context struct**
-
-In `backend/plugin/advisor/advisor.go`, change the Context struct:
-
-```go
-// Context is the context for advisor.
-type Context struct {
- DBSchema *storepb.DatabaseSchemaMetadata
- ChangeType storepb.PlanCheckRunConfig_ChangeDatabaseType
- EnablePriorBackup bool
- ClassificationConfig *storepb.DataClassificationSetting_DataClassificationConfig
- ListDatabaseNamesFunc base.ListDatabaseNamesFunc
- InstanceID string
- IsObjectCaseSensitive bool
-
- // SQL review rule special fields.
- AST any
- Rule *storepb.SQLReviewRule
-
- // CHANGED: Use model.DatabaseMetadata instead of catalog.DatabaseState
- OriginCatalog *model.DatabaseMetadata
- FinalCatalog *model.DatabaseMetadata
-
- Driver *sql.DB
-}
-```
-
-**Step 2: Update all references in advisor code**
-
-This will require searching for all usages of `OriginCatalog` and `FinalCatalog` throughout the advisor package and updating them to use `DatabaseMetadata` methods instead of `DatabaseState` methods.
-
-**Step 3: Run advisor tests**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/plugin/advisor/...`
-
-Expected: Tests may fail initially - we'll fix in next steps
-
-**Step 4: Commit**
-
-```bash
-git add backend/plugin/advisor/advisor.go
-git commit -m "refactor: change advisor Context to use DatabaseMetadata"
-```
-
----
-
-## Task 7: Migrate Walk-Through Code to Use DatabaseMetadata
-
-**Files:**
-- Modify: `backend/plugin/advisor/catalog/walk_through_for_mysql.go`
-- Modify: `backend/plugin/advisor/catalog/walk_through_for_pg.go`
-- Modify: `backend/plugin/advisor/catalog/walk_through_for_tidb.go`
-- Modify: `backend/plugin/advisor/catalog/walk_through.go`
-
-**Step 1: Update MySQLWalkThrough signature**
-
-Change walk-through functions to accept `*model.DatabaseMetadata`:
-
-```go
-// MySQLWalkThrough walks through MySQL AST and updates the database metadata.
-func MySQLWalkThrough(d *model.DatabaseMetadata, ast any) error {
- // Implementation...
-}
-```
-
-**Step 2: Update walk-through listener to use DatabaseMetadata**
-
-```go
-type mysqlListener struct {
- *mysql.BaseMySQLParserListener
-
- baseLine int
- lineNumber int
- text string
- databaseMetadata *model.DatabaseMetadata
- err error
-}
-```
-
-**Step 3: Replace DatabaseState method calls with DatabaseMetadata methods**
-
-For example, change:
-```go
-schema.CreateTable(tableName)
-```
-To use the new methods we added to `SchemaMetadata`.
-
-**Step 4: Run walk-through tests**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/plugin/advisor/catalog -run ".*WalkThrough"`
-
-Expected: PASS
-
-**Step 5: Commit**
-
-```bash
-git add backend/plugin/advisor/catalog/walk_through*.go
-git commit -m "refactor: migrate walk-through code to use DatabaseMetadata"
-```
-
----
-
-## Task 8: Remove DatabaseState
-
-**Files:**
-- Delete: `backend/plugin/advisor/catalog/state.go`
-- Update imports throughout advisor package
-
-**Step 1: Verify no references to DatabaseState remain**
-
-Run: `grep -r "DatabaseState" backend/plugin/advisor/`
-
-Expected: No results (or only in comments)
-
-**Step 2: Delete state.go**
-
-Run: `git rm backend/plugin/advisor/catalog/state.go`
-
-**Step 3: Run all tests**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/...`
-
-Expected: All tests PASS
-
-**Step 4: Commit**
-
-```bash
-git commit -m "refactor: remove DatabaseState (replaced by DatabaseMetadata)"
-```
-
----
-
-## Task 9: Document the New Architecture
-
-**Files:**
-- Create: `backend/store/model/README.md`
-- Update: `backend/plugin/advisor/catalog/README.md`
-
-**Step 1: Write model documentation**
-
-Create `backend/store/model/README.md`:
-
-```markdown
-# Database Model Architecture
-
-## Overview
-
-The `model` package provides the unified database schema representation used throughout Bytebase. Both read-only operations (querying schema) and mutation operations (walk-through simulation) use the same `DatabaseMetadata` API.
-
-## Core Types
-
-### DatabaseSchema
-
-Immutable wrapper around `DatabaseSchemaMetadata` proto.
-
-- Contains: metadata (proto), schema (raw dump), config
-- Created from storage via `store.GetDBSchema()`
-- Provides access to `DatabaseMetadata` via `GetDatabaseMetadata()`
-
-### DatabaseMetadata
-
-The main schema catalog with both read and mutation methods.
-
-**Read methods:**
-- `GetSchema(name)` - Get schema by name
-- `SearchTable(searchPath, name)` - Search with PostgreSQL search path
-- `SearchView(searchPath, name)` - Search view with search path
-- `GetOwner()`, `GetSearchPath()` - Database-level getters
-
-**Mutation methods:**
-(Added for advisor walk-through simulation)
-- Schema-level: Via `SchemaMetadata`
-- Table-level: Via `TableMetadata`
-
-### SchemaMetadata
-
-Schema-level catalog with table operations.
-
-**Read methods:**
-- `GetTable(name)` - Get table by name
-- `GetView(name)` - Get view by name
-- `ListTableNames()` - List all tables
-
-**Mutation methods:**
-- `CreateTable(name)` - Create new table
-- `DropTable(name)` - Drop existing table
-- `RenameTable(old, new)` - Rename table
-
-### TableMetadata
-
-Table-level catalog with column/index operations.
-
-**Read methods:**
-- `GetColumn(name)` - Get column by name
-- `GetIndex(name)` - Get index by name
-- `GetColumns()` - List all columns
-
-**Mutation methods:**
-- `CreateColumn(proto)` - Create new column
-- `DropColumn(name)` - Drop existing column
-- `CreateIndex(proto)` - Create new index
-- `DropIndex(name)` - Drop existing index
-
-## Usage Patterns
-
-### Read-Only (Query Schema)
-
-```go
-schema, _ := store.GetDBSchema(ctx, ...)
-dbMeta := schema.GetDatabaseMetadata()
-
-// Query schema
-table := dbMeta.GetSchema("public").GetTable("users")
-column := table.GetColumn("email")
-```
-
-### Mutation (Advisor Walk-Through)
-
-```go
-// Get base metadata
-metadata := baseSchema.GetMetadata()
-
-// Create two independent copies using proto.Clone
-origin, final, _ := catalog.NewCatalogWithMetadata(metadata, engine, caseSensitive)
-
-// origin = immutable snapshot
-// final = will be mutated during walk-through
-
-// Mutate final during SQL analysis
-final.GetSchema("public").CreateTable("products")
-final.GetSchema("public").DropTable("old_table")
-
-// Compare origin vs final to generate advisor recommendations
-```
-
-## Benefits
-
-1. **Unified API**: Same types for read and mutation operations
-2. **No duplication**: Single source of truth for schema structure
-3. **Simple cloning**: Use `proto.Clone` for independent copies
-4. **Type safety**: All methods strongly typed, errors returned explicitly
-5. **Maintainable**: One place to add new schema object types
-
-## Migration from DatabaseState
-
-Previously, advisors used `catalog.DatabaseState` (a separate mutable wrapper). This has been removed in favor of adding mutation methods directly to `DatabaseMetadata`.
-
-**Old:**
-```go
-origin := catalog.NewDatabaseState(metadata, ...)
-final := catalog.NewDatabaseState(metadata, ...)
-```
-
-**New:**
-```go
-origin, final, _ := catalog.NewCatalogWithMetadata(metadata, ...)
-```
-```
-
-**Step 2: Update advisor catalog README**
-
-Update `backend/plugin/advisor/catalog/README.md`:
-
-```markdown
-# Advisor Catalog
-
-## Overview
-
-The catalog package provides helper functions for creating database metadata catalogs for SQL advisors.
-
-## Key Functions
-
-### NewCatalog
-
-Creates origin and final database metadata from store.
-
-```go
-origin, final, err := catalog.NewCatalog(ctx, store, instanceID, dbName, engine, caseSensitive, override)
-```
-
-- Fetches current schema from storage (or uses override)
-- Returns two independent `DatabaseMetadata` instances
-- `origin` = immutable snapshot of current state
-- `final` = mutable copy for walk-through simulation
-
-### NewCatalogWithMetadata
-
-Creates origin and final database metadata from existing metadata proto.
-
-```go
-origin, final, err := catalog.NewCatalogWithMetadata(metadata, engine, caseSensitive)
-```
-
-Uses `proto.Clone` to create independent copies.
-
-## Walk-Through Functions
-
-### MySQLWalkThrough / PgWalkThrough / TiDBWalkThrough
-
-Walks through parsed SQL AST and mutates the final database metadata.
-
-```go
-err := catalog.MySQLWalkThrough(final, parsedAST)
-```
-
-These functions call mutation methods on `model.DatabaseMetadata` to simulate schema changes.
-
-## Architecture
-
-All catalog functionality delegates to `backend/store/model` types. This package provides advisor-specific helpers but does not define its own schema model types.
-
-See `backend/store/model/README.md` for details on the unified schema model.
-```
-
-**Step 3: Commit documentation**
-
-```bash
-git add backend/store/model/README.md backend/plugin/advisor/catalog/README.md
-git commit -m "docs: document unified DatabaseMetadata architecture"
-```
-
----
-
-## Task 10: Build and Verify
-
-**Files:**
-- Build: All backend code
-
-**Step 1: Build the backend**
-
-Run: `go build -ldflags "-w -s" -p=16 -o ./bytebase-build/bytebase ./backend/bin/server/main.go`
-
-Expected: SUCCESS (no build errors)
-
-**Step 2: Run full test suite**
-
-Run: `go test -v -count=1 github.com/bytebase/bytebase/backend/...`
-
-Expected: All tests PASS
-
-**Step 3: Run final linter check**
-
-Run: `golangci-lint run --allow-parallel-runners`
-
-Keep running until no issues remain (max-issues limit may require multiple runs)
-
-Expected: Clean output
-
-**Step 4: Final commit**
-
-```bash
-git add .
-git commit -m "chore: final cleanup and verification"
-```
-
----
-
-## Summary
-
-This plan consolidates database schema models by:
-
-1. **Adding mutation methods to `model.DatabaseMetadata`** and related types
-2. **Using `proto.Clone`** to create independent origin/final catalogs
-3. **Removing `catalog.DatabaseState`** entirely
-4. **Updating advisors** to work directly with `DatabaseMetadata`
-5. **Migrating walk-through code** to use the new mutation methods
-
-The result: A single unified schema model (`DatabaseMetadata`) used by all code, with both read and mutation capabilities.
-
-**Key architectural principles:**
-- DRY: Single schema model, not duplicated
-- Simple: proto.Clone for independent copies
-- Unified: Same API for read and mutation
-- Type-safe: Explicit error returns