Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: add git version to changelog and use it for drift detection (#15961)
Danny Xu584972ac546d
diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.goindex 083a452741..902a2f2875 100644--- a/backend/runner/schemasync/syncer.go+++ b/backend/runner/schemasync/syncer.go@@ -20,6 +20,7 @@ import ("github.com/bytebase/bytebase/backend/base""github.com/bytebase/bytebase/backend/common""github.com/bytebase/bytebase/backend/common/log"+ "github.com/bytebase/bytebase/backend/component/config""github.com/bytebase/bytebase/backend/component/dbfactory""github.com/bytebase/bytebase/backend/component/state""github.com/bytebase/bytebase/backend/plugin/db"@@ -41,10 +42,11 @@ const ()// NewSyncer creates a schema syncer.-func NewSyncer(stores *store.Store, dbFactory *dbfactory.DBFactory, stateCfg *state.State) *Syncer {+func NewSyncer(stores *store.Store, dbFactory *dbfactory.DBFactory, profile *config.Profile, stateCfg *state.State) *Syncer {return &Syncer{store: stores,dbFactory: dbFactory,+ profile: profile,stateCfg: stateCfg,}}@@ -55,6 +57,7 @@ type Syncer struct {store *store.StoredbFactory *dbfactory.DBFactory+ profile *config.ProfilestateCfg *state.StatedatabaseSyncMap sync.Map // map[string]*store.DatabaseMessage}@@ -518,11 +521,13 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.Databasmetadata.LastSyncTime = timestamppb.New(time.Now())metadata.BackupAvailable = s.hasBackupSchema(ctx, instance, databaseMetadata)metadata.Datashare = databaseMetadata.Datashare- drifted, err := s.getSchemaDrifted(ctx, instance, database, string(rawDump))+ drifted, skipped, err := s.getSchemaDrifted(ctx, instance, database, string(rawDump))if err != nil {return errors.Wrapf(err, "failed to get schema drifted for database %q", database.DatabaseName)}- metadata.Drifted = drifted+ if !skipped {+ metadata.Drifted = drifted+ }if _, err := s.store.UpdateDatabase(ctx, &store.UpdateDatabaseMessage{InstanceID: database.InstanceID,DatabaseName: database.DatabaseName,@@ -547,10 +552,10 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.Databasreturn nil}-func (s *Syncer) getSchemaDrifted(ctx context.Context, instance *store.InstanceMessage, database *store.DatabaseMessage, rawDump string) (bool, error) {+func (s *Syncer) getSchemaDrifted(ctx context.Context, instance *store.InstanceMessage, database *store.DatabaseMessage, rawDump string) (drifted bool, skipped bool, err error) {// Redis and MongoDB are schemaless.if disableSchemaDriftCheck(instance.Metadata.GetEngine()) {- return false, nil+ return false, false, nil}limit := 1list, err := s.store.ListChangelogs(ctx, &store.FindChangelogMessage{@@ -562,18 +567,21 @@ func (s *Syncer) getSchemaDrifted(ctx context.Context, instance *store.InstanceMShowFull: true,})if err != nil {- return false, errors.Wrapf(err, "failed to list changelogs")+ return false, false, errors.Wrapf(err, "failed to list changelogs")}if len(list) == 0 {- return false, nil+ return false, false, nil}changelog := list[0]+ if changelog.Payload.GetGitCommit() != s.profile.GitCommit {+ return false, true, nil+ }if changelog.SyncHistoryUID == nil {- return false, errors.Errorf("expect sync history but get nil")+ return false, false, errors.Errorf("expect sync history but get nil")}latestSchema := string(rawDump)- return changelog.Schema != latestSchema, nil+ return changelog.Schema != latestSchema, false, nil}func (s *Syncer) hasBackupSchema(ctx context.Context, instance *store.InstanceMessage, dbSchema *storepb.DatabaseSchemaMetadata) bool {diff --git a/backend/server/server.go b/backend/server/server.goindex 77eb6a6155..e00882de65 100644--- a/backend/server/server.go+++ b/backend/server/server.go@@ -223,7 +223,7 @@ func NewServer(ctx context.Context, profile *config.Profile) (*Server, error) {)s.metricReporter = metricreport.NewReporter(s.store, s.licenseService, s.profile)- s.schemaSyncer = schemasync.NewSyncer(stores, s.dbFactory, s.stateCfg)+ s.schemaSyncer = schemasync.NewSyncer(stores, s.dbFactory, s.profile, s.stateCfg)s.approvalRunner = approval.NewRunner(stores, sheetManager, s.dbFactory, s.stateCfg, s.webhookManager, s.licenseService)s.taskSchedulerV2 = taskrun.NewSchedulerV2(stores, s.stateCfg, s.webhookManager, profile, s.licenseService)diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.mdindex 386f7960e4..661b39a8c0 100644--- a/proto/gen/grpc-doc/store/README.md+++ b/proto/gen/grpc-doc/store/README.md@@ -964,6 +964,7 @@ Metadata about the request.| sheet | [string](#string) | | The sheet that holds the content. Format: projects/{project}/sheets/{sheet} || version | [string](#string) | | || type | [ChangelogPayload.Type](#bytebase-store-ChangelogPayload-Type) | | |+| git_commit | [string](#string) | | |diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.htmlindex 918b607d39..8f25605115 100644--- a/proto/gen/grpc-doc/store/index.html+++ b/proto/gen/grpc-doc/store/index.html@@ -2969,6 +2969,13 @@ Format: projects/{project}/sheets/{sheet} </p></td><td><p> </p></td></tr>+ <tr>+ <td>git_commit</td>+ <td><a href="#string">string</a></td>+ <td></td>+ <td><p> </p></td>+ </tr>+</tbody></table>diff --git a/proto/generated-go/store/changelog.pb.go b/proto/generated-go/store/changelog.pb.goindex 28ab9a36b1..63f6458929 100644--- a/proto/generated-go/store/changelog.pb.go+++ b/proto/generated-go/store/changelog.pb.go@@ -94,6 +94,7 @@ type ChangelogPayload struct {Sheet string `protobuf:"bytes,5,opt,name=sheet,proto3" json:"sheet,omitempty"`Version string `protobuf:"bytes,6,opt,name=version,proto3" json:"version,omitempty"`Type ChangelogPayload_Type `protobuf:"varint,7,opt,name=type,proto3,enum=bytebase.store.ChangelogPayload_Type" json:"type,omitempty"`+ GitCommit string `protobuf:"bytes,8,opt,name=git_commit,json=gitCommit,proto3" json:"git_commit,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCache}@@ -177,6 +178,13 @@ func (x *ChangelogPayload) GetType() ChangelogPayload_Type {return ChangelogPayload_TYPE_UNSPECIFIED}+func (x *ChangelogPayload) GetGitCommit() string {+ if x != nil {+ return x.GitCommit+ }+ return ""+}+type ChangedResources struct {state protoimpl.MessageState `protogen:"open.v1"`Databases []*ChangedResourceDatabase `protobuf:"bytes,1,rep,name=databases,proto3" json:"databases,omitempty"`@@ -574,7 +582,7 @@ var File_store_changelog_proto protoreflect.FileDescriptorconst file_store_changelog_proto_rawDesc = "" +"\n" +- "\x15store/changelog.proto\x12\x0ebytebase.store\x1a\x12store/common.proto\"\x80\x03\n" ++ "\x15store/changelog.proto\x12\x0ebytebase.store\x1a\x12store/common.proto\"\x9f\x03\n" +"\x10ChangelogPayload\x12\x19\n" +"\btask_run\x18\x01 \x01(\tR\ataskRun\x12\x14\n" +"\x05issue\x18\x02 \x01(\tR\x05issue\x12\x1a\n" +@@ -582,7 +590,9 @@ const file_store_changelog_proto_rawDesc = "" +"\x11changed_resources\x18\x04 \x01(\v2 .bytebase.store.ChangedResourcesR\x10changedResources\x12\x14\n" +"\x05sheet\x18\x05 \x01(\tR\x05sheet\x12\x18\n" +"\aversion\x18\x06 \x01(\tR\aversion\x129\n" +- "\x04type\x18\a \x01(\x0e2%.bytebase.store.ChangelogPayload.TypeR\x04type\"e\n" ++ "\x04type\x18\a \x01(\x0e2%.bytebase.store.ChangelogPayload.TypeR\x04type\x12\x1d\n" ++ "\n" ++ "git_commit\x18\b \x01(\tR\tgitCommit\"e\n" +"\x04Type\x12\x14\n" +"\x10TYPE_UNSPECIFIED\x10\x00\x12\f\n" +"\bBASELINE\x10\x01\x12\v\n" +diff --git a/proto/store/store/changelog.proto b/proto/store/store/changelog.protoindex e3733972d8..ed16c18c01 100644--- a/proto/store/store/changelog.proto+++ b/proto/store/store/changelog.proto@@ -33,6 +33,8 @@ message ChangelogPayload {DATA = 6;}Type type = 7;++ string git_commit = 8;}message ChangedResources {