Skip to content

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

chore: fast equal database metadata (#12738)

Danny Xu967abbfbb069
diff --git a/backend/api/v1/branch_service.go b/backend/api/v1/branch_service.go
index a6d0a0f04d..befd2e78b8 100644
--- a/backend/api/v1/branch_service.go
+++ b/backend/api/v1/branch_service.go
@@ -1087,7 +1087,7 @@ func trimDatabaseMetadata(sourceMetadata *storepb.DatabaseSchemaMetadata, target
continue
}
- if !equalTable(table, tt.GetProto()) {
+ if !common.EqualTable(table, tt.GetProto()) {
trimSchema.Tables = append(trimSchema.Tables, table)
continue
}
@@ -1133,7 +1133,7 @@ func trimDatabaseMetadata(sourceMetadata *storepb.DatabaseSchemaMetadata, target
continue
}
- if !equalTable(table, tt.GetProto()) {
+ if !common.EqualTable(table, tt.GetProto()) {
trimSchema.Tables = append(trimSchema.Tables, table)
continue
}
@@ -1179,116 +1179,6 @@ func trimDatabaseMetadata(sourceMetadata *storepb.DatabaseSchemaMetadata, target
return s, t
}
-func equalTable(s, t *storepb.TableMetadata) bool {
- if len(s.GetColumns()) != len(t.GetColumns()) {
- return false
- }
- if len(s.Indexes) != len(t.Indexes) {
- return false
- }
- if len(s.Partitions) != len(t.Partitions) {
- return false
- }
- if s.GetComment() != t.GetComment() {
- return false
- }
- if s.GetUserComment() != t.GetUserComment() {
- return false
- }
- for i := 0; i < len(s.GetColumns()); i++ {
- sc, tc := s.GetColumns()[i], t.GetColumns()[i]
- if sc.Name != tc.Name {
- return false
- }
- if sc.OnUpdate != tc.OnUpdate {
- return false
- }
- if sc.Comment != tc.Comment {
- return false
- }
- if sc.UserComment != tc.UserComment {
- return false
- }
- if sc.Type != tc.Type {
- return false
- }
- if sc.Nullable != tc.Nullable {
- return false
- }
- if sc.GetDefault().GetValue() != tc.GetDefault().GetValue() {
- return false
- }
- if sc.GetDefaultExpression() != tc.GetDefaultExpression() {
- return false
- }
- if sc.GetDefaultNull() != tc.GetDefaultNull() {
- return false
- }
- }
- for i := 0; i < len(s.GetIndexes()); i++ {
- si, ti := s.GetIndexes()[i], t.GetIndexes()[i]
- if si.GetName() != ti.GetName() {
- return false
- }
- if si.GetDefinition() != ti.GetDefinition() {
- return false
- }
- if si.GetPrimary() != ti.GetPrimary() {
- return false
- }
- if si.GetUnique() != ti.GetUnique() {
- return false
- }
- if si.GetType() != ti.GetType() {
- return false
- }
- if si.GetVisible() != ti.GetVisible() {
- return false
- }
- if si.GetComment() != ti.GetComment() {
- return false
- }
- if !slices.Equal(si.GetExpressions(), ti.GetExpressions()) {
- return false
- }
- }
-
- for i := 0; i < len(s.GetPartitions()); i++ {
- si, ti := s.GetPartitions()[i], t.GetPartitions()[i]
- if !equalPartitions(si, ti) {
- return false
- }
- }
- return true
-}
-
-func equalPartitions(s, t *storepb.TablePartitionMetadata) bool {
- if s.GetName() != t.GetName() {
- return false
- }
- if s.Type != t.Type {
- return false
- }
- if s.Expression != t.Expression {
- return false
- }
- if s.Value != t.Value {
- return false
- }
- if s.UseDefault != t.UseDefault {
- return false
- }
- if len(s.Subpartitions) != len(t.Subpartitions) {
- return false
- }
- for i := 0; i < len(s.Subpartitions); i++ {
- if !equalPartitions(s.Subpartitions[i], t.Subpartitions[i]) {
- return false
- }
- }
- return true
-}
-
func reconcileMetadata(metadata *storepb.DatabaseSchemaMetadata, engine storepb.Engine) {
for _, schema := range metadata.Schemas {
for _, table := range schema.Tables {
diff --git a/backend/api/v1/branch_service_test.go b/backend/api/v1/branch_service_test.go
index 5ddd26ea87..b9c00b4555 100644
--- a/backend/api/v1/branch_service_test.go
+++ b/backend/api/v1/branch_service_test.go
@@ -10,119 +10,6 @@ import (
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-func TestEqualTable(t *testing.T) {
- tests := []struct {
- s *storepb.TableMetadata
- t *storepb.TableMetadata
- want bool
- }{
- {
- s: &storepb.TableMetadata{Comment: "a"},
- t: &storepb.TableMetadata{Comment: "b"},
- want: false,
- },
- {
- s: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a"},
- },
- },
- t: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "b"},
- },
- },
- want: false,
- },
- {
- s: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a"},
- },
- },
- t: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a"},
- {Name: "b"},
- },
- },
- want: false,
- },
- {
- s: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int"},
- {Name: "b", Type: "int"},
- },
- },
- t: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int"},
- {Name: "b", Type: "varchar"},
- },
- },
- want: false,
- },
- {
- s: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int"},
- },
- },
- t: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int", Comment: "hello?"},
- },
- },
- want: false,
- },
- {
- s: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int"},
- },
- },
- t: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int", UserComment: "hello?"},
- },
- },
- want: false,
- },
- {
- s: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int", DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "abc"}},
- },
- },
- t: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int", DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "abc"}},
- },
- },
- want: true,
- },
- {
- s: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int", DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "abc"}},
- },
- },
- t: &storepb.TableMetadata{
- Columns: []*storepb.ColumnMetadata{
- {Name: "a", Type: "int", DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "cba"}},
- },
- },
- want: false,
- },
- }
-
- for i, tc := range tests {
- got := equalTable(tc.s, tc.t)
- require.Equal(t, tc.want, got, i)
- }
-}
-
func TestTrimDatabaseMetadata(t *testing.T) {
source := &storepb.DatabaseSchemaMetadata{
Schemas: []*storepb.SchemaMetadata{
diff --git a/backend/common/db_schema.go b/backend/common/db_schema.go
index 48eba355c3..0c835392d8 100644
--- a/backend/common/db_schema.go
+++ b/backend/common/db_schema.go
@@ -2,7 +2,10 @@ package common
import (
"regexp"
+ "slices"
"strings"
+
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
var numberReg = regexp.MustCompile("^[0-9]+$")
@@ -33,3 +36,212 @@ func GetCommentFromClassificationAndUserComment(classification, userComment stri
}
return classification + "-" + userComment
}
+
+func EqualDatabaseSchemaMetadataFast(s, t *storepb.DatabaseSchemaMetadata) bool {
+ if s.GetName() != t.GetName() {
+ return false
+ }
+ if len(s.GetSchemas()) != len(t.GetSchemas()) {
+ return false
+ }
+ oldSchemaMap := make(map[string]*storepb.SchemaMetadata)
+ for _, schema := range s.GetSchemas() {
+ oldSchemaMap[schema.GetName()] = schema
+ }
+ for _, schema := range t.GetSchemas() {
+ oldSchema, ok := oldSchemaMap[schema.GetName()]
+ if !ok {
+ return false
+ }
+ if !equalSchema(oldSchema, schema) {
+ return false
+ }
+ }
+ return true
+}
+
+func equalSchema(s, t *storepb.SchemaMetadata) bool {
+ if s.GetName() != t.GetName() {
+ return false
+ }
+ if len(s.GetTables()) != len(t.GetTables()) {
+ return false
+ }
+ oldTableMap := make(map[string]*storepb.TableMetadata)
+ for _, table := range s.GetTables() {
+ oldTableMap[table.GetName()] = table
+ }
+ for _, table := range t.GetTables() {
+ oldTable, ok := oldTableMap[table.GetName()]
+ if !ok {
+ return false
+ }
+ if !EqualTable(oldTable, table) {
+ return false
+ }
+ }
+
+ if len(s.GetViews()) != len(t.GetViews()) {
+ return false
+ }
+ oldViewMap := make(map[string]*storepb.ViewMetadata)
+ for _, view := range s.GetViews() {
+ oldViewMap[view.GetName()] = view
+ }
+ for _, view := range t.GetViews() {
+ oldView, ok := oldViewMap[view.GetName()]
+ if !ok {
+ return false
+ }
+ if oldView.GetDefinition() != view.GetDefinition() {
+ return false
+ }
+ }
+
+ if len(s.GetFunctions()) != len(t.GetFunctions()) {
+ return false
+ }
+ oldFunctionMap := make(map[string]*storepb.FunctionMetadata)
+ for _, function := range s.GetFunctions() {
+ oldFunctionMap[function.GetName()] = function
+ }
+ for _, function := range t.GetFunctions() {
+ oldFunction, ok := oldFunctionMap[function.GetName()]
+ if !ok {
+ return false
+ }
+ if oldFunction.GetDefinition() != function.GetDefinition() {
+ return false
+ }
+ }
+
+ if len(s.GetProcedures()) != len(t.GetProcedures()) {
+ return false
+ }
+ oldProcedureMap := make(map[string]*storepb.ProcedureMetadata)
+ for _, procedure := range s.GetProcedures() {
+ oldProcedureMap[procedure.GetName()] = procedure
+ }
+ for _, procedure := range t.GetProcedures() {
+ oldProcedure, ok := oldProcedureMap[procedure.GetName()]
+ if !ok {
+ return false
+ }
+ if oldProcedure.GetDefinition() != procedure.GetDefinition() {
+ return false
+ }
+ }
+
+ return true
+}
+
+// EqualTable compares metadata for two tables.
+func EqualTable(s, t *storepb.TableMetadata) bool {
+ if len(s.GetColumns()) != len(t.GetColumns()) {
+ return false
+ }
+ if len(s.Indexes) != len(t.Indexes) {
+ return false
+ }
+ if len(s.Partitions) != len(t.Partitions) {
+ return false
+ }
+ if s.GetComment() != t.GetComment() {
+ return false
+ }
+ if s.GetUserComment() != t.GetUserComment() {
+ return false
+ }
+ for i := 0; i < len(s.GetColumns()); i++ {
+ sc, tc := s.GetColumns()[i], t.GetColumns()[i]
+ if sc.Name != tc.Name {
+ return false
+ }
+ if sc.OnUpdate != tc.OnUpdate {
+ return false
+ }
+ if sc.Comment != tc.Comment {
+ return false
+ }
+ if sc.UserComment != tc.UserComment {
+ return false
+ }
+ if sc.Type != tc.Type {
+ return false
+ }
+ if sc.Nullable != tc.Nullable {
+ return false
+ }
+ if sc.GetDefault().GetValue() != tc.GetDefault().GetValue() {
+ return false
+ }
+ if sc.GetDefaultExpression() != tc.GetDefaultExpression() {
+ return false
+ }
+ if sc.GetDefaultNull() != tc.GetDefaultNull() {
+ return false
+ }
+ }
+ for i := 0; i < len(s.GetIndexes()); i++ {
+ si, ti := s.GetIndexes()[i], t.GetIndexes()[i]
+ if si.GetName() != ti.GetName() {
+ return false
+ }
+ if si.GetDefinition() != ti.GetDefinition() {
+ return false
+ }
+ if si.GetPrimary() != ti.GetPrimary() {
+ return false
+ }
+ if si.GetUnique() != ti.GetUnique() {
+ return false
+ }
+ if si.GetType() != ti.GetType() {
+ return false
+ }
+ if si.GetVisible() != ti.GetVisible() {
+ return false
+ }
+ if si.GetComment() != ti.GetComment() {
+ return false
+ }
+ if !slices.Equal(si.GetExpressions(), ti.GetExpressions()) {
+ return false
+ }
+ }
+
+ for i := 0; i < len(s.GetPartitions()); i++ {
+ si, ti := s.GetPartitions()[i], t.GetPartitions()[i]
+ if !equalPartitions(si, ti) {
+ return false
+ }
+ }
+ return true
+}
+
+func equalPartitions(s, t *storepb.TablePartitionMetadata) bool {
+ if s.GetName() != t.GetName() {
+ return false
+ }
+ if s.Type != t.Type {
+ return false
+ }
+ if s.Expression != t.Expression {
+ return false
+ }
+ if s.Value != t.Value {
+ return false
+ }
+ if s.UseDefault != t.UseDefault {
+ return false
+ }
+ if len(s.Subpartitions) != len(t.Subpartitions) {
+ return false
+ }
+ for i := 0; i < len(s.Subpartitions); i++ {
+ if !equalPartitions(s.Subpartitions[i], t.Subpartitions[i]) {
+ return false
+ }
+ }
+ return true
+}
diff --git a/backend/common/db_schema_test.go b/backend/common/db_schema_test.go
index 4930c245b5..43273dcac8 100644
--- a/backend/common/db_schema_test.go
+++ b/backend/common/db_schema_test.go
@@ -5,6 +5,9 @@ import (
"testing"
"github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/require"
+
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
func TestClassification(t *testing.T) {
@@ -85,3 +88,115 @@ func TestClassification(t *testing.T) {
})
}
}
+func TestEqualTable(t *testing.T) {
+ tests := []struct {
+ s *storepb.TableMetadata
+ t *storepb.TableMetadata
+ want bool
+ }{
+ {
+ s: &storepb.TableMetadata{Comment: "a"},
+ t: &storepb.TableMetadata{Comment: "b"},
+ want: false,
+ },
+ {
+ s: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a"},
+ },
+ },
+ t: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "b"},
+ },
+ },
+ want: false,
+ },
+ {
+ s: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a"},
+ },
+ },
+ t: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a"},
+ {Name: "b"},
+ },
+ },
+ want: false,
+ },
+ {
+ s: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int"},
+ {Name: "b", Type: "int"},
+ },
+ },
+ t: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int"},
+ {Name: "b", Type: "varchar"},
+ },
+ },
+ want: false,
+ },
+ {
+ s: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int"},
+ },
+ },
+ t: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int", Comment: "hello?"},
+ },
+ },
+ want: false,
+ },
+ {
+ s: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int"},
+ },
+ },
+ t: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int", UserComment: "hello?"},
+ },
+ },
+ want: false,
+ },
+ {
+ s: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int", DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "abc"}},
+ },
+ },
+ t: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int", DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "abc"}},
+ },
+ },
+ want: true,
+ },
+ {
+ s: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int", DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "abc"}},
+ },
+ },
+ t: &storepb.TableMetadata{
+ Columns: []*storepb.ColumnMetadata{
+ {Name: "a", Type: "int", DefaultValue: &storepb.ColumnMetadata_DefaultExpression{DefaultExpression: "cba"}},
+ },
+ },
+ want: false,
+ },
+ }
+
+ for i, tc := range tests {
+ got := EqualTable(tc.s, tc.t)
+ require.Equal(t, tc.want, got, i)
+ }
+}
diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.go
index 84d3ef820a..132996d51a 100644
--- a/backend/runner/schemasync/syncer.go
+++ b/backend/runner/schemasync/syncer.go
@@ -426,7 +426,7 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.Databas
// Avoid updating dump everytime by dumping the schema only when the database metadata is changed.
// if oldDatabaseMetadata is nil and databaseMetadata is not, they are not equal resulting a sync.
- if force || !equalDatabaseMetadata(oldDatabaseMetadata, databaseMetadata) {
+ if force || !common.EqualDatabaseSchemaMetadataFast(oldDatabaseMetadata, databaseMetadata) {
var schemaBuf bytes.Buffer
if _, err := driver.Dump(ctx, &schemaBuf); err != nil {
return errors.Wrapf(err, "failed to dump database schema for database %q", database.DatabaseName)
@@ -599,12 +599,6 @@ func equalInstanceMetadata(x, y *storepb.InstanceMetadata) bool {
return cmp.Equal(x, y, protocmp.Transform(), protocmp.IgnoreFields(&storepb.InstanceMetadata{}, "last_sync_time"))
}
-func equalDatabaseMetadata(x, y *storepb.DatabaseSchemaMetadata) bool {
- return cmp.Equal(x, y, protocmp.Transform(),
- protocmp.IgnoreFields(&storepb.TableMetadata{}, "row_count", "data_size", "index_size", "data_free"),
- )
-}
-
func setClassificationAndUserCommentFromComment(dbSchema *storepb.DatabaseSchemaMetadata, databaseConfig *model.DatabaseConfig) {
for _, schema := range dbSchema.Schemas {
schemaConfig := databaseConfig.CreateOrGetSchemaConfig(schema.Name)
diff --git a/backend/runner/schemasync/syncer_test.go b/backend/runner/schemasync/syncer_test.go
index 6aebe5c78e..2725670350 100644
--- a/backend/runner/schemasync/syncer_test.go
+++ b/backend/runner/schemasync/syncer_test.go
@@ -5,10 +5,11 @@ import (
"github.com/stretchr/testify/assert"
+ "github.com/bytebase/bytebase/backend/common"
storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
-func TestEqualDatabaseMetadata(t *testing.T) {
+func TestEqualDatabaseMetadataFast(t *testing.T) {
tests := []struct {
x *storepb.DatabaseSchemaMetadata
y *storepb.DatabaseSchemaMetadata
@@ -22,7 +23,7 @@ func TestEqualDatabaseMetadata(t *testing.T) {
{
x: nil,
y: &storepb.DatabaseSchemaMetadata{},
- want: false,
+ want: true,
},
{
x: &storepb.DatabaseSchemaMetadata{
@@ -129,8 +130,9 @@ func TestEqualDatabaseMetadata(t *testing.T) {
want: true,
},
}
+
for _, test := range tests {
- got := equalDatabaseMetadata(test.x, test.y)
+ got := common.EqualDatabaseSchemaMetadataFast(test.x, test.y)
assert.Equal(t, test.want, got)
}
}