Skip to content

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

refactor: remove SortProto() method from DatabaseMetadata (#18147)

Danny Xuded03e1871f4
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 e2e5663a33..c245951b7d 100644
--- a/backend/plugin/advisor/pg/advisor_statement_object_owner_check.go
+++ b/backend/plugin/advisor/pg/advisor_statement_object_owner_check.go
@@ -147,7 +147,7 @@ func (r *statementObjectOwnerCheckRule) checkSchemaOwnership(schemaName string,
return
}
- owner := schemaMeta.GetOwner()
+ owner := schemaMeta.GetProto().GetOwner()
if owner == pgDatabaseOwner {
owner = r.dbMetadata.GetProto().GetOwner()
}
diff --git a/backend/plugin/schema/mysql/walk_through_test.go b/backend/plugin/schema/mysql/walk_through_test.go
index 4e09d05760..36e4aefec7 100644
--- a/backend/plugin/schema/mysql/walk_through_test.go
+++ b/backend/plugin/schema/mysql/walk_through_test.go
@@ -68,10 +68,12 @@ func TestWalkThrough(t *testing.T) {
want := &storepb.DatabaseSchemaMetadata{}
err := common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)
require.NoError(t, err)
- // Sort proto for deterministic comparison
- state.SortProto()
result := state.GetProto()
- diff := cmp.Diff(want, result, protocmp.Transform())
+ diff := cmp.Diff(want, result, protocmp.Transform(),
+ protocmp.SortRepeatedFields(&storepb.DatabaseSchemaMetadata{}, "schemas"),
+ protocmp.SortRepeatedFields(&storepb.SchemaMetadata{}, "tables", "views"),
+ protocmp.SortRepeatedFields(&storepb.TableMetadata{}, "indexes", "columns"),
+ )
require.Empty(t, diff)
}
}
diff --git a/backend/plugin/schema/pg/walk_through_test.go b/backend/plugin/schema/pg/walk_through_test.go
index e2aa4d34f4..f3c7087127 100644
--- a/backend/plugin/schema/pg/walk_through_test.go
+++ b/backend/plugin/schema/pg/walk_through_test.go
@@ -111,10 +111,12 @@ func TestWalkThrough(t *testing.T) {
want := &storepb.DatabaseSchemaMetadata{}
err := common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)
require.NoError(t, err)
- // Sort proto for deterministic comparison
- state.SortProto()
result := state.GetProto()
- diff := cmp.Diff(want, result, protocmp.Transform())
+ diff := cmp.Diff(want, result, protocmp.Transform(),
+ protocmp.SortRepeatedFields(&storepb.DatabaseSchemaMetadata{}, "schemas"),
+ protocmp.SortRepeatedFields(&storepb.SchemaMetadata{}, "tables", "views"),
+ protocmp.SortRepeatedFields(&storepb.TableMetadata{}, "indexes", "columns"),
+ )
require.Empty(t, diff)
}
}
@@ -208,10 +210,12 @@ func TestWalkThroughANTLR(t *testing.T) {
want := &storepb.DatabaseSchemaMetadata{}
err := common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)
require.NoError(t, err)
- // Sort proto for deterministic comparison
- state.SortProto()
result := state.GetProto()
- diff := cmp.Diff(want, result, protocmp.Transform())
+ diff := cmp.Diff(want, result, protocmp.Transform(),
+ protocmp.SortRepeatedFields(&storepb.DatabaseSchemaMetadata{}, "schemas"),
+ protocmp.SortRepeatedFields(&storepb.SchemaMetadata{}, "tables", "views"),
+ protocmp.SortRepeatedFields(&storepb.TableMetadata{}, "indexes", "columns"),
+ )
require.Empty(t, diff)
}
}
diff --git a/backend/plugin/schema/tidb/walk_through_test.go b/backend/plugin/schema/tidb/walk_through_test.go
index a0ca6691fa..547589176c 100644
--- a/backend/plugin/schema/tidb/walk_through_test.go
+++ b/backend/plugin/schema/tidb/walk_through_test.go
@@ -68,10 +68,12 @@ func TestWalkThrough(t *testing.T) {
want := &storepb.DatabaseSchemaMetadata{}
err := common.ProtojsonUnmarshaler.Unmarshal([]byte(test.Want), want)
require.NoError(t, err)
- // Sort proto for deterministic comparison
- state.SortProto()
result := state.GetProto()
- diff := cmp.Diff(want, result, protocmp.Transform())
+ diff := cmp.Diff(want, result, protocmp.Transform(),
+ protocmp.SortRepeatedFields(&storepb.DatabaseSchemaMetadata{}, "schemas"),
+ protocmp.SortRepeatedFields(&storepb.SchemaMetadata{}, "tables", "views"),
+ protocmp.SortRepeatedFields(&storepb.TableMetadata{}, "indexes", "columns"),
+ )
require.Empty(t, diff)
}
}
diff --git a/backend/store/model/database.go b/backend/store/model/database.go
index d60e916383..54bf289df4 100644
--- a/backend/store/model/database.go
+++ b/backend/store/model/database.go
@@ -300,43 +300,6 @@ func (d *DatabaseMetadata) GetLinkedDatabase(name string) *storepb.LinkedDatabas
return d.linkedDatabase[nameID]
}
-func (d *DatabaseMetadata) SortProto() {
- // Sort schemas by name
- 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.proto.Schemas {
- // Sort tables by name
- slices.SortFunc(schema.Tables, func(x, y *storepb.TableMetadata) int {
- return strings.Compare(x.Name, y.Name)
- })
-
- // Sort indexes within each table by name
- for _, table := range schema.Tables {
- slices.SortFunc(table.Indexes, func(x, y *storepb.IndexMetadata) int {
- return strings.Compare(x.Name, y.Name)
- })
-
- // Sort columns by position
- slices.SortFunc(table.Columns, func(x, y *storepb.ColumnMetadata) int {
- if x.Position < y.Position {
- return -1
- } else if x.Position > y.Position {
- return 1
- }
- return 0
- })
- }
-
- // Sort views by name
- slices.SortFunc(schema.Views, func(x, y *storepb.ViewMetadata) int {
- return strings.Compare(x.Name, y.Name)
- })
- }
-}
-
func (d *DatabaseMetadata) GetIsObjectCaseSensitive() bool {
return d.isObjectCaseSensitive
}
@@ -480,13 +443,6 @@ func (t *TableConfig) GetColumnConfig(name string) *storepb.ColumnCatalog {
}
}
-func (s *SchemaMetadata) GetOwner() string {
- if s.proto == nil {
- return ""
- }
- return s.proto.Owner
-}
-
// GetTable gets the schema by name.
func (s *SchemaMetadata) GetTable(name string) *TableMetadata {
if s == nil {