Skip to content

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

chore: add database metadata compare function by ignoring the usage related fields (#3873)

Danny Xudeec5ca6157e
diff --git a/go.mod b/go.mod
index a07adb0013..9d5749425e 100644
--- a/go.mod
+++ b/go.mod
@@ -15,6 +15,7 @@ require (
github.com/github/gh-ost v1.1.5
github.com/go-sql-driver/mysql v1.6.0
github.com/golang-jwt/jwt/v4 v4.4.2
+ github.com/google/go-cmp v0.5.9
github.com/google/jsonapi v1.0.0
github.com/google/uuid v1.3.0
github.com/gosimple/slug v1.13.1
diff --git a/server/runner/schemasync/syncer.go b/server/runner/schemasync/syncer.go
index c6b99254b9..8bc0457a8c 100644
--- a/server/runner/schemasync/syncer.go
+++ b/server/runner/schemasync/syncer.go
@@ -9,10 +9,12 @@ import (
"sync"
"time"
+ "github.com/google/go-cmp/cmp"
"github.com/labstack/echo/v4"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/protobuf/encoding/protojson"
+ "google.golang.org/protobuf/testing/protocmp"
"github.com/bytebase/bytebase/api"
"github.com/bytebase/bytebase/common"
@@ -431,17 +433,32 @@ func syncDBSchema(ctx context.Context, store *store.Store, database *api.Databas
}
databaseMetadata := convertDBSchema(schema)
- bytes, err := protojson.Marshal(databaseMetadata)
- if err != nil {
- return err
+ var oldDatabaseMetadata *storepb.DatabaseMetadata
+ if dbSchema != nil {
+ var m storepb.DatabaseMetadata
+ if err := protojson.Unmarshal([]byte(dbSchema.Metadata), &m); err != nil {
+ return err
+ }
+ oldDatabaseMetadata = &m
}
- metadataString := string(bytes)
- if dbSchema == nil || dbSchema.Metadata != metadataString {
+
+ if !cmp.Equal(oldDatabaseMetadata, databaseMetadata, protocmp.Transform()) {
+ bytes, err := protojson.Marshal(databaseMetadata)
+ if err != nil {
+ return err
+ }
+ metadata := string(bytes)
+ // TODO(d): avoid updating dump everytime if possible.
+ rawDump := ""
+ if dbSchema != nil {
+ rawDump = dbSchema.RawDump
+ }
+
if _, err := store.UpsertDBSchema(ctx, api.DBSchemaUpsert{
UpdatorID: api.SystemBotID,
DatabaseID: database.ID,
- Metadata: metadataString,
- RawDump: "",
+ Metadata: metadata,
+ RawDump: rawDump,
}); err != nil {
return err
}
@@ -450,7 +467,6 @@ func syncDBSchema(ctx context.Context, store *store.Store, database *api.Databas
}
func convertDBSchema(schema *db.Schema) *storepb.DatabaseMetadata {
- // TODO(d): add unit test.
databaseMetadata := &storepb.DatabaseMetadata{
Name: schema.Name,
CharacterSet: schema.CharacterSet,
@@ -571,3 +587,9 @@ func convertDBSchema(schema *db.Schema) *storepb.DatabaseMetadata {
}
return databaseMetadata
}
+
+func equalDatabaseMetadata(x, y *storepb.DatabaseMetadata) bool {
+ return cmp.Equal(x, y, protocmp.Transform(),
+ protocmp.IgnoreFields(&storepb.TableMetadata{}, "row_count", "data_size", "index_size", "data_free"),
+ )
+}
diff --git a/server/runner/schemasync/syncer_test.go b/server/runner/schemasync/syncer_test.go
index caf28b6558..5e24a19eb7 100644
--- a/server/runner/schemasync/syncer_test.go
+++ b/server/runner/schemasync/syncer_test.go
@@ -294,3 +294,130 @@ func TestConvertDBSchema(t *testing.T) {
got := convertDBSchema(dbSchema)
assert.Equal(t, want, got)
}
+
+func TestEqualDatabaseMetadata(t *testing.T) {
+ tests := []struct {
+ x *storepb.DatabaseMetadata
+ y *storepb.DatabaseMetadata
+ want bool
+ }{
+ {
+ x: &storepb.DatabaseMetadata{},
+ y: &storepb.DatabaseMetadata{},
+ want: true,
+ },
+ {
+ x: nil,
+ y: &storepb.DatabaseMetadata{},
+ want: false,
+ },
+ {
+ x: &storepb.DatabaseMetadata{
+ Name: "hello",
+ },
+ y: &storepb.DatabaseMetadata{
+ Name: "world",
+ },
+ want: false,
+ },
+ {
+ x: &storepb.DatabaseMetadata{
+ Name: "hello",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "students",
+ },
+ },
+ },
+ },
+ },
+ y: &storepb.DatabaseMetadata{
+ Name: "hello",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "students",
+ },
+ },
+ },
+ },
+ },
+ want: true,
+ },
+ {
+ x: &storepb.DatabaseMetadata{
+ Name: "hello",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "teachers",
+ },
+ },
+ },
+ },
+ },
+ y: &storepb.DatabaseMetadata{
+ Name: "hello",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "students",
+ },
+ },
+ },
+ },
+ },
+ want: false,
+ },
+ {
+ x: &storepb.DatabaseMetadata{
+ Name: "hello",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "students",
+ RowCount: 321,
+ DataSize: 321,
+ IndexSize: 321,
+ DataFree: 321,
+ },
+ },
+ },
+ },
+ },
+ y: &storepb.DatabaseMetadata{
+ Name: "hello",
+ Schemas: []*storepb.SchemaMetadata{
+ {
+ Name: "public",
+ Tables: []*storepb.TableMetadata{
+ {
+ Name: "students",
+ RowCount: 123,
+ DataSize: 123,
+ IndexSize: 123,
+ DataFree: 123,
+ },
+ },
+ },
+ },
+ },
+ want: true,
+ },
+ }
+ for _, test := range tests {
+ got := equalDatabaseMetadata(test.x, test.y)
+ assert.Equal(t, test.want, got)
+ }
+}