Skip to content

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

refactor: use storepb.FunctionMetadata and improve PG function comparison (#18143)

Danny Xu3fb91d66c03d
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index d9f2417104..95809a4178 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -617,7 +617,7 @@ func (s *DatabaseService) GetDatabaseMetadata(ctx context.Context, req *connect.
if err != nil {
return nil, err
}
- v1pbMetadata := convertStoreDatabaseMetadata(dbMetadata.GetMetadata(), filter, int(req.Msg.Limit))
+ v1pbMetadata := convertStoreDatabaseMetadata(dbMetadata.GetProto(), filter, int(req.Msg.Limit))
v1pbMetadata.Name = fmt.Sprintf("%s%s/%s%s%s", common.InstanceNamePrefix, database.InstanceID, common.DatabaseIDPrefix, database.DatabaseName, common.MetadataSuffix)
return connect.NewResponse(v1pbMetadata), nil
@@ -670,7 +670,7 @@ func (s *DatabaseService) GetDatabaseSchema(ctx context.Context, req *connect.Re
}
dbMetadata = newDBSchema
}
- schemaString := string(dbMetadata.GetSchema())
+ schemaString := string(dbMetadata.GetRawDump())
return connect.NewResponse(&v1pb.DatabaseSchema{Schema: schemaString}), nil
}
@@ -725,7 +725,7 @@ func (s *DatabaseService) GetDatabaseSDLSchema(ctx context.Context, req *connect
dbMetadata = newDBSchema
}
- metadata := dbMetadata.GetMetadata()
+ metadata := dbMetadata.GetProto()
if metadata == nil {
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("database metadata not found for database %q", databaseName))
}
@@ -1128,7 +1128,7 @@ func (s *DatabaseService) GetSchemaString(ctx context.Context, req *connect.Requ
}
return connect.NewResponse(&v1pb.GetSchemaStringResponse{SchemaString: s}), nil
case v1pb.GetSchemaStringRequest_DATABASE:
- metadata := dbMetadata.GetMetadata()
+ metadata := dbMetadata.GetProto()
s, err := schema.GetDatabaseDefinition(instance.Metadata.Engine, schema.GetDefinitionContext{
SkipBackupSchema: false,
PrintHeader: false,
@@ -1204,7 +1204,7 @@ func (s *DatabaseService) GetSchemaString(ctx context.Context, req *connect.Requ
if functionMetadata == nil {
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("function %q not found", req.Msg.Object))
}
- s, err := schema.GetFunctionDefinition(instance.Metadata.Engine, req.Msg.Schema, functionMetadata.GetProto())
+ s, err := schema.GetFunctionDefinition(instance.Metadata.Engine, req.Msg.Schema, functionMetadata)
if err != nil {
return nil, connect.NewError(connect.CodeInternal, errors.Errorf("Failed to get function schema: %v", err))
}
diff --git a/backend/api/v1/release_service_check.go b/backend/api/v1/release_service_check.go
index a648e13601..7db262cb3c 100644
--- a/backend/api/v1/release_service_check.go
+++ b/backend/api/v1/release_service_check.go
@@ -174,10 +174,10 @@ loop:
return nil, connect.NewError(connect.CodeNotFound, errors.Errorf("database schema not found for %s", database.DatabaseName))
}
// Create original metadata as read-only
- originMetadata := model.NewDatabaseMetadata(dbMetadata.GetMetadata(), nil, nil, engine, store.IsObjectCaseSensitive(instance))
+ originMetadata := model.NewDatabaseMetadata(dbMetadata.GetProto(), nil, nil, engine, store.IsObjectCaseSensitive(instance))
// Clone metadata for final to avoid modifying the original
- clonedMetadata, ok := proto.Clone(dbMetadata.GetMetadata()).(*storepb.DatabaseSchemaMetadata)
+ clonedMetadata, ok := proto.Clone(dbMetadata.GetProto()).(*storepb.DatabaseSchemaMetadata)
if !ok {
return nil, connect.NewError(connect.CodeInternal, errors.New("failed to clone database schema metadata"))
}
@@ -650,7 +650,7 @@ func (s *ReleaseService) runSQLReviewCheckForFile(
}
}
- dbMetadataProto := dbMetadata.GetMetadata()
+ dbMetadataProto := dbMetadata.GetProto()
useDatabaseOwner, err := getUseDatabaseOwner(ctx, s.store, instance, database)
if err != nil {
return storepb.Advice_ERROR, nil, connect.NewError(connect.CodeInternal, errors.Wrapf(err, "failed to get use database owner"))
diff --git a/backend/component/export/resources.go b/backend/component/export/resources.go
index 85b5376a64..179d8506a2 100644
--- a/backend/component/export/resources.go
+++ b/backend/component/export/resources.go
@@ -112,7 +112,7 @@ func getResourcesForMySQL(
Table: sourceColumn.Table,
LinkedServer: sourceColumn.Server,
}
- if sourceColumn.Database != dbMetadata.GetMetadata().Name {
+ if sourceColumn.Database != dbMetadata.GetProto().Name {
resourceDB, err := storeInstance.GetDatabaseV2(ctx, &store.FindDatabaseMessage{
InstanceID: &instance.ResourceID,
DatabaseName: &sourceColumn.Database,
@@ -198,7 +198,7 @@ func getResourcesForPostgres(
LinkedServer: sourceColumn.Server,
}
- if sourceColumn.Database != dbMetadata.GetMetadata().Name {
+ if sourceColumn.Database != dbMetadata.GetProto().Name {
continue
}
diff --git a/backend/plugin/advisor/mysql/rule_online_migration.go b/backend/plugin/advisor/mysql/rule_online_migration.go
index df7f7fb0aa..f9c0e922de 100644
--- a/backend/plugin/advisor/mysql/rule_online_migration.go
+++ b/backend/plugin/advisor/mysql/rule_online_migration.go
@@ -204,7 +204,7 @@ func (r *OnlineMigrationRule) exitAlterStatement(ctx *mysql.AlterStatementContex
for _, resource := range r.changedResources {
var tableRows int64
- for _, table := range r.dbMetadata.GetMetadata().GetSchemas()[0].GetTables() {
+ for _, table := range r.dbMetadata.GetProto().GetSchemas()[0].GetTables() {
if table.GetName() == resource.Table {
tableRows = table.GetRowCount()
break
diff --git a/backend/plugin/advisor/pg/advisor_statement_object_owner_check.go b/backend/plugin/advisor/pg/advisor_statement_object_owner_check.go
index 272c426097..e2e5663a33 100644
--- a/backend/plugin/advisor/pg/advisor_statement_object_owner_check.go
+++ b/backend/plugin/advisor/pg/advisor_statement_object_owner_check.go
@@ -149,7 +149,7 @@ func (r *statementObjectOwnerCheckRule) checkSchemaOwnership(schemaName string,
owner := schemaMeta.GetOwner()
if owner == pgDatabaseOwner {
- owner = r.dbMetadata.GetOwner()
+ owner = r.dbMetadata.GetProto().GetOwner()
}
if owner != r.currentRole {
r.AddAdvice(&storepb.Advice{
@@ -182,7 +182,7 @@ func (r *statementObjectOwnerCheckRule) checkTableOwnership(schemaName, tableNam
owner := tableMeta.GetOwner()
if owner == pgDatabaseOwner {
- owner = r.dbMetadata.GetOwner()
+ owner = r.dbMetadata.GetProto().GetOwner()
}
if owner != r.currentRole {
r.AddAdvice(&storepb.Advice{
@@ -307,12 +307,12 @@ func (r *statementObjectOwnerCheckRule) handleCreateschemastmt(ctx *parser.Creat
return
}
- owner := r.dbMetadata.GetOwner()
+ owner := r.dbMetadata.GetProto().GetOwner()
if owner != r.currentRole {
r.AddAdvice(&storepb.Advice{
Status: r.level,
Title: r.title,
- Content: fmt.Sprintf("Database \"%s\" is owned by \"%s\", but the current role is \"%s\".", r.dbMetadata.GetName(), owner, r.currentRole),
+ Content: fmt.Sprintf("Database \"%s\" is owned by \"%s\", but the current role is \"%s\".", r.dbMetadata.GetProto().GetName(), owner, r.currentRole),
Code: code.StatementObjectOwnerCheck.Int32(),
StartPosition: &storepb.Position{
Line: int32(ctx.GetStart().GetLine()),
diff --git a/backend/plugin/parser/mysql/query_span_extractor.go b/backend/plugin/parser/mysql/query_span_extractor.go
index bcbe6c789c..153e86caa8 100644
--- a/backend/plugin/parser/mysql/query_span_extractor.go
+++ b/backend/plugin/parser/mysql/query_span_extractor.go
@@ -1379,7 +1379,7 @@ func (q *querySpanExtractor) findTableSchema(databaseName, tableName string) (ba
return &base.PhysicalTable{
Name: tableSchema.GetProto().Name,
Schema: emptySchema,
- Database: dbMetadata.GetName(),
+ Database: dbMetadata.GetProto().GetName(),
Server: "",
Columns: columnNames,
}, nil
@@ -1404,7 +1404,7 @@ func (q *querySpanExtractor) findTableSchema(databaseName, tableName string) (ba
return &base.PhysicalView{
Name: viewSchema.GetProto().Name,
Schema: emptySchema,
- Database: dbMetadata.GetName(),
+ Database: dbMetadata.GetProto().GetName(),
Server: "",
Columns: columns,
}, nil
diff --git a/backend/plugin/parser/pg/query_span_extractor.go b/backend/plugin/parser/pg/query_span_extractor.go
index 89840ea050..5eeb6b28ac 100644
--- a/backend/plugin/parser/pg/query_span_extractor.go
+++ b/backend/plugin/parser/pg/query_span_extractor.go
@@ -9,6 +9,7 @@ import (
"github.com/antlr4-go/antlr/v4"
"github.com/pkg/errors"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
parsererror "github.com/bytebase/bytebase/backend/plugin/parser/errors"
"github.com/bytebase/parser/postgresql"
@@ -235,7 +236,7 @@ func (q *querySpanExtractor) getQuerySpan(ctx context.Context, stmt string) (*ba
type functionDefinition struct {
schemaName string
- metadata *model.FunctionMetadata
+ metadata *storepb.FunctionMetadata
}
func (q *querySpanExtractor) findFunctionDefine(schemaName, funcName string, nArgs int) (base.TableSource, error) {
@@ -300,7 +301,7 @@ type functionDefinitionDetail struct {
func buildFunctionDefinitionDetail(funcDef *functionDefinition) (*functionDefinitionDetail, error) {
function := funcDef.metadata
- definition := function.GetProto().GetDefinition()
+ definition := function.GetDefinition()
res, err := ParsePostgreSQL(definition)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse function definition: %s", definition)
@@ -365,7 +366,7 @@ func getFunctionCandidates(functions []*functionDefinition, nArgs int, funcName
// Filter by name only.
var nameFiltered []*functionDefinition
for _, function := range functions {
- if function.metadata.GetProto().GetName() != funcName {
+ if function.metadata.GetName() != funcName {
continue
}
nameFiltered = append(nameFiltered, function)
diff --git a/backend/plugin/schema/differ.go b/backend/plugin/schema/differ.go
index f0786688d5..705047d794 100644
--- a/backend/plugin/schema/differ.go
+++ b/backend/plugin/schema/differ.go
@@ -312,8 +312,8 @@ func GetDatabaseSchemaDiff(engine storepb.Engine, oldSchema, newSchema *model.Da
return nil, nil
}
- oldMetadata := oldSchema.GetMetadata()
- newMetadata := newSchema.GetMetadata()
+ oldMetadata := oldSchema.GetProto()
+ newMetadata := newSchema.GetProto()
if oldMetadata == nil || newMetadata == nil {
return nil, nil
}
@@ -419,17 +419,17 @@ func addNewSchemaObjects(diff *MetadataDiff, schemaName string, schema *model.Sc
// Add all functions
for _, function := range schema.ListFunctions() {
- if !function.GetProto().GetSkipDump() {
+ if !function.GetSkipDump() {
// Use signature if available, otherwise fall back to name
- functionName := function.GetProto().Signature
+ functionName := function.Signature
if functionName == "" {
- functionName = function.GetProto().Name
+ functionName = function.Name
}
diff.FunctionChanges = append(diff.FunctionChanges, &FunctionDiff{
Action: MetadataDiffActionCreate,
SchemaName: schemaName,
FunctionName: functionName,
- NewFunction: function.GetProto(),
+ NewFunction: function,
})
}
}
@@ -1677,24 +1677,24 @@ func compareFunctions(engine storepb.Engine, diff *MetadataDiff, schemaName stri
// Functions can have overloading, so we need to handle them carefully
// Group functions by signature to properly match overloaded functions
// Build map of old functions by signature
- oldFuncsBySignature := make(map[string]*model.FunctionMetadata)
+ oldFuncsBySignature := make(map[string]*storepb.FunctionMetadata)
for _, fn := range oldSchema.ListFunctions() {
- if !fn.GetProto().GetSkipDump() {
- sig := fn.GetProto().Signature
+ if !fn.GetSkipDump() {
+ sig := fn.Signature
if sig == "" {
- sig = fn.GetProto().Name // fallback if no signature
+ sig = fn.Name // fallback if no signature
}
oldFuncsBySignature[sig] = fn
}
}
// Build map of new functions by signature
- newFuncsBySignature := make(map[string]*model.FunctionMetadata)
+ newFuncsBySignature := make(map[string]*storepb.FunctionMetadata)
for _, fn := range newSchema.ListFunctions() {
- if !fn.GetProto().GetSkipDump() {
- sig := fn.GetProto().Signature
+ if !fn.GetSkipDump() {
+ sig := fn.Signature
if sig == "" {
- sig = fn.GetProto().Name // fallback if no signature
+ sig = fn.Name // fallback if no signature
}
newFuncsBySignature[sig] = fn
}
@@ -1704,15 +1704,15 @@ func compareFunctions(engine storepb.Engine, diff *MetadataDiff, schemaName stri
for sig, oldFunc := range oldFuncsBySignature {
if _, exists := newFuncsBySignature[sig]; !exists {
// Use signature if available, otherwise fall back to name
- functionName := oldFunc.GetProto().Signature
+ functionName := oldFunc.Signature
if functionName == "" {
- functionName = oldFunc.GetProto().Name
+ functionName = oldFunc.Name
}
diff.FunctionChanges = append(diff.FunctionChanges, &FunctionDiff{
Action: MetadataDiffActionDrop,
SchemaName: schemaName,
FunctionName: functionName,
- OldFunction: oldFunc.GetProto(),
+ OldFunction: oldFunc,
})
}
}
@@ -1722,15 +1722,15 @@ func compareFunctions(engine storepb.Engine, diff *MetadataDiff, schemaName stri
oldFunc, exists := oldFuncsBySignature[sig]
if !exists {
// Use signature if available, otherwise fall back to name
- functionName := newFunc.GetProto().Signature
+ functionName := newFunc.Signature
if functionName == "" {
- functionName = newFunc.GetProto().Name
+ functionName = newFunc.Name
}
diff.FunctionChanges = append(diff.FunctionChanges, &FunctionDiff{
Action: MetadataDiffActionCreate,
SchemaName: schemaName,
FunctionName: functionName,
- NewFunction: newFunc.GetProto(),
+ NewFunction: newFunc,
})
} else {
// Use detailed comparison to determine migration strategy
@@ -1744,16 +1744,16 @@ func compareFunctions(engine storepb.Engine, diff *MetadataDiff, schemaName stri
if comparison.CanUseAlterFunction {
// Use ALTER FUNCTION for body-only changes
// Use signature if available, otherwise fall back to name
- functionName := oldFunc.GetProto().Signature
+ functionName := oldFunc.Signature
if functionName == "" {
- functionName = oldFunc.GetProto().Name
+ functionName = oldFunc.Name
}
diff.FunctionChanges = append(diff.FunctionChanges, &FunctionDiff{
Action: MetadataDiffActionAlter,
SchemaName: schemaName,
FunctionName: functionName,
- OldFunction: oldFunc.GetProto(),
- NewFunction: newFunc.GetProto(),
+ OldFunction: oldFunc,
+ NewFunction: newFunc,
SignatureChanged: comparison.SignatureChanged,
BodyChanged: comparison.BodyChanged,
AttributesChanged: comparison.AttributesChanged,
@@ -1763,20 +1763,20 @@ func compareFunctions(engine storepb.Engine, diff *MetadataDiff, schemaName stri
} else {
// Use DROP and CREATE for signature changes
// Use signature if available, otherwise fall back to name for DROP
- oldFunctionName := oldFunc.GetProto().Signature
+ oldFunctionName := oldFunc.Signature
if oldFunctionName == "" {
- oldFunctionName = oldFunc.GetProto().Name
+ oldFunctionName = oldFunc.Name
}
// Use signature if available, otherwise fall back to name for CREATE
- newFunctionName := newFunc.GetProto().Signature
+ newFunctionName := newFunc.Signature
if newFunctionName == "" {
- newFunctionName = newFunc.GetProto().Name
+ newFunctionName = newFunc.Name
}
diff.FunctionChanges = append(diff.FunctionChanges, &FunctionDiff{
Action: MetadataDiffActionDrop,
SchemaName: schemaName,
FunctionName: oldFunctionName,
- OldFunction: oldFunc.GetProto(),
+ OldFunction: oldFunc,
SignatureChanged: comparison.SignatureChanged,
BodyChanged: comparison.BodyChanged,
AttributesChanged: comparison.AttributesChanged,
@@ -1787,7 +1787,7 @@ func compareFunctions(engine storepb.Engine, diff *MetadataDiff, schemaName stri
Action: MetadataDiffActionCreate,
SchemaName: schemaName,
FunctionName: newFunctionName,
- NewFunction: newFunc.GetProto(),
+ NewFunction: newFunc,
SignatureChanged: comparison.SignatureChanged,
BodyChanged: comparison.BodyChanged,
AttributesChanged: comparison.AttributesChanged,
diff --git a/backend/plugin/schema/function_comparer.go b/backend/plugin/schema/function_comparer.go
index af3e16e1d8..ea09c816f8 100644
--- a/backend/plugin/schema/function_comparer.go
+++ b/backend/plugin/schema/function_comparer.go
@@ -4,7 +4,6 @@ import (
"sync"
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
- "github.com/bytebase/bytebase/backend/store/model"
)
// FunctionChange represents a type of change detected in a function.
@@ -37,51 +36,30 @@ type FunctionComparisonResult struct {
// FunctionComparer provides an interface for engine-specific function comparison logic.
type FunctionComparer interface {
// Equal compares two functions and returns whether they are equal.
- Equal(oldFunc, newFunc *model.FunctionMetadata) bool
+ Equal(oldFunc, newFunc *storepb.FunctionMetadata) bool
// CompareDetailed performs detailed comparison and returns migration strategy information.
// Returns nil if functions are equal.
- CompareDetailed(oldFunc, newFunc *model.FunctionMetadata) (*FunctionComparisonResult, error)
+ CompareDetailed(oldFunc, newFunc *storepb.FunctionMetadata) (*FunctionComparisonResult, error)
}
// DefaultFunctionComparer provides default function comparison logic that can be used by most engines.
type DefaultFunctionComparer struct{}
// Equal compares two functions using simple definition comparison.
-func (*DefaultFunctionComparer) Equal(oldFunc, newFunc *model.FunctionMetadata) bool {
+func (*DefaultFunctionComparer) Equal(oldFunc, newFunc *storepb.FunctionMetadata) bool {
if oldFunc == nil || newFunc == nil {
return oldFunc == newFunc
}
- oldProto := oldFunc.GetProto()
- newProto := newFunc.GetProto()
- if oldProto == nil || newProto == nil {
- return oldProto == newProto
- }
-
// Simple definition comparison
- return functionsEqual(oldProto, newProto)
+ return functionsEqual(oldFunc, newFunc)
}
// CompareDetailed provides basic comparison for engines that don't have advanced comparison logic.
-func (*DefaultFunctionComparer) CompareDetailed(oldFunc, newFunc *model.FunctionMetadata) (*FunctionComparisonResult, error) {
- if oldFunc == nil || newFunc == nil {
- return nil, nil
- }
-
- oldProto := oldFunc.GetProto()
- newProto := newFunc.GetProto()
-
- // Handle nil protos
- if oldProto == nil {
- oldProto = &storepb.FunctionMetadata{Definition: oldFunc.Definition}
- }
- if newProto == nil {
- newProto = &storepb.FunctionMetadata{Definition: newFunc.Definition}
- }
-
+func (*DefaultFunctionComparer) CompareDetailed(oldFunc, newFunc *storepb.FunctionMetadata) (*FunctionComparisonResult, error) {
// For default implementation, if functions are equal, return nil
- if functionsEqual(oldProto, newProto) {
+ if functionsEqual(oldFunc, newFunc) {
return nil, nil
}
diff --git a/backend/plugin/schema/pg/function_comparer.go b/backend/plugin/schema/pg/function_comparer.go
index 9d9096ae4d..affb7e7547 100644
--- a/backend/plugin/schema/pg/function_comparer.go
+++ b/backend/plugin/schema/pg/function_comparer.go
@@ -10,7 +10,6 @@ import (
storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
"github.com/bytebase/bytebase/backend/plugin/schema/pg/ast"
- "github.com/bytebase/bytebase/backend/store/model"
)
func init() {
@@ -35,29 +34,20 @@ const (
)
// CompareDetailed compares two functions with PostgreSQL-specific logic using ANTLR parsing.
-func (*PostgreSQLFunctionComparer) CompareDetailed(oldFunc, newFunc *model.FunctionMetadata) (*schema.FunctionComparisonResult, error) {
+func (*PostgreSQLFunctionComparer) CompareDetailed(oldFunc, newFunc *storepb.FunctionMetadata) (*schema.FunctionComparisonResult, error) {
if oldFunc == nil || newFunc == nil {
- return nil, nil
+ if oldFunc == newFunc {
+ return nil, nil
+ }
+ return nil, errors.New("cannot compare nil functions")
}
result := &schema.FunctionComparisonResult{}
- // Compare function signature using ANTLR parsing
- oldProto := oldFunc.GetProto()
- newProto := newFunc.GetProto()
-
- // If proto is nil, create temporary proto with definition for comparison
- if oldProto == nil {
- oldProto = &storepb.FunctionMetadata{Definition: oldFunc.Definition}
- }
- if newProto == nil {
- newProto = &storepb.FunctionMetadata{Definition: newFunc.Definition}
- }
-
- result.SignatureChanged = compareFunctionSignature(oldProto, newProto)
+ result.SignatureChanged = compareFunctionSignature(oldFunc, newFunc)
// Compare function attributes (comment, volatility, etc.)
- attributesChanged, changedAttrs := compareFunctionAttributes(oldProto, newProto)
+ attributesChanged, changedAttrs := compareFunctionAttributes(oldFunc, newFunc)
result.AttributesChanged = attributesChanged
result.ChangedAttributes = changedAttrs
@@ -79,6 +69,27 @@ func (*PostgreSQLFunctionComparer) CompareDetailed(oldFunc, newFunc *model.Funct
return result, nil
}
+// Equal compares two functions for equality (implements the interface expected by the schema differ).
+func (c *PostgreSQLFunctionComparer) Equal(oldFunc, newFunc *storepb.FunctionMetadata) bool {
+ if oldFunc == nil || newFunc == nil {
+ return oldFunc == newFunc
+ }
+
+ result, err := c.CompareDetailed(oldFunc, newFunc)
+ if err != nil {
+ // Fallback to simple definition comparison on error
+ return oldFunc.Definition == newFunc.Definition
+ }
+
+ // If result is nil, functions are equal
+ if result == nil {
+ return true
+ }
+
+ // Functions are not equal if there are any changes
+ return false
+}
+
// compareFunctionSignature compares function signatures using ANTLR parsing.
func compareFunctionSignature(oldFunc, newFunc *storepb.FunctionMetadata) bool {
// Extract signature components using ANTLR parsing
@@ -613,23 +624,6 @@ func determineMigrationStrategy(changeType FunctionChangeType, changedAttrs []st
}
}
-// Equal compares two functions for equality (implements the interface expected by the schema differ).
-func (c *PostgreSQLFunctionComparer) Equal(oldFunc, newFunc *model.FunctionMetadata) bool {
- result, err := c.CompareDetailed(oldFunc, newFunc)
- if err != nil {
- // Fallback to simple definition comparison on error
- return oldFunc.Definition == newFunc.Definition
- }
-
- // If result is nil, functions are equal
- if result == nil {
- return true
- }
-
- // Functions are not equal if there are any changes
- return false
-}
-
// postgreSQLTypeStringsEqual compares two PostgreSQL type strings, handling type aliases and format differences.
func postgreSQLTypeStringsEqual(type1, type2 string) bool {
// Normalize both types and compare
diff --git a/backend/plugin/schema/pg/function_comparer_test.go b/backend/plugin/schema/pg/function_comparer_test.go
index de6437200a..b34df5c94d 100644
--- a/backend/plugin/schema/pg/function_comparer_test.go
+++ b/backend/plugin/schema/pg/function_comparer_test.go
@@ -4,8 +4,8 @@ import (
"strings"
"testing"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/plugin/schema"
- "github.com/bytebase/bytebase/backend/store/model"
)
func TestPostgreSQLFunctionComparer_Equal(t *testing.T) {
@@ -13,36 +13,36 @@ func TestPostgreSQLFunctionComparer_Equal(t *testing.T) {
tests := []struct {
name string
- oldFunc *model.FunctionMetadata
- newFunc *model.FunctionMetadata
+ oldFunc *storepb.FunctionMetadata
+ newFunc *storepb.FunctionMetadata
wantEqual bool
}{
{
name: "identical functions",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
wantEqual: true,
},
{
name: "body only difference",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a * b; END; $$ LANGUAGE plpgsql;`,
},
wantEqual: false,
},
{
name: "signature difference",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer, c integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
wantEqual: false,
@@ -55,48 +55,48 @@ func TestPostgreSQLFunctionComparer_Equal(t *testing.T) {
},
{
name: "one nil function",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE FUNCTION test() RETURNS integer AS $$ BEGIN RETURN 1; END; $$ LANGUAGE plpgsql;`,
},
newFunc: nil,
- wantEqual: true, // CompareDetailed returns nil for nil inputs, so Equal returns true
+ wantEqual: false,
},
{
name: "identical procedures",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,
},
wantEqual: true,
},
{
name: "procedure body only difference",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_name text) AS $$ BEGIN UPDATE users SET name = new_name, updated_at = NOW() WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,
},
wantEqual: false,
},
{
name: "procedure with dollar quote differences",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE public.refresh_stats() LANGUAGE plpgsql AS $procedure$ BEGIN REFRESH MATERIALIZED VIEW stats; END; $procedure$`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE refresh_stats() LANGUAGE plpgsql AS $$ BEGIN REFRESH MATERIALIZED VIEW stats; END; $$`,
},
wantEqual: true, // Should be equal - only formatting differences
},
{
name: "function vs procedure - different types should not be equal",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION get_user_count() RETURNS integer AS $$ BEGIN RETURN (SELECT COUNT(*) FROM users); END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE get_user_count() AS $$ DECLARE count_val integer; BEGIN SELECT COUNT(*) INTO count_val FROM users; END; $$ LANGUAGE plpgsql;`,
},
wantEqual: false,
@@ -118,8 +118,8 @@ func TestPostgreSQLFunctionComparer_CompareDetailed(t *testing.T) {
tests := []struct {
name string
- oldFunc *model.FunctionMetadata
- newFunc *model.FunctionMetadata
+ oldFunc *storepb.FunctionMetadata
+ newFunc *storepb.FunctionMetadata
wantResult *schema.FunctionComparisonResult
wantSignatureChanged bool
wantBodyChanged bool
@@ -127,20 +127,20 @@ func TestPostgreSQLFunctionComparer_CompareDetailed(t *testing.T) {
}{
{
name: "identical functions",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
wantResult: nil, // No changes, should return nil
},
{
name: "body only difference",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a * b; END; $$ LANGUAGE plpgsql;`,
},
wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil
@@ -150,10 +150,10 @@ func TestPostgreSQLFunctionComparer_CompareDetailed(t *testing.T) {
},
{
name: "signature difference",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION add_numbers(a integer, b integer, c integer) RETURNS integer AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;`,
},
wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil
@@ -169,10 +169,10 @@ func TestPostgreSQLFunctionComparer_CompareDetailed(t *testing.T) {
},
{
name: "procedure body only difference",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE update_stats() AS $$ BEGIN UPDATE stats SET last_updated = NOW(); END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE update_stats() AS $$ BEGIN UPDATE stats SET last_updated = NOW(), count = count + 1; END; $$ LANGUAGE plpgsql;`,
},
wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil
@@ -182,10 +182,10 @@ func TestPostgreSQLFunctionComparer_CompareDetailed(t *testing.T) {
},
{
name: "procedure signature difference",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer) AS $$ BEGIN UPDATE users SET updated_at = NOW() WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE update_user(user_id integer, new_status text) AS $$ BEGIN UPDATE users SET updated_at = NOW() WHERE id = user_id; END; $$ LANGUAGE plpgsql;`,
},
wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil
@@ -195,20 +195,20 @@ func TestPostgreSQLFunctionComparer_CompareDetailed(t *testing.T) {
},
{
name: "procedure with dollar quote formatting differences",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE public.refresh_data() LANGUAGE plpgsql AS $procedure$ BEGIN REFRESH MATERIALIZED VIEW data_view; END; $procedure$`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE refresh_data() LANGUAGE plpgsql AS $$ BEGIN REFRESH MATERIALIZED VIEW data_view; END; $$`,
},
wantResult: nil, // Should be nil - functions are equivalent after normalization
},
{
name: "function to procedure change",
- oldFunc: &model.FunctionMetadata{
+ oldFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE FUNCTION calculate_total() RETURNS integer AS $$ BEGIN RETURN (SELECT SUM(amount) FROM orders); END; $$ LANGUAGE plpgsql;`,
},
- newFunc: &model.FunctionMetadata{
+ newFunc: &storepb.FunctionMetadata{
Definition: `CREATE OR REPLACE PROCEDURE calculate_total() AS $$ BEGIN RETURN (SELECT SUM(amount) FROM orders); END; $$ LANGUAGE plpgsql;`,
},
wantResult: &schema.FunctionComparisonResult{}, // Expect a result, not nil
@@ -425,8 +425,8 @@ func TestPostgreSQLFunctionComparer_ProcedureSupport(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
- oldFunc := &model.FunctionMetadata{Definition: tt.def1}
- newFunc := &model.FunctionMetadata{Definition: tt.def2}
+ oldFunc := &storepb.FunctionMetadata{Definition: tt.def1}
+ newFunc := &storepb.FunctionMetadata{Definition: tt.def2}
equal := comparer.Equal(oldFunc, newFunc)
if equal == tt.wantDiff {
diff --git a/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go b/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go
index bf068024d2..66731e5572 100644
--- a/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go
+++ b/backend/plugin/schema/pg/get_database_metadata_testcontainer_test.go
@@ -1393,8 +1393,8 @@ func compareFunctions(t *testing.T, syncFuncs, parseFuncs []*storepb.FunctionMet
// Compare function definitions using PostgreSQL function comparer
comparer := &PostgreSQLFunctionComparer{}
- syncFunc := &model.FunctionMetadata{Definition: syncFn.Definition}
- parseFunc := &model.FunctionMetadata{Definition: parseFn.Definition}
+ syncFunc := &storepb.FunctionMetadata{Definition: syncFn.Definition}
+ parseFunc := &storepb.FunctionMetadata{Definition: parseFn.Definition}
// Use function comparer to check if they are equal
if !comparer.Equal(syncFunc, parseFunc) {
diff --git a/backend/plugin/schema/pg/get_sdl_diff.go b/backend/plugin/schema/pg/get_sdl_diff.go
index bac9146fb6..9731d91c91 100644
--- a/backend/plugin/schema/pg/get_sdl_diff.go
+++ b/backend/plugin/schema/pg/get_sdl_diff.go
@@ -2715,8 +2715,8 @@ func applyMinimalChangesToChunks(previousChunks *schema.SDLChunks, currentSchema
}
// Get table differences between schemas
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -3730,7 +3730,7 @@ func convertDatabaseSchemaToSDL(dbMetadata *model.DatabaseMetadata) (string, err
return "", nil
}
- metadata := dbMetadata.GetMetadata()
+ metadata := dbMetadata.GetProto()
if metadata == nil {
return "", nil
}
@@ -4242,8 +4242,8 @@ func applyStandaloneIndexChangesToChunks(previousChunks *schema.SDLChunks, curre
}
// Get index differences by comparing schema metadata
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -4578,8 +4578,8 @@ func applyFunctionChangesToChunks(previousChunks *schema.SDLChunks, currentSchem
}
// Get function differences by comparing schema metadata
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -4950,8 +4950,8 @@ func applySequenceChangesToChunks(previousChunks *schema.SDLChunks, currentSchem
}
// Get sequence differences by comparing schema metadata
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -5744,8 +5744,8 @@ func applyViewChangesToChunks(previousChunks *schema.SDLChunks, currentSchema, p
}
// Get view differences by comparing schema metadata
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -5996,8 +5996,8 @@ func applyMaterializedViewChangesToChunks(previousChunks *schema.SDLChunks, curr
}
// Get materialized view differences by comparing schema metadata
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -6226,8 +6226,8 @@ func applyEnumTypeChangesToChunks(previousChunks *schema.SDLChunks, currentSchem
}
// Get enum type differences by comparing schema metadata
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -6488,8 +6488,8 @@ func applyColumnCommentChanges(previousChunks *schema.SDLChunks, currentSchema,
}
// Get table metadata from schemas
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -6964,8 +6964,8 @@ func applyExtensionChangesToChunks(previousChunks *schema.SDLChunks, currentSche
}
// Get extension differences by comparing schema metadata
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
@@ -7251,8 +7251,8 @@ func applyTriggerChangesToChunks(previousChunks *schema.SDLChunks, currentSchema
}
// Get trigger differences by comparing schema metadata
- currentMetadata := currentSchema.GetMetadata()
- previousMetadata := previousSchema.GetMetadata()
+ currentMetadata := currentSchema.GetProto()
+ previousMetadata := previousSchema.GetProto()
if currentMetadata == nil || previousMetadata == nil {
return nil
}
diff --git a/backend/runner/plancheck/statement_advise_executor.go b/backend/runner/plancheck/statement_advise_executor.go
index cfdf525332..2ea43cdf13 100644
--- a/backend/runner/plancheck/statement_advise_executor.go
+++ b/backend/runner/plancheck/statement_advise_executor.go
@@ -136,7 +136,7 @@ func (e *StatementAdviseExecutor) runReview(
if dbMetadata == nil {
return nil, errors.Errorf("database schema %s not found", database.String())
}
- if dbMetadata.GetMetadata() == nil {
+ if dbMetadata.GetProto() == nil {
return nil, errors.Errorf("database schema metadata %s not found", database.String())
}
@@ -151,10 +151,10 @@ func (e *StatementAdviseExecutor) runReview(
}
// Create original metadata as read-only
- originMetadata := model.NewDatabaseMetadata(dbMetadata.GetMetadata(), nil, nil, instance.Metadata.GetEngine(), store.IsObjectCaseSensitive(instance))
+ originMetadata := model.NewDatabaseMetadata(dbMetadata.GetProto(), nil, nil, instance.Metadata.GetEngine(), store.IsObjectCaseSensitive(instance))
// Clone metadata for final to avoid modifying the original
- clonedMetadata, ok := proto.Clone(dbMetadata.GetMetadata()).(*storepb.DatabaseSchemaMetadata)
+ clonedMetadata, ok := proto.Clone(dbMetadata.GetProto()).(*storepb.DatabaseSchemaMetadata)
if !ok {
return nil, common.Wrapf(errors.New("failed to clone database schema metadata"), common.Internal, "failed to create a catalog")
}
@@ -177,9 +177,9 @@ func (e *StatementAdviseExecutor) runReview(
classificationConfig := getClassificationByProject(ctx, e.store, database.ProjectID)
adviceList, err := advisor.SQLReviewCheck(ctx, e.sheetManager, statement, reviewConfig.SqlReviewRules, advisor.SQLReviewCheckContext{
- Charset: dbMetadata.GetMetadata().CharacterSet,
- Collation: dbMetadata.GetMetadata().Collation,
- DBSchema: dbMetadata.GetMetadata(),
+ Charset: dbMetadata.GetProto().CharacterSet,
+ Collation: dbMetadata.GetProto().Collation,
+ DBSchema: dbMetadata.GetProto(),
ChangeType: changeType,
DBType: instance.Metadata.GetEngine(),
OriginalMetadata: originMetadata,
diff --git a/backend/runner/plancheck/statement_report_executor.go b/backend/runner/plancheck/statement_report_executor.go
index ca4be3e012..7b83950640 100644
--- a/backend/runner/plancheck/statement_report_executor.go
+++ b/backend/runner/plancheck/statement_report_executor.go
@@ -146,7 +146,7 @@ func GetSQLSummaryReport(ctx context.Context, stores *store.Store, sheetManager
if databaseSchema == nil {
return nil, errors.Errorf("database schema %s not found", database.String())
}
- if databaseSchema.GetMetadata() == nil {
+ if databaseSchema.GetProto() == nil {
return nil, errors.Errorf("database schema metadata %s not found", database.String())
}
instance, err := stores.GetInstanceV2(ctx, &store.FindInstanceMessage{ResourceID: &database.InstanceID})
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index 3ef9d0b627..a53036c54e 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -13,24 +13,97 @@ import (
// This struct combines what were previously separate types: DatabaseMetadata, and DatabaseConfig.
type DatabaseMetadata struct {
// Proto representations for serialization
- metadata *storepb.DatabaseSchemaMetadata
- config *storepb.DatabaseConfig
- schema []byte
+ proto *storepb.DatabaseSchemaMetadata
+ config *storepb.DatabaseConfig
+ rawDump []byte
// Case sensitivity flags
isObjectCaseSensitive bool
isDetailCaseSensitive bool
// Metadata fields (formerly in DatabaseMetadata)
- name string
- owner string
searchPath []string
internal map[string]*SchemaMetadata
+ configInternal map[string]*SchemaConfig
linkedDatabase map[string]*storepb.LinkedDatabaseMetadata
+}
- // Config fields (formerly in DatabaseConfig)
- configName string
- configInternal map[string]*SchemaConfig
+// SchemaMetadata is the metadata for a schema.
+type SchemaMetadata struct {
+ isObjectCaseSensitive bool
+ isDetailCaseSensitive bool
+ internalTables map[string]*TableMetadata
+ internalExternalTable map[string]*ExternalTableMetadata
+ internalViews map[string]*ViewMetadata
+ internalMaterializedView map[string]*MaterializedViewMetadata
+ // Store internal functions by list to take care of the overloadings.
+ functions []*storepb.FunctionMetadata
+ internalProcedures map[string]*ProcedureMetadata
+ internalSequences map[string]*SequenceMetadata
+ internalPackages map[string]*PackageMetadata
+
+ proto *storepb.SchemaMetadata
+}
+
+// SchemaConfig is the config for a schema.
+type SchemaConfig struct {
+ internal map[string]*TableConfig
+}
+
+// TableMetadata is the metadata for a table.
+type TableMetadata struct {
+ // If partitionOf is not nil, it means this table is a partition table.
+ partitionOf *TableMetadata
+
+ isDetailCaseSensitive bool
+ internalColumn map[string]*storepb.ColumnMetadata
+ internalIndexes map[string]*IndexMetadata
+ columns []*storepb.ColumnMetadata
+ rowCount int64
+ proto *storepb.TableMetadata
+}
+
+// TableConfig is the config for a table.
+type TableConfig struct {
+ Classification string
+ internal map[string]*storepb.ColumnCatalog
+}
+
+// ExternalTableMetadata is the metadata for a external table.
+type ExternalTableMetadata struct {
+ isDetailCaseSensitive bool
+ internal map[string]*storepb.ColumnMetadata
+ columns []*storepb.ColumnMetadata
+ proto *storepb.ExternalTableMetadata
+}
+
+type IndexMetadata struct {
+ tableProto *storepb.TableMetadata
+ proto *storepb.IndexMetadata
+}
+
+// ViewMetadata is the metadata for a view.
+type ViewMetadata struct {
+ Definition string
+ proto *storepb.ViewMetadata
+}
+
+type MaterializedViewMetadata struct {
+ Definition string
+ proto *storepb.MaterializedViewMetadata
+}
+
+type ProcedureMetadata struct {
+ Definition string
+ proto *storepb.ProcedureMetadata
+}
+type PackageMetadata struct {
+ Definition string
+ proto *storepb.PackageMetadata
+}
+
+type SequenceMetadata struct {
+ proto *storepb.SequenceMetadata
}
func NewDatabaseMetadata(
@@ -42,23 +115,17 @@ func NewDatabaseMetadata(
) *DatabaseMetadata {
isDetailCaseSensitive := getIsDetailCaseSensitive(engine)
dbMetadata := &DatabaseMetadata{
- metadata: metadata,
- schema: schema,
+ proto: metadata,
+ rawDump: schema,
config: config,
isObjectCaseSensitive: isObjectCaseSensitive,
isDetailCaseSensitive: isDetailCaseSensitive,
- name: metadata.Name,
- owner: metadata.Owner,
searchPath: normalizeSearchPath(metadata.SearchPath),
internal: make(map[string]*SchemaMetadata),
linkedDatabase: make(map[string]*storepb.LinkedDatabaseMetadata),
configInternal: make(map[string]*SchemaConfig),
}
- if config != nil {
- dbMetadata.configName = config.Name
- }
-
// Build schema metadata maps
for _, s := range metadata.Schemas {
schemaMetadata := &SchemaMetadata{
@@ -68,7 +135,6 @@ func NewDatabaseMetadata(
internalExternalTable: make(map[string]*ExternalTableMetadata),
internalViews: make(map[string]*ViewMetadata),
internalMaterializedView: make(map[string]*MaterializedViewMetadata),
- internalFunctions: make([]*FunctionMetadata, 0),
internalProcedures: make(map[string]*ProcedureMetadata),
internalPackages: make(map[string]*PackageMetadata),
internalSequences: make(map[string]*SequenceMetadata),
@@ -134,12 +200,7 @@ func NewDatabaseMetadata(
proto: materializedView,
}
}
- for _, function := range s.Functions {
- schemaMetadata.internalFunctions = append(schemaMetadata.internalFunctions, &FunctionMetadata{
- Definition: function.Definition,
- proto: function,
- })
- }
+ schemaMetadata.functions = s.Functions
for _, procedure := range s.Procedures {
var procedureID string
if isDetailCaseSensitive {
@@ -217,28 +278,18 @@ func NewDatabaseMetadata(
return dbMetadata
}
-// Proto getters for serialization
-func (d *DatabaseMetadata) GetMetadata() *storepb.DatabaseSchemaMetadata {
- return d.metadata
+func (d *DatabaseMetadata) GetProto() *storepb.DatabaseSchemaMetadata {
+ return d.proto
}
-func (d *DatabaseMetadata) GetSchema() []byte {
- return d.schema
+func (d *DatabaseMetadata) GetRawDump() []byte {
+ return d.rawDump
}
func (d *DatabaseMetadata) GetConfig() *storepb.DatabaseConfig {
return d.config
}
-// Metadata methods (formerly from DatabaseMetadata)
-func (d *DatabaseMetadata) GetName() string {
- return d.name
-}
-
-func (d *DatabaseMetadata) GetOwner() string {
- return d.owner
-}
-
func (d *DatabaseMetadata) GetSearchPath() []string {
return d.searchPath
}
@@ -254,10 +305,10 @@ func (d *DatabaseMetadata) GetSchemaMetadata(name string) *SchemaMetadata {
}
func (d *DatabaseMetadata) DatabaseName() string {
- if d.metadata == nil {
+ if d.proto == nil {
return ""
}
- return d.metadata.Name
+ return d.proto.Name
}
func (d *DatabaseMetadata) HasNoTable() bool {
@@ -287,18 +338,14 @@ func (d *DatabaseMetadata) GetLinkedDatabase(name string) *storepb.LinkedDatabas
return d.linkedDatabase[nameID]
}
-func (d *DatabaseMetadata) GetProto() *storepb.DatabaseSchemaMetadata {
- return d.metadata
-}
-
func (d *DatabaseMetadata) SortProto() {
// Sort schemas by name
- slices.SortFunc(d.metadata.Schemas, func(x, y *storepb.SchemaMetadata) int {
+ slices.SortFunc(d.proto.Schemas, func(x, y *storepb.SchemaMetadata) int {
return strings.Compare(x.Name, y.Name)
})
// Sort tables and indexes within each schema
- for _, schema := range d.metadata.Schemas {
+ for _, schema := range d.proto.Schemas {
// Sort tables by name
slices.SortFunc(schema.Tables, func(x, y *storepb.TableMetadata) int {
return strings.Compare(x.Name, y.Name)
@@ -341,7 +388,7 @@ func (d *DatabaseMetadata) CreateSchema(schemaName string) *SchemaMetadata {
}
// Add to proto's schema list
- d.metadata.Schemas = append(d.metadata.Schemas, newSchemaProto)
+ d.proto.Schemas = append(d.proto.Schemas, newSchemaProto)
// Create SchemaMetadata wrapper
schemaMeta := &SchemaMetadata{
@@ -351,7 +398,6 @@ func (d *DatabaseMetadata) CreateSchema(schemaName string) *SchemaMetadata {
internalExternalTable: make(map[string]*ExternalTableMetadata),
internalViews: make(map[string]*ViewMetadata),
internalMaterializedView: make(map[string]*MaterializedViewMetadata),
- internalFunctions: []*FunctionMetadata{},
internalProcedures: make(map[string]*ProcedureMetadata),
internalPackages: make(map[string]*PackageMetadata),
internalSequences: make(map[string]*SequenceMetadata),
@@ -373,7 +419,7 @@ func (d *DatabaseMetadata) CreateSchema(schemaName string) *SchemaMetadata {
func (d *DatabaseMetadata) DropSchema(schemaName string) error {
// Check if schema exists
if d.GetSchemaMetadata(schemaName) == nil {
- return errors.Errorf("schema %q does not exist in database %q", schemaName, d.name)
+ return errors.Errorf("schema %q does not exist in database %q", schemaName, d.proto.GetName())
}
// Remove from internal map
@@ -386,8 +432,8 @@ func (d *DatabaseMetadata) DropSchema(schemaName string) error {
delete(d.internal, schemaID)
// Remove from proto's schema list
- newSchemas := make([]*storepb.SchemaMetadata, 0, len(d.metadata.Schemas)-1)
- for _, schema := range d.metadata.Schemas {
+ newSchemas := make([]*storepb.SchemaMetadata, 0, len(d.proto.Schemas)-1)
+ for _, schema := range d.proto.Schemas {
if d.isObjectCaseSensitive {
if schema.Name != schemaName {
newSchemas = append(newSchemas, schema)
@@ -398,7 +444,7 @@ func (d *DatabaseMetadata) DropSchema(schemaName string) error {
}
}
}
- d.metadata.Schemas = newSchemas
+ d.proto.Schemas = newSchemas
return nil
}
@@ -420,7 +466,7 @@ func (d *DatabaseMetadata) BuildDatabaseConfig() *storepb.DatabaseConfig {
if d == nil {
return nil
}
- config := &storepb.DatabaseConfig{Name: d.configName}
+ config := &storepb.DatabaseConfig{Name: d.config.GetName()}
for schemaName, sConfig := range d.configInternal {
schemaConfig := &storepb.SchemaCatalog{Name: schemaName}
@@ -444,11 +490,6 @@ func (d *DatabaseMetadata) BuildDatabaseConfig() *storepb.DatabaseConfig {
return config
}
-// SchemaConfig is the config for a schema.
-type SchemaConfig struct {
- internal map[string]*TableConfig
-}
-
// GetTableConfig gets the table config by name.
// If not found, returns a new empty table config.
func (s *SchemaConfig) GetTableConfig(name string) *TableConfig {
@@ -463,12 +504,6 @@ func (s *SchemaConfig) GetTableConfig(name string) *TableConfig {
}
}
-// TableConfig is the config for a table.
-type TableConfig struct {
- Classification string
- internal map[string]*storepb.ColumnCatalog
-}
-
// GetColumnConfig gets the column config by name.
// If not found, returns a new empty column config.
func (t *TableConfig) GetColumnConfig(name string) *storepb.ColumnCatalog {
@@ -483,23 +518,6 @@ func (t *TableConfig) GetColumnConfig(name string) *storepb.ColumnCatalog {
}
}
-// SchemaMetadata is the metadata for a schema.
-type SchemaMetadata struct {
- isObjectCaseSensitive bool
- isDetailCaseSensitive bool
- internalTables map[string]*TableMetadata
- internalExternalTable map[string]*ExternalTableMetadata
- internalViews map[string]*ViewMetadata
- internalMaterializedView map[string]*MaterializedViewMetadata
- // Store internal functions by list to take care of the overloadings.
- internalFunctions []*FunctionMetadata
- internalProcedures map[string]*ProcedureMetadata
- internalSequences map[string]*SequenceMetadata
- internalPackages map[string]*PackageMetadata
-
- proto *storepb.SchemaMetadata
-}
-
func (s *SchemaMetadata) GetOwner() string {
if s.proto == nil {
return ""
@@ -589,21 +607,19 @@ func (s *SchemaMetadata) GetExternalTable(name string) *ExternalTableMetadata {
}
// ListFunctions lists the functions.
-func (s *SchemaMetadata) ListFunctions() []*FunctionMetadata {
- var result []*FunctionMetadata
- result = append(result, s.internalFunctions...)
- return result
+func (s *SchemaMetadata) ListFunctions() []*storepb.FunctionMetadata {
+ return s.functions
}
// GetFunction gets the function by name.
-func (s *SchemaMetadata) GetFunction(name string) *FunctionMetadata {
- for _, function := range s.internalFunctions {
+func (s *SchemaMetadata) GetFunction(name string) *storepb.FunctionMetadata {
+ for _, function := range s.functions {
if s.isDetailCaseSensitive {
- if function.proto.Name == name {
+ if function.Name == name {
return function
}
} else {
- if strings.EqualFold(function.proto.Name, name) {
+ if strings.EqualFold(function.Name, name) {
return function
}
}
@@ -668,8 +684,8 @@ func (s *SchemaMetadata) ListProcedureNames() []string {
// ListFunctionNames lists the function names.
func (s *SchemaMetadata) ListFunctionNames() []string {
var result []string
- for _, function := range s.internalFunctions {
- result = append(result, function.GetProto().GetName())
+ for _, function := range s.functions {
+ result = append(result, function.GetName())
}
slices.Sort(result)
@@ -1078,19 +1094,6 @@ func buildTablesMetadataRecursive(originalColumn []*storepb.ColumnMetadata, part
return tables, names
}
-// TableMetadata is the metadata for a table.
-type TableMetadata struct {
- // If partitionOf is not nil, it means this table is a partition table.
- partitionOf *TableMetadata
-
- isDetailCaseSensitive bool
- internalColumn map[string]*storepb.ColumnMetadata
- internalIndexes map[string]*IndexMetadata
- columns []*storepb.ColumnMetadata
- rowCount int64
- proto *storepb.TableMetadata
-}
-
func (t *TableMetadata) GetOwner() string {
return t.proto.Owner
}
@@ -1384,14 +1387,6 @@ func (t *TableMetadata) RenameColumn(oldName string, newName string) error {
return nil
}
-// ExternalTableMetadata is the metadata for a external table.
-type ExternalTableMetadata struct {
- isDetailCaseSensitive bool
- internal map[string]*storepb.ColumnMetadata
- columns []*storepb.ColumnMetadata
- proto *storepb.ExternalTableMetadata
-}
-
func (t *ExternalTableMetadata) GetProto() *storepb.ExternalTableMetadata {
return t.proto
}
@@ -1412,11 +1407,6 @@ func (t *ExternalTableMetadata) GetColumns() []*storepb.ColumnMetadata {
return t.columns
}
-type IndexMetadata struct {
- tableProto *storepb.TableMetadata
- proto *storepb.IndexMetadata
-}
-
func (i *IndexMetadata) GetProto() *storepb.IndexMetadata {
return i.proto
}
@@ -1552,56 +1542,22 @@ func (t *TableMetadata) RenameIndex(oldName string, newName string) error {
return nil
}
-// ViewMetadata is the metadata for a view.
-type ViewMetadata struct {
- Definition string
- proto *storepb.ViewMetadata
-}
-
func (v *ViewMetadata) GetProto() *storepb.ViewMetadata {
return v.proto
}
-type MaterializedViewMetadata struct {
- Definition string
- proto *storepb.MaterializedViewMetadata
-}
-
func (m *MaterializedViewMetadata) GetProto() *storepb.MaterializedViewMetadata {
return m.proto
}
-type FunctionMetadata struct {
- Definition string
- proto *storepb.FunctionMetadata
-}
-
-func (f *FunctionMetadata) GetProto() *storepb.FunctionMetadata {
- return f.proto
-}
-
-type ProcedureMetadata struct {
- Definition string
- proto *storepb.ProcedureMetadata
-}
-
func (p *ProcedureMetadata) GetProto() *storepb.ProcedureMetadata {
return p.proto
}
-type PackageMetadata struct {
- Definition string
- proto *storepb.PackageMetadata
-}
-
func (p *PackageMetadata) GetProto() *storepb.PackageMetadata {
return p.proto
}
-type SequenceMetadata struct {
- proto *storepb.SequenceMetadata
-}
-
func (p *SequenceMetadata) GetProto() *storepb.SequenceMetadata {
return p.proto
}
diff --git a/backend/store/model/pg_searcher.go b/backend/store/model/pg_searcher.go
index c31e137fd3..10bdca36a0 100644
--- a/backend/store/model/pg_searcher.go
+++ b/backend/store/model/pg_searcher.go
@@ -1,6 +1,10 @@
package model
-import "strings"
+import (
+ "strings"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
+)
// DatabaseSearcher provides a fluent interface for searching database objects with a specific search path.
// This helper avoids repetitive searchPath parameter passing when performing multiple searches.
@@ -126,22 +130,22 @@ func (s *DatabaseSearcher) SearchMaterializedView(name string) (string, *Materia
// SearchFunctions searches for functions in the search path.
// NOTE: This is primarily designed for PostgreSQL's search_path concept.
-func (s *DatabaseSearcher) SearchFunctions(name string) ([]string, []*FunctionMetadata) {
+func (s *DatabaseSearcher) SearchFunctions(name string) ([]string, []*storepb.FunctionMetadata) {
var schemas []string
- var funcs []*FunctionMetadata
+ var funcs []*storepb.FunctionMetadata
for _, schemaName := range s.searchPath {
schema := s.db.GetSchemaMetadata(schemaName)
if schema == nil {
continue
}
- for _, function := range schema.ListFunctions() {
+ for _, function := range schema.functions {
if s.db.isDetailCaseSensitive {
- if function.proto.Name == name {
+ if function.Name == name {
schemas = append(schemas, schema.proto.Name)
funcs = append(funcs, function)
}
} else {
- if strings.EqualFold(function.proto.Name, name) {
+ if strings.EqualFold(function.Name, name) {
schemas = append(schemas, schema.proto.Name)
funcs = append(funcs, function)
}
@@ -264,22 +268,22 @@ func (d *DatabaseMetadata) SearchMaterializedView(searchPath []string, name stri
// SearchFunctions searches for functions in the search path.
// NOTE: This is primarily designed for PostgreSQL's search_path concept.
-func (d *DatabaseMetadata) SearchFunctions(searchPath []string, name string) ([]string, []*FunctionMetadata) {
+func (d *DatabaseMetadata) SearchFunctions(searchPath []string, name string) ([]string, []*storepb.FunctionMetadata) {
var schemas []string
- var funcs []*FunctionMetadata
+ var funcs []*storepb.FunctionMetadata
for _, schemaName := range searchPath {
schema := d.GetSchemaMetadata(schemaName)
if schema == nil {
continue
}
- for _, function := range schema.ListFunctions() {
+ for _, function := range schema.functions {
if d.isDetailCaseSensitive {
- if function.proto.Name == name {
+ if function.Name == name {
schemas = append(schemas, schema.proto.Name)
funcs = append(funcs, function)
}
} else {
- if strings.EqualFold(function.proto.Name, name) {
+ if strings.EqualFold(function.Name, name) {
schemas = append(schemas, schema.proto.Name)
funcs = append(funcs, function)
}