Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
refactor: consolidate DatabaseState and DatabaseSchema models (#18110)
Danny Xu2cd41c98c1a5
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.goindex dc941c663a..4b13e8b575 100644--- a/backend/api/v1/release_service_check.go+++ b/backend/api/v1/release_service_check.go@@ -161,7 +161,18 @@ loop:}engine := instance.Metadata.GetEngine()- originCatalog, finalCatalog, err := catalog.NewCatalog(ctx, s.store, database.InstanceID, database.DatabaseName, engine, store.IsObjectCaseSensitive(instance), nil)+ // Get the database metadata+ dbSchema, err := s.store.GetDBSchema(ctx, &store.FindDBSchemaMessage{+ InstanceID: database.InstanceID,+ DatabaseName: database.DatabaseName,+ })+ if err != nil {+ return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get database schema"))+ }+ if dbSchema == nil {+ return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("database schema not found for %s", database.DatabaseName))+ }+ originCatalog, finalCatalog, err := catalog.NewCatalogWithMetadata(dbSchema.GetMetadata(), engine, store.IsObjectCaseSensitive(instance))if err != nil {return nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to create catalog"))}@@ -603,7 +614,7 @@ func (s *ReleaseService) checkReleaseDeclarative(ctx context.Context, files []*vfunc (s *ReleaseService) runSQLReviewCheckForFile(ctx context.Context,- originCatalog *catalog.DatabaseState,+ originCatalog *model.DatabaseMetadata,finalCatalog *catalog.DatabaseState,instance *store.InstanceMessage,database *store.DatabaseMessage,@@ -652,7 +663,7 @@ func (s *ReleaseService) runSQLReviewCheckForFile(ChangeType: changeType,DBSchema: dbMetadata,DBType: instance.Metadata.GetEngine(),- OriginCatalog: originCatalog,+ OriginalMetadata: originCatalog,FinalCatalog: finalCatalog,Driver: connection,CurrentDatabase: database.DatabaseName,diff --git a/backend/plugin/advisor/advisor.go b/backend/plugin/advisor/advisor.goindex 8cdbe1180f..8b23571d4f 100644--- a/backend/plugin/advisor/advisor.go+++ b/backend/plugin/advisor/advisor.go@@ -15,6 +15,7 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/store/model")const (@@ -45,11 +46,11 @@ type Context struct {IsObjectCaseSensitive bool// SQL review rule special fields.- AST any- Rule *storepb.SQLReviewRule- OriginCatalog *catalog.DatabaseState- FinalCatalog *catalog.DatabaseState- Driver *sql.DB+ AST any+ Rule *storepb.SQLReviewRule+ OriginalMetadata *model.DatabaseMetadata+ FinalCatalog *catalog.DatabaseState+ Driver *sql.DB// CurrentDatabase is the current database.CurrentDatabase stringdiff --git a/backend/plugin/advisor/catalog/catalog.go b/backend/plugin/advisor/catalog/catalog.goindex 33a1daabe6..4c0a9a4593 100644--- a/backend/plugin/advisor/catalog/catalog.go+++ b/backend/plugin/advisor/catalog/catalog.go@@ -2,30 +2,24 @@package catalogimport (- "context"+ "google.golang.org/protobuf/proto"storepb "github.com/bytebase/bytebase/backend/generated-go/store"- "github.com/bytebase/bytebase/backend/store"+ "github.com/bytebase/bytebase/backend/store/model")-// 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 *DatabaseState, final *DatabaseState, err error) {- dbMetadata := overrideDatabaseMetadata- if dbMetadata == nil {- 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- }- dbMetadata = databaseMeta.GetMetadata()- }- ignoreCaseSensitive := !isCaseSensitive- origin = NewDatabaseState(dbMetadata, ignoreCaseSensitive, engineType)- final = NewDatabaseState(dbMetadata, ignoreCaseSensitive, engineType)- return origin, final, nil+// NewCatalogWithMetadata creates original and final database catalogs from schema metadata.+// OriginalMetadata is DatabaseMetadata (read-only), FinalCatalog is DatabaseState (mutable for walk-through).+func NewCatalogWithMetadata(metadata *storepb.DatabaseSchemaMetadata, engineType storepb.Engine, isCaseSensitive bool) (originalMetadata *model.DatabaseMetadata, final *DatabaseState, err error) {+ // Create original metadata from original metadata as DatabaseMetadata (read-only)+ originalSchema := model.NewDatabaseSchema(metadata, nil, nil, engineType, isCaseSensitive)+ originalMetadata = originalSchema.GetDatabaseMetadata()++ // Clone metadata for final to avoid modifying the original+ clonedMetadata := proto.CloneOf(metadata)++ // Create final as DatabaseState (mutable for walk-through)+ final = NewDatabaseState(clonedMetadata, !isCaseSensitive, engineType)++ return originalMetadata, final, nil}diff --git a/backend/plugin/advisor/catalog/catalog_test.go b/backend/plugin/advisor/catalog/catalog_test.gonew file mode 100644index 0000000000..5923cf190c--- /dev/null+++ b/backend/plugin/advisor/catalog/catalog_test.go@@ -0,0 +1,49 @@+package catalog++import (+ "testing"++ "github.com/stretchr/testify/require"++ storepb "github.com/bytebase/bytebase/backend/generated-go/store"+)++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"))+ finalSchema, err := final.GetSchema("public")+ require.Nil(t, err)+ finalTable, err := finalSchema.GetTable("users")+ require.Nil(t, err)+ require.NotNil(t, finalTable)++ // Mutate final - DatabaseState CreateTable returns (*TableState, *WalkThroughError)+ _, walkErr := finalSchema.CreateTable("products")+ require.Nil(t, walkErr)++ // Verify origin is unchanged (DatabaseMetadata)+ require.Nil(t, origin.GetSchema("public").GetTable("products"))++ // Verify final has the new table (DatabaseState)+ newTable, walkErr := finalSchema.GetTable("products")+ require.Nil(t, walkErr)+ require.NotNil(t, newTable)+}diff --git a/backend/plugin/advisor/catalog/state.go b/backend/plugin/advisor/catalog/state.goindex 1e1dc4f37d..b597f78e27 100644--- a/backend/plugin/advisor/catalog/state.go+++ b/backend/plugin/advisor/catalog/state.go@@ -1,8 +1,16 @@package catalog-// This file defines the database state during walk-through. The only two cases will use database state:-// 1. during walk-through-// 2. the underlying implementation of Finder+// This file defines the internal database state used during walk-through.+//+// INTERNAL USE ONLY: DatabaseState is an internal implementation detail of the catalog package.+// External code should use model.DatabaseMetadata instead.+//+// DatabaseState is used internally for:+// 1. Walk-through implementations (MySQLWalkThrough, PgWalkThrough, TiDBWalkThrough)+// 2. Advisor rules that need mutable catalog operations+//+// The public API uses model.DatabaseMetadata, which provides a cleaner interface+// and better integration with the rest of the codebase.import ("fmt"diff --git a/backend/plugin/advisor/catalog/walk_through.go b/backend/plugin/advisor/catalog/walk_through.goindex 4fde6e32fd..ade3687870 100644--- a/backend/plugin/advisor/catalog/walk_through.go+++ b/backend/plugin/advisor/catalog/walk_through.go@@ -92,7 +92,7 @@ func (e *WalkThroughError) Error() string {return e.Content}-// WalkThrough will collect the catalog schema in the databaseState as it walks through the stmt.+// WalkThrough will collect the catalog schema in the database state as it walks through the stmt.func WalkThrough(d *DatabaseState, ast any) error {switch d.dbType {case storepb.Engine_TIDB:diff --git a/backend/plugin/advisor/catalog/walk_through_test.go b/backend/plugin/advisor/catalog/walk_through_test.goindex b01a45fc50..91a59361bb 100644--- a/backend/plugin/advisor/catalog/walk_through_test.go+++ b/backend/plugin/advisor/catalog/walk_through_test.go@@ -176,13 +176,16 @@ func runWalkThroughTest(t *testing.T, file string, engineType storepb.Engine, orsm := sheet.NewManager(nil)for _, test := range tests {- var state *DatabaseState+ var proto *storepb.DatabaseSchemaMetadataif originDatabase != nil {- state = NewDatabaseState(originDatabase, test.IgnoreCaseSensitive, engineType)+ proto = originDatabase} else {- state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, test.IgnoreCaseSensitive, engineType)+ proto = &storepb.DatabaseSchemaMetadata{}}+ // Create DatabaseState for walk-through+ state := NewDatabaseState(proto, test.IgnoreCaseSensitive, engineType)+asts, _ := sm.GetASTsForChecks(engineType, test.Statement)err := WalkThrough(state, asts)if err != nil {@@ -215,13 +218,16 @@ func runANTLRWalkThroughTest(t *testing.T, file string, engineType storepb.Enginrequire.NoError(t, err)for _, test := range tests {- var state *DatabaseState+ var proto *storepb.DatabaseSchemaMetadataif originDatabase != nil {- state = NewDatabaseState(originDatabase, test.IgnoreCaseSensitive, engineType)+ proto = originDatabase} else {- state = NewDatabaseState(&storepb.DatabaseSchemaMetadata{}, test.IgnoreCaseSensitive, engineType)+ proto = &storepb.DatabaseSchemaMetadata{}}+ // Create DatabaseState for walk-through+ state := NewDatabaseState(proto, test.IgnoreCaseSensitive, engineType)+// Parse using ANTLR parser instead of legacy parserparseResult, parseErr := pgparser.ParsePostgreSQL(test.Statement)if parseErr != nil {diff --git a/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go b/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.goindex d9142336d7..594579aa8b 100644--- a/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go+++ b/backend/plugin/advisor/mysql/rule_column_disallow_changing_type.go@@ -15,8 +15,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -46,7 +46,7 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(_ context.Context, checkCtx advi}// Create the rule- rule := NewColumnDisallowChangingTypeRule(level, string(checkCtx.Rule.Type), checkCtx.OriginCatalog)+ rule := NewColumnDisallowChangingTypeRule(level, string(checkCtx.Rule.Type), checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -64,11 +64,11 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(_ context.Context, checkCtx advitype ColumnDisallowChangingTypeRule struct {BaseRuletext string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// NewColumnDisallowChangingTypeRule creates a new ColumnDisallowChangingTypeRule.-func NewColumnDisallowChangingTypeRule(level storepb.Advice_Status, title string, originCatalog *catalog.DatabaseState) *ColumnDisallowChangingTypeRule {+func NewColumnDisallowChangingTypeRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *ColumnDisallowChangingTypeRule {return &ColumnDisallowChangingTypeRule{BaseRule: BaseRule{level: level,@@ -176,7 +176,7 @@ func (r *ColumnDisallowChangingTypeRule) changeColumnType(tableName, columnNamereturn}- if normalizeColumnType(column.Type()) != normalizeColumnType(tp) {+ if normalizeColumnType(column.Type) != normalizeColumnType(tp) {r.AddAdvice(&storepb.Advice{Status: r.level,Code: code.ChangeColumnType.Int32(),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.goindex 65606592ca..70d78a3aef 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@@ -13,8 +13,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -44,7 +44,7 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(_ context.Context, checkCtx advis}// Create the rule- rule := NewColumnDisallowDropInIndexRule(level, string(checkCtx.Rule.Type), checkCtx.OriginCatalog)+ rule := NewColumnDisallowDropInIndexRule(level, string(checkCtx.Rule.Type), checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -62,11 +62,11 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(_ context.Context, checkCtx advistype ColumnDisallowDropInIndexRule struct {BaseRuletables tableState- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// NewColumnDisallowDropInIndexRule creates a new ColumnDisallowDropInIndexRule.-func NewColumnDisallowDropInIndexRule(level storepb.Advice_Status, title string, originCatalog *catalog.DatabaseState) *ColumnDisallowDropInIndexRule {+func NewColumnDisallowDropInIndexRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *ColumnDisallowDropInIndexRule {return &ColumnDisallowDropInIndexRule{BaseRule: BaseRule{level: level,@@ -164,7 +164,7 @@ func (r *ColumnDisallowDropInIndexRule) checkAlterTable(ctx *mysql.AlterTableConif r.tables[tableName] == nil {r.tables[tableName] = make(columnSet)}- for _, indexColumn := range *index {+ for _, indexColumn := range index {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.goindex 32d3884da9..dbfbda466e 100644--- a/backend/plugin/advisor/mysql/rule_column_no_null.go+++ b/backend/plugin/advisor/mysql/rule_column_no_null.go@@ -135,7 +135,8 @@ func (r *ColumnNoNullRule) generateAdvice() {})for _, column := range columnList {- col := r.finalCatalog.GetColumn("", column.tableName, column.columnName)+ dbState := r.finalCatalog+ col := dbState.GetColumn("", column.tableName, column.columnName)if col != nil && col.Nullable() {r.AddAdvice(&storepb.Advice{Status: r.level,diff --git a/backend/plugin/advisor/mysql/rule_database_drop_empty_db.go b/backend/plugin/advisor/mysql/rule_database_drop_empty_db.goindex 228d1c1e41..d01cab2e40 100644--- a/backend/plugin/advisor/mysql/rule_database_drop_empty_db.go+++ b/backend/plugin/advisor/mysql/rule_database_drop_empty_db.go@@ -13,8 +13,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -44,7 +44,7 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(_ context.Context, checkCtx adviso}// Create the rule- rule := NewDatabaseDropEmptyDBRule(level, string(checkCtx.Rule.Type), checkCtx.OriginCatalog)+ rule := NewDatabaseDropEmptyDBRule(level, string(checkCtx.Rule.Type), checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -61,11 +61,11 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(_ context.Context, checkCtx adviso// DatabaseDropEmptyDBRule checks for drop database only if empty.type DatabaseDropEmptyDBRule struct {BaseRule- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// NewDatabaseDropEmptyDBRule creates a new DatabaseDropEmptyDBRule.-func NewDatabaseDropEmptyDBRule(level storepb.Advice_Status, title string, originCatalog *catalog.DatabaseState) *DatabaseDropEmptyDBRule {+func NewDatabaseDropEmptyDBRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *DatabaseDropEmptyDBRule {return &DatabaseDropEmptyDBRule{BaseRule: BaseRule{level: level,diff --git a/backend/plugin/advisor/mysql/rule_index_pk_type.go b/backend/plugin/advisor/mysql/rule_index_pk_type.goindex d41d8ba5b8..94a324823a 100644--- a/backend/plugin/advisor/mysql/rule_index_pk_type.go+++ b/backend/plugin/advisor/mysql/rule_index_pk_type.go@@ -14,8 +14,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -45,7 +45,7 @@ func (*IndexPkTypeAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([}// Create the rule- rule := NewIndexPkTypeRule(level, string(checkCtx.Rule.Type), checkCtx.OriginCatalog)+ rule := NewIndexPkTypeRule(level, string(checkCtx.Rule.Type), checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -63,12 +63,12 @@ func (*IndexPkTypeAdvisor) Check(_ context.Context, checkCtx advisor.Context) ([type IndexPkTypeRule struct {BaseRuleline map[string]int- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatatablesNewColumns tableColumnTypes}// NewIndexPkTypeRule creates a new IndexPkTypeRule.-func NewIndexPkTypeRule(level storepb.Advice_Status, title string, originCatalog *catalog.DatabaseState) *IndexPkTypeRule {+func NewIndexPkTypeRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *IndexPkTypeRule {return &IndexPkTypeRule{BaseRule: BaseRule{level: level,@@ -240,7 +240,7 @@ func (r *IndexPkTypeRule) getPKColumnType(tableName string, columnName string) (}column := r.originCatalog.GetColumn("", tableName, columnName)if column != nil {- return column.Type(), nil+ return column.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.goindex 5ac678661d..7ba99df599 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@@ -14,8 +14,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -52,7 +52,7 @@ func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx ad}// Create the rule- rule := NewIndexPrimaryKeyTypeAllowlistRule(level, string(checkCtx.Rule.Type), allowlist, checkCtx.OriginCatalog)+ rule := NewIndexPrimaryKeyTypeAllowlistRule(level, string(checkCtx.Rule.Type), allowlist, checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -70,12 +70,12 @@ func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx adtype IndexPrimaryKeyTypeAllowlistRule struct {BaseRuleallowlist map[string]bool- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatatablesNewColumns tableColumnTypes}// NewIndexPrimaryKeyTypeAllowlistRule creates a new IndexPrimaryKeyTypeAllowlistRule.-func NewIndexPrimaryKeyTypeAllowlistRule(level storepb.Advice_Status, title string, allowlist map[string]bool, originCatalog *catalog.DatabaseState) *IndexPrimaryKeyTypeAllowlistRule {+func NewIndexPrimaryKeyTypeAllowlistRule(level storepb.Advice_Status, title string, allowlist map[string]bool, originCatalog *model.DatabaseMetadata) *IndexPrimaryKeyTypeAllowlistRule {return &IndexPrimaryKeyTypeAllowlistRule{BaseRule: BaseRule{level: level,@@ -254,7 +254,7 @@ func (r *IndexPrimaryKeyTypeAllowlistRule) getPKColumnType(tableName string, col}column := r.originCatalog.GetColumn("", tableName, columnName)if column != nil {- return column.Type(), nil+ return column.Type, nil}return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)}diff --git a/backend/plugin/advisor/mysql/rule_index_total_number_limit.go b/backend/plugin/advisor/mysql/rule_index_total_number_limit.goindex 77c4d51f78..af874730d1 100644--- a/backend/plugin/advisor/mysql/rule_index_total_number_limit.go+++ b/backend/plugin/advisor/mysql/rule_index_total_number_limit.go@@ -132,7 +132,8 @@ func (r *IndexTotalNumberLimitRule) generateAdvice() []*storepb.Advice {})for _, table := range tableList {- tableInfo := r.finalCatalog.GetTable("", table.name)+ dbState := r.finalCatalog+ tableInfo := dbState.GetTable("", table.name)if tableInfo != nil && tableInfo.CountIndex() > r.max {r.AddAdvice(&storepb.Advice{Status: r.level,diff --git a/backend/plugin/advisor/mysql/rule_index_type_no_blob.go b/backend/plugin/advisor/mysql/rule_index_type_no_blob.goindex 753c41ce29..efa1d5ef70 100644--- a/backend/plugin/advisor/mysql/rule_index_type_no_blob.go+++ b/backend/plugin/advisor/mysql/rule_index_type_no_blob.go@@ -14,8 +14,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -45,7 +45,7 @@ func (*IndexTypeNoBlobAdvisor) Check(_ context.Context, checkCtx advisor.Context}// Create the rule- rule := NewIndexTypeNoBlobRule(level, string(checkCtx.Rule.Type), checkCtx.OriginCatalog)+ rule := NewIndexTypeNoBlobRule(level, string(checkCtx.Rule.Type), checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -62,12 +62,12 @@ func (*IndexTypeNoBlobAdvisor) Check(_ context.Context, checkCtx advisor.Context// IndexTypeNoBlobRule checks for index type no blob.type IndexTypeNoBlobRule struct {BaseRule- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatatablesNewColumns tableColumnTypes}// NewIndexTypeNoBlobRule creates a new IndexTypeNoBlobRule.-func NewIndexTypeNoBlobRule(level storepb.Advice_Status, title string, originCatalog *catalog.DatabaseState) *IndexTypeNoBlobRule {+func NewIndexTypeNoBlobRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *IndexTypeNoBlobRule {return &IndexTypeNoBlobRule{BaseRule: BaseRule{level: level,@@ -297,7 +297,7 @@ func (r *IndexTypeNoBlobRule) getColumnType(tableName string, columnName string)}column := r.originCatalog.GetColumn("", tableName, columnName)if column != nil {- return column.Type(), nil+ return column.Type, nil}return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)}diff --git a/backend/plugin/advisor/mysql/rule_naming_index_convention.go b/backend/plugin/advisor/mysql/rule_naming_index_convention.goindex 46b1258a53..d2cbd1588e 100644--- a/backend/plugin/advisor/mysql/rule_naming_index_convention.go+++ b/backend/plugin/advisor/mysql/rule_naming_index_convention.go@@ -12,9 +12,9 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -57,7 +57,7 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.C}// Create the rule- rule := NewNamingIndexConventionRule(level, string(checkCtx.Rule.Type), format, maxLength, templateList, checkCtx.OriginCatalog)+ rule := NewNamingIndexConventionRule(level, string(checkCtx.Rule.Type), format, maxLength, templateList, checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -78,11 +78,11 @@ type NamingIndexConventionRule struct {format stringmaxLength inttemplateList []string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// NewNamingIndexConventionRule creates a new NamingIndexConventionRule.-func NewNamingIndexConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originCatalog *catalog.DatabaseState) *NamingIndexConventionRule {+func NewNamingIndexConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originCatalog *model.DatabaseMetadata) *NamingIndexConventionRule {return &NamingIndexConventionRule{BaseRule: BaseRule{level: level,diff --git a/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go b/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.goindex cc71d6822e..e67968a5ab 100644--- a/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go+++ b/backend/plugin/advisor/mysql/rule_naming_unique_key_convention.go@@ -12,9 +12,9 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -49,7 +49,7 @@ func (*NamingUKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cont}// Create the rule- rule := NewNamingUKConventionRule(level, string(checkCtx.Rule.Type), format, maxLength, templateList, checkCtx.OriginCatalog)+ rule := NewNamingUKConventionRule(level, string(checkCtx.Rule.Type), format, maxLength, templateList, checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -78,11 +78,11 @@ type NamingUKConventionRule struct {format stringmaxLength inttemplateList []string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// NewNamingUKConventionRule creates a new NamingUKConventionRule.-func NewNamingUKConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originCatalog *catalog.DatabaseState) *NamingUKConventionRule {+func NewNamingUKConventionRule(level storepb.Advice_Status, title string, format string, maxLength int, templateList []string, originCatalog *model.DatabaseMetadata) *NamingUKConventionRule {return &NamingUKConventionRule{BaseRule: BaseRule{level: level,@@ -192,7 +192,7 @@ func (r *NamingUKConventionRule) checkAlterTable(ctx *mysql.AlterTableContext) {if indexStateMap == nil {continue}- indexState, ok := (*indexStateMap)[oldIndexName]+ indexState, ok := indexStateMap[oldIndexName]if !ok {continue}diff --git a/backend/plugin/advisor/mysql/rule_table_require_pk.go b/backend/plugin/advisor/mysql/rule_table_require_pk.goindex 2b3bc7517b..1fb458c449 100644--- a/backend/plugin/advisor/mysql/rule_table_require_pk.go+++ b/backend/plugin/advisor/mysql/rule_table_require_pk.go@@ -13,9 +13,9 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"mysqlparser "github.com/bytebase/bytebase/backend/plugin/parser/mysql"+ "github.com/bytebase/bytebase/backend/store/model")const (@@ -49,7 +49,7 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)}// Create the rule- rule := NewTableRequirePKRule(level, string(checkCtx.Rule.Type), checkCtx.OriginCatalog)+ rule := NewTableRequirePKRule(level, string(checkCtx.Rule.Type), checkCtx.OriginalMetadata)// Create the generic checker with the rulechecker := NewGenericChecker([]Rule{rule})@@ -71,11 +71,11 @@ type TableRequirePKRule struct {BaseRuletables map[string]columnSetline map[string]int- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// NewTableRequirePKRule creates a new TableRequirePKRule.-func NewTableRequirePKRule(level storepb.Advice_Status, title string, originCatalog *catalog.DatabaseState) *TableRequirePKRule {+func NewTableRequirePKRule(level storepb.Advice_Status, title string, originCatalog *model.DatabaseMetadata) *TableRequirePKRule {return &TableRequirePKRule{BaseRule: BaseRule{level: level,diff --git a/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go b/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.goindex b21a93707d..e496f9b2a5 100644--- a/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go+++ b/backend/plugin/advisor/mysql/rule_table_text_fields_total_length.go@@ -116,7 +116,8 @@ func (r *TableTextFieldsTotalLengthRule) checkCreateTable(ctx *mysql.CreateTableif tableName == "" {return}- tableInfo := r.finalCatalog.GetTable("", tableName)+ dbState := r.finalCatalog+ tableInfo := dbState.GetTable("", tableName)if tableInfo == nil {return}@@ -150,7 +151,8 @@ func (r *TableTextFieldsTotalLengthRule) checkAlterTable(ctx *mysql.AlterTableCoif tableName == "" {return}- tableInfo := r.finalCatalog.GetTable("", tableName)+ dbState := r.finalCatalog+ tableInfo := dbState.GetTable("", tableName)if tableInfo == nil {return}diff --git a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.goindex 7799f63c62..0edccd86f2 100644--- a/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go+++ b/backend/plugin/advisor/pg/advisor_builtin_prior_backup_check.go@@ -67,7 +67,7 @@ func (*BuiltinPriorBackupCheckAdvisor) Check(_ context.Context, checkCtx advisor// Check if backup schema existsschemaName := common.BackupDatabaseNameOfEngine(storepb.Engine_POSTGRES)- if !checkCtx.OriginCatalog.HasSchema(schemaName) {+ if checkCtx.OriginalMetadata.GetSchema(schemaName) == nil {adviceList = append(adviceList, &storepb.Advice{Status: level,Title: title,diff --git a/backend/plugin/advisor/pg/advisor_column_no_null.go b/backend/plugin/advisor/pg/advisor_column_no_null.goindex cd256d4265..36f6df5c1b 100644--- a/backend/plugin/advisor/pg/advisor_column_no_null.go+++ b/backend/plugin/advisor/pg/advisor_column_no_null.go@@ -11,9 +11,9 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code""github.com/bytebase/bytebase/backend/plugin/parser/pg"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -45,7 +45,7 @@ func (*ColumnNoNullAdvisor) Check(_ context.Context, checkCtx advisor.Context) (level: level,title: string(checkCtx.Rule.Type),},- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,nullableColumns: make(columnMap),}@@ -74,7 +74,7 @@ type columnMap map[columnName]inttype columnNoNullRule struct {BaseRule- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatanullableColumns columnMap}diff --git a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go b/backend/plugin/advisor/pg/advisor_index_total_number_limit.goindex 1c3f7650f5..8c6ac5a192 100644--- a/backend/plugin/advisor/pg/advisor_index_total_number_limit.go+++ b/backend/plugin/advisor/pg/advisor_index_total_number_limit.go@@ -122,7 +122,14 @@ func (r *indexTotalNumberLimitRule) generateAdvice() {})for _, table := range tableList {- tableInfo := r.finalCatalog.GetTable(table.schema, table.table)+ schema, err := r.finalCatalog.GetSchema(table.schema)+ if err != nil {+ continue+ }+ tableInfo, err := schema.GetTable(table.table)+ if err != nil {+ continue+ }if tableInfo != nil && tableInfo.CountIndex() > r.max {r.AddAdvice(&storepb.Advice{Status: r.level,diff --git a/backend/plugin/advisor/pg/advisor_naming_index_convention.go b/backend/plugin/advisor/pg/advisor_naming_index_convention.goindex af56fb3b88..a1f26530f9 100644--- a/backend/plugin/advisor/pg/advisor_naming_index_convention.go+++ b/backend/plugin/advisor/pg/advisor_naming_index_convention.go@@ -11,9 +11,9 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -53,7 +53,7 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cformat: format,maxLength: maxLength,templateList: templateList,- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,}checker := NewGenericChecker([]Rule{rule})@@ -68,7 +68,7 @@ type namingIndexConventionRule struct {format stringmaxLength inttemplateList []string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// Name returns the rule name.@@ -233,7 +233,7 @@ 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, *catalog.IndexState) {+func (r *namingIndexConventionRule) findIndex(schemaName string, tableName string, indexName string) (string, *model.IndexMetadata) {if r.originCatalog == nil {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.goindex f7a290dc48..6d50a5dc5d 100644--- a/backend/plugin/advisor/pg/advisor_naming_primary_key_convention.go+++ b/backend/plugin/advisor/pg/advisor_naming_primary_key_convention.go@@ -11,9 +11,9 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -53,7 +53,7 @@ func (*NamingPKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Contformat: format,maxLength: maxLength,templateList: templateList,- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,statementsText: checkCtx.Statements,}@@ -70,7 +70,7 @@ type namingPKConventionRule struct {format stringmaxLength inttemplateList []string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatastatementsText string}diff --git a/backend/plugin/advisor/pg/advisor_naming_unique_key_convention.go b/backend/plugin/advisor/pg/advisor_naming_unique_key_convention.goindex 04d8b2f58e..b1b8f73959 100644--- a/backend/plugin/advisor/pg/advisor_naming_unique_key_convention.go+++ b/backend/plugin/advisor/pg/advisor_naming_unique_key_convention.go@@ -11,9 +11,9 @@ import (storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"pgparser "github.com/bytebase/bytebase/backend/plugin/parser/pg"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -53,7 +53,7 @@ func (*NamingUKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Contformat: format,maxLength: maxLength,templateList: templateList,- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,}checker := NewGenericChecker([]Rule{rule})@@ -68,7 +68,7 @@ type namingUKConventionRule struct {format stringmaxLength inttemplateList []string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}//nolint:unused@@ -407,7 +407,7 @@ 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, *catalog.IndexState) {+func (r *namingUKConventionRule) findIndex(schemaName string, tableName string, indexName string) (string, *model.IndexMetadata) {if r.originCatalog == nil {return "", nil}diff --git a/backend/plugin/advisor/pg/test_helper.go b/backend/plugin/advisor/pg/test_helper.goindex 359893bbe3..c1ce651835 100644--- a/backend/plugin/advisor/pg/test_helper.go+++ b/backend/plugin/advisor/pg/test_helper.go@@ -16,6 +16,7 @@ import ("github.com/bytebase/bytebase/backend/plugin/advisor""github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/parser/pg"+ "github.com/bytebase/bytebase/backend/store/model")// TestCase is the data struct for test.@@ -58,9 +59,12 @@ func RunANTLRAdvisorRuleTest(t *testing.T, rule advisor.SQLReviewRuleType, dbTypt.Fatalf("%s doesn't have mocked metadata support", storepb.Engine_name[int32(dbType)])}- database := advisor.MockPostgreSQLDatabase- originCatalog := catalog.NewDatabaseState(database, false /* ignoreCaseSensitive */, dbType)- finalCatalog := catalog.NewDatabaseState(database, false /* ignoreCaseSensitive */, dbType)+ // Create OriginalMetadata as DatabaseMetadata (read-only)+ originalSchema := model.NewDatabaseSchema(schemaMetadata, nil, nil, dbType, true /* isCaseSensitive for PostgreSQL */)+ originalMetadata := originalSchema.GetDatabaseMetadata()++ // Create FinalCatalog as DatabaseState (mutable for walk-through)+ finalCatalog := catalog.NewDatabaseState(schemaMetadata, false /* ignoreCaseSensitive */, dbType)// Get default payload, or use empty string for test-only rulespayload, err := advisor.SetDefaultSQLReviewRulePayload(rule, dbType)@@ -97,7 +101,7 @@ func RunANTLRAdvisorRuleTest(t *testing.T, rule advisor.SQLReviewRuleType, dbTypAST: tree, // Pass ANTLR parse resultStatements: tc.Statement,Rule: ruleList[0],- OriginCatalog: originCatalog,+ OriginalMetadata: originalMetadata,FinalCatalog: finalCatalog,Driver: nil,CurrentDatabase: "TEST_DB",diff --git a/backend/plugin/advisor/sql_review.go b/backend/plugin/advisor/sql_review.goindex cff44d76bd..30d8196fbe 100644--- a/backend/plugin/advisor/sql_review.go+++ b/backend/plugin/advisor/sql_review.go@@ -14,6 +14,7 @@ import ("github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code""github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/backend/store/model")// How to add a SQL review rule:@@ -491,7 +492,7 @@ type SQLReviewCheckContext struct {ChangeType storepb.PlanCheckRunConfig_ChangeDatabaseTypeDBSchema *storepb.DatabaseSchemaMetadataDBType storepb.Engine- OriginCatalog *catalog.DatabaseState+ OriginalMetadata *model.DatabaseMetadataFinalCatalog *catalog.DatabaseStateDriver *sql.DBEnablePriorBackup bool@@ -561,7 +562,7 @@ func SQLReviewCheck(AST: asts,Statements: statements,Rule: rule,- OriginCatalog: checkContext.OriginCatalog,+ OriginalMetadata: checkContext.OriginalMetadata,FinalCatalog: checkContext.FinalCatalog,Driver: checkContext.Driver,CurrentDatabase: checkContext.CurrentDatabase,diff --git a/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go b/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.goindex 04a80cebde..d5b97bfbba 100644--- a/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go+++ b/backend/plugin/advisor/tidb/advisor_column_disallow_changing_type.go@@ -15,7 +15,7 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -45,7 +45,7 @@ func (*ColumnDisallowChangingTypeAdvisor) Check(_ context.Context, checkCtx advichecker := &columnDisallowChangingTypeChecker{level: level,title: string(checkCtx.Rule.Type),- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,}for _, stmt := range stmtList {@@ -63,7 +63,7 @@ type columnDisallowChangingTypeChecker struct {title stringtext stringline int- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// Enter implements the ast.Visitor interface.@@ -137,5 +137,5 @@ func (checker *columnDisallowChangingTypeChecker) changeColumnType(tableName strreturn false}- return normalizeColumnType(column.Type()) != normalizeColumnType(newType)+ return normalizeColumnType(column.Type) != normalizeColumnType(newType)}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.goindex 6b5290641c..c889e77db7 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@@ -14,7 +14,7 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -46,7 +46,7 @@ func (*ColumnDisallowDropInIndexAdvisor) Check(_ context.Context, checkCtx advislevel: level,title: string(checkCtx.Rule.Type),tables: make(tableState),- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,}for _, stmt := range stmtList {@@ -64,7 +64,7 @@ type columnDisallowDropInIndexChecker struct {title stringtext stringtables tableState // the variable mean whether the column in index.- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadataline int}@@ -88,13 +88,13 @@ func (checker *columnDisallowDropInIndexChecker) dropColumn(in ast.Node) (ast.Noif spec.Tp == ast.AlterTableDropColumn {table := node.Table.Name.O- index := checker.originCatalog.Index("", table)+ tableMetadata := checker.originCatalog.GetTable("", table)- if index != nil {+ if tableMetadata != nil {if checker.tables[table] == nil {checker.tables[table] = make(columnSet)}- for _, indexColumn := range *index {+ for _, indexColumn := range tableMetadata.ListIndexes() {for _, column := range indexColumn.ExpressionList() {checker.tables[table][column] = true}diff --git a/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go b/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.goindex 999469cb55..475978548d 100644--- a/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go+++ b/backend/plugin/advisor/tidb/advisor_database_drop_empty_db.go@@ -12,7 +12,7 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -43,7 +43,7 @@ func (*DatabaseAllowDropIfEmptyAdvisor) Check(_ context.Context, checkCtx advisochecker := &allowDropEmptyDBChecker{level: level,title: string(checkCtx.Rule.Type),- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,}for _, stmtNode := range root {(stmtNode).Accept(checker)@@ -56,7 +56,7 @@ type allowDropEmptyDBChecker struct {adviceList []*storepb.Advicelevel storepb.Advice_Statustitle string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// Enter implements the ast.Visitor interface.diff --git a/backend/plugin/advisor/tidb/advisor_index_pk_type.go b/backend/plugin/advisor/tidb/advisor_index_pk_type.goindex 18e40f4a46..e4360e1a84 100644--- a/backend/plugin/advisor/tidb/advisor_index_pk_type.go+++ b/backend/plugin/advisor/tidb/advisor_index_pk_type.go@@ -16,7 +16,7 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -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.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,tablesNewColumns: make(map[string]columnNameToColumnDef),}@@ -88,7 +88,7 @@ type indexPkTypeChecker struct {level storepb.Advice_Statustitle stringline map[string]int- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatatablesNewColumns tableNewColumn}@@ -222,7 +222,7 @@ func (v *indexPkTypeChecker) getPKColumnType(tableName string, columnName string}column := v.originCatalog.GetColumn("", tableName, columnName)if column != nil {- return column.Type(), nil+ return column.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.goindex 02f3654cd0..245c0b121c 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@@ -15,8 +15,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"tidbparser "github.com/bytebase/bytebase/backend/plugin/parser/tidb"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -55,7 +55,7 @@ func (*IndexPrimaryKeyTypeAllowlistAdvisor) Check(_ context.Context, checkCtx adlevel: level,title: string(checkCtx.Rule.Type),allowlist: allowlist,- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,tablesNewColumns: make(map[string]columnNameToColumnDef),}@@ -75,7 +75,7 @@ type indexPrimaryKeyTypeAllowlistChecker struct {text stringline intallowlist map[string]bool- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatatablesNewColumns tableNewColumn}@@ -203,7 +203,7 @@ func (v *indexPrimaryKeyTypeAllowlistChecker) getPKColumnType(tableName string,}column := v.originCatalog.GetColumn("", tableName, columnName)if column != nil {- return column.Type(), nil+ return column.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.goindex a31df370bf..4676940c1c 100644--- a/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go+++ b/backend/plugin/advisor/tidb/advisor_index_type_no_blob.go@@ -17,7 +17,7 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -47,7 +47,7 @@ func (*IndexTypeNoBlobAdvisor) Check(_ context.Context, checkCtx advisor.Contextchecker := &indexTypeNoBlobChecker{level: level,title: string(checkCtx.Rule.Type),- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,tablesNewColumns: make(map[string]columnNameToColumnDef),}@@ -66,7 +66,7 @@ type indexTypeNoBlobChecker struct {title stringtext stringline int- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatatablesNewColumns tableNewColumn}@@ -225,7 +225,7 @@ func (v *indexTypeNoBlobChecker) getColumnType(tableName string, columnName stri}column := v.originCatalog.GetColumn("", tableName, columnName)if column != nil {- return column.Type(), nil+ return column.Type, nil}return "", errors.Errorf("cannot find the type of `%s`.`%s`", tableName, columnName)}diff --git a/backend/plugin/advisor/tidb/advisor_naming_index_convention.go b/backend/plugin/advisor/tidb/advisor_naming_index_convention.goindex 9245554c6e..9512a13782 100644--- a/backend/plugin/advisor/tidb/advisor_naming_index_convention.go+++ b/backend/plugin/advisor/tidb/advisor_naming_index_convention.go@@ -12,8 +12,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -51,7 +51,7 @@ func (*NamingIndexConventionAdvisor) Check(_ context.Context, checkCtx advisor.Cformat: format,maxLength: maxLength,templateList: templateList,- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,}for _, stmtNode := range root {(stmtNode).Accept(checker)@@ -67,7 +67,7 @@ type namingIndexConventionChecker struct {format stringmaxLength inttemplateList []string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// Enter implements the ast.Visitor interface.diff --git a/backend/plugin/advisor/tidb/advisor_naming_unique_key_convention.go b/backend/plugin/advisor/tidb/advisor_naming_unique_key_convention.goindex 700015d88c..8dd3ea5bf6 100644--- a/backend/plugin/advisor/tidb/advisor_naming_unique_key_convention.go+++ b/backend/plugin/advisor/tidb/advisor_naming_unique_key_convention.go@@ -11,8 +11,8 @@ import ("github.com/bytebase/bytebase/backend/common"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/advisor"- "github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -50,7 +50,7 @@ func (*NamingUKConventionAdvisor) Check(_ context.Context, checkCtx advisor.Contformat: format,maxLength: maxLength,templateList: templateList,- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,}for _, stmtNode := range root {(stmtNode).Accept(checker)@@ -66,7 +66,7 @@ type namingUKConventionChecker struct {format stringmaxLength inttemplateList []string- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadata}// Enter implements the ast.Visitor interface.diff --git a/backend/plugin/advisor/tidb/advisor_table_require_pk.go b/backend/plugin/advisor/tidb/advisor_table_require_pk.goindex 6530280c98..c4053da1d9 100644--- a/backend/plugin/advisor/tidb/advisor_table_require_pk.go+++ b/backend/plugin/advisor/tidb/advisor_table_require_pk.go@@ -13,6 +13,7 @@ import ("github.com/bytebase/bytebase/backend/plugin/advisor""github.com/bytebase/bytebase/backend/plugin/advisor/catalog""github.com/bytebase/bytebase/backend/plugin/advisor/code"+ "github.com/bytebase/bytebase/backend/store/model")const (@@ -48,7 +49,7 @@ func (*TableRequirePKAdvisor) Check(_ context.Context, checkCtx advisor.Context)title: string(checkCtx.Rule.Type),tables: make(tablePK),line: make(map[string]int),- originCatalog: checkCtx.OriginCatalog,+ originCatalog: checkCtx.OriginalMetadata,finalCatalog: checkCtx.FinalCatalog,}@@ -65,7 +66,7 @@ type tableRequirePKChecker struct {title stringtables tablePKline map[string]int- originCatalog *catalog.DatabaseState+ originCatalog *model.DatabaseMetadatafinalCatalog *catalog.DatabaseState}@@ -182,13 +183,11 @@ func (v *tableRequirePKChecker) createTableLike(node *ast.CreateTableStmt) {}v.tables[table] = newColumnSet(columns)} else {- referTableState := v.originCatalog.GetTable("", referTableName)- if referTableState != nil {- indexSet := referTableState.Index()- for _, index := range *indexSet {- if index.Primary() {- v.tables[table] = newColumnSet(index.ExpressionList())- }+ referTableMetadata := v.originCatalog.GetTable("", referTableName)+ if referTableMetadata != nil {+ primaryKey := referTableMetadata.GetPrimaryKey()+ if primaryKey != nil {+ v.tables[table] = newColumnSet(primaryKey.ExpressionList())}}}diff --git a/backend/plugin/advisor/utils_for_tests.go b/backend/plugin/advisor/utils_for_tests.goindex 53d711f75a..5e8864051c 100644--- a/backend/plugin/advisor/utils_for_tests.go+++ b/backend/plugin/advisor/utils_for_tests.go@@ -21,6 +21,7 @@ import (v1pb "github.com/bytebase/bytebase/backend/generated-go/v1""github.com/bytebase/bytebase/backend/plugin/advisor/catalog"database "github.com/bytebase/bytebase/backend/plugin/db"+ "github.com/bytebase/bytebase/backend/store/model")var (@@ -196,12 +197,27 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.E}}- database := MockMySQLDatabase+ // Use the schemaMetadata if available, otherwise use mock database for catalog creation+ catalogMetadata := schemaMetadata+ if catalogMetadata == nil {+ if dbType == storepb.Engine_POSTGRES {+ catalogMetadata = MockPostgreSQLDatabase+ } else {+ catalogMetadata = MockMySQLDatabase+ }+ }++ isCaseSensitive := falseif dbType == storepb.Engine_POSTGRES {- database = MockPostgreSQLDatabase+ isCaseSensitive = true}- originCatalog := catalog.NewDatabaseState(database, false /* ignoreCaseSensitive */, dbType)- finalCatalog := catalog.NewDatabaseState(database, false /* ignoreCaseSensitive */, dbType)++ // Create OriginalMetadata as DatabaseMetadata (read-only)+ originalSchema := model.NewDatabaseSchema(catalogMetadata, nil, nil, dbType, isCaseSensitive)+ originalMetadata := originalSchema.GetDatabaseMetadata()++ // Create FinalCatalog as DatabaseState (mutable for walk-through)+ finalCatalog := catalog.NewDatabaseState(catalogMetadata, !isCaseSensitive, dbType)payload, err := SetDefaultSQLReviewRulePayload(rule, dbType)require.NoError(t, err)@@ -218,7 +234,7 @@ func RunSQLReviewRuleTest(t *testing.T, rule SQLReviewRuleType, dbType storepb.ECharset: "",Collation: "",DBType: dbType,- OriginCatalog: originCatalog,+ OriginalMetadata: originalMetadata,FinalCatalog: finalCatalog,Driver: nil,CurrentDatabase: curDB,diff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.goindex 1b341d7df8..a68b2517e6 100644--- a/backend/runner/plancheck/statement_advise_executor.go+++ b/backend/runner/plancheck/statement_advise_executor.go@@ -149,7 +149,7 @@ func (e *StatementAdviseExecutor) runReview(}}- originCatalog, finalCatalog, err := catalog.NewCatalog(ctx, e.store, database.InstanceID, database.DatabaseName, instance.Metadata.GetEngine(), store.IsObjectCaseSensitive(instance), nil /* Override Metadata */)+ originCatalog, finalCatalog, err := catalog.NewCatalogWithMetadata(dbSchema.GetMetadata(), instance.Metadata.GetEngine(), store.IsObjectCaseSensitive(instance))if err != nil {return nil, common.Wrapf(err, common.Internal, "failed to create a catalog")}@@ -176,7 +176,7 @@ func (e *StatementAdviseExecutor) runReview(DBSchema: dbSchema.GetMetadata(),ChangeType: changeType,DBType: instance.Metadata.GetEngine(),- OriginCatalog: originCatalog,+ OriginalMetadata: originCatalog,FinalCatalog: finalCatalog,Driver: connection,EnablePriorBackup: enablePriorBackup,diff --git a/backend/store/model/database.go b/backend/store/model/database.goindex 5516c55252..9cc18da73e 100644--- a/backend/store/model/database.go+++ b/backend/store/model/database.go@@ -6,6 +6,8 @@ import ("slices""strings"+ "github.com/pkg/errors"+storepb "github.com/bytebase/bytebase/backend/generated-go/store")@@ -231,6 +233,7 @@ func (t *TableConfig) IsEmpty() bool {// DatabaseMetadata is the metadata for a database.type DatabaseMetadata struct {+ proto *storepb.DatabaseSchemaMetadataname stringowner stringsearchPath []string@@ -243,6 +246,7 @@ type DatabaseMetadata struct {// NewDatabaseMetadata creates a new database metadata.func NewDatabaseMetadata(metadata *storepb.DatabaseSchemaMetadata, isObjectCaseSensitive bool, isDetailCaseSensitive bool) *DatabaseMetadata {databaseMetadata := &DatabaseMetadata{+ proto: metadata,name: metadata.Name,owner: metadata.Owner,searchPath: NormalizeSearchPath(metadata.SearchPath),@@ -466,6 +470,104 @@ 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 {+ return ""+ }+ return d.proto.Name+}++// HasNoTable returns true if the database has no tables.+func (d *DatabaseMetadata) HasNoTable() bool {+ for _, schema := range d.internal {+ if schema != nil && schema.proto != nil && len(schema.proto.Tables) > 0 {+ return false+ }+ }+ return true+}+func (d *DatabaseMetadata) SearchSequence(searchPath []string, name string) (string, *SequenceMetadata) {// Search in the search path first.for _, schemaName := range searchPath {@@ -563,6 +665,21 @@ func (d *DatabaseMetadata) GetSearchPath() []string {return d.searchPath}+// GetProto returns the underlying proto for this database metadata.+func (d *DatabaseMetadata) GetProto() *storepb.DatabaseSchemaMetadata {+ return d.proto+}++// GetIsObjectCaseSensitive returns whether object names (schemas, tables) are case-sensitive.+func (d *DatabaseMetadata) GetIsObjectCaseSensitive() bool {+ return d.isObjectCaseSensitive+}++// GetIsDetailCaseSensitive returns whether detail names (columns, indexes) are case-sensitive.+func (d *DatabaseMetadata) GetIsDetailCaseSensitive() bool {+ return d.isDetailCaseSensitive+}+// LinkedDatabaseMetadata is the metadata for a linked database.type LinkedDatabaseMetadata struct {name string@@ -801,6 +918,80 @@ func (s *SchemaMetadata) ListMaterializedViewNames() []string {return result}+// 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, errors.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+}++// 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 errors.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+}+func buildTablesMetadata(table *storepb.TableMetadata, isDetailCaseSensitive bool) ([]*TableMetadata, []string) {if table == nil {return nil, nil@@ -971,6 +1162,70 @@ func (t *TableMetadata) GetProto() *storepb.TableMetadata {return t.proto}+// 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 errors.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+}++// 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 errors.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+}+// ExternalTableMetadata is the metadata for a external table.type ExternalTableMetadata struct {isDetailCaseSensitive bool@@ -1012,6 +1267,30 @@ func (i *IndexMetadata) GetTableProto() *storepb.TableMetadata {return i.tableProto}+// Primary returns true if the index is a primary key.+func (i *IndexMetadata) Primary() bool {+ if i.proto == nil {+ return false+ }+ return i.proto.Primary+}++// Unique returns true if the index is unique.+func (i *IndexMetadata) Unique() bool {+ if i.proto == nil {+ return false+ }+ return i.proto.Unique+}++// ExpressionList returns the list of expressions/columns in the index.+func (i *IndexMetadata) ExpressionList() []string {+ if i.proto == nil {+ return nil+ }+ return i.proto.Expressions+}+// ViewMetadata is the metadata for a view.type ViewMetadata struct {Definition stringdiff --git a/backend/store/model/database_test.go b/backend/store/model/database_test.goindex 671d88c93b..48f2bbfe78 100644--- a/backend/store/model/database_test.go+++ b/backend/store/model/database_test.go@@ -94,3 +94,241 @@ func TestBuildTablesMetadata(t *testing.T) {}}}++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")+}++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")+}++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")+}++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")+}diff --git a/docs/plans/2025-11-16-consolidate-database-schema-models.md b/docs/plans/2025-11-16-consolidate-database-schema-models.mdnew file mode 100644index 0000000000..8211d43d9b--- /dev/null+++ b/docs/plans/2025-11-16-consolidate-database-schema-models.md@@ -0,0 +1,1094 @@+# 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