Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: migrate instance change history to protobuf and return push event in v1 api (#6395)
p0nye80b4eea834d
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.goindex acecf8ca06..4c67826e1b 100644--- a/backend/api/v1/database_service.go+++ b/backend/api/v1/database_service.go@@ -731,6 +731,7 @@ func convertToChangeHistory(h *store.InstanceChangeHistoryMessage) (*v1pb.ChangeSchema: h.Schema,PrevSchema: h.SchemaPrev,ExecutionDuration: durationpb.New(time.Duration(h.ExecutionDurationNs)),+ PushEvent: convertToPushEvent(h.Payload.GetPushEvent()),Review: "",}if h.IssueUID != nil {@@ -739,6 +740,74 @@ func convertToChangeHistory(h *store.InstanceChangeHistoryMessage) (*v1pb.Changereturn v1pbHistory, nil}+func convertToPushEvent(e *storepb.PushEvent) *v1pb.PushEvent {+ if e == nil {+ return nil+ }+ return &v1pb.PushEvent{+ VcsType: convertToVcsType(e.VcsType),+ BaseDir: e.BaseDir,+ Ref: e.Ref,+ Before: e.Before,+ After: e.After,+ RepositoryId: e.RepositoryId,+ RepositoryUrl: e.RepositoryUrl,+ RepositoryFullPath: e.RepositoryFullPath,+ AuthorName: e.AuthorName,+ Commits: convertToCommits(e.Commits),+ FileCommit: convertToFileCommit(e.FileCommit),+ }+}++func convertToVcsType(t storepb.VcsType) v1pb.VcsType {+ switch t {+ case storepb.VcsType_GITLAB:+ return v1pb.VcsType_GITLAB+ case storepb.VcsType_GITHUB:+ return v1pb.VcsType_GITHUB+ case storepb.VcsType_BITBUCKET:+ return v1pb.VcsType_BITBUCKET+ case storepb.VcsType_VCS_TYPE_UNSPECIFIED:+ return v1pb.VcsType_VCS_TYPE_UNSPECIFIED+ default:+ return v1pb.VcsType_VCS_TYPE_UNSPECIFIED+ }+}++func convertToCommits(commits []*storepb.Commit) []*v1pb.Commit {+ var converted []*v1pb.Commit+ for _, c := range commits {+ converted = append(converted, &v1pb.Commit{+ Id: c.Id,+ Title: c.Title,+ Message: c.Message,+ CreatedTime: timestamppb.New(time.Unix(c.CreatedTs, 0)),+ Url: c.Url,+ AuthorName: c.AuthorName,+ AuthorEmail: c.AuthorEmail,+ AddedList: c.AddedList,+ ModifiedList: c.ModifiedList,+ })+ }+ return converted+}++func convertToFileCommit(c *storepb.FileCommit) *v1pb.FileCommit {+ if c == nil {+ return nil+ }+ return &v1pb.FileCommit{+ Id: c.Id,+ Title: c.Title,+ Message: c.Message,+ CreatedTime: timestamppb.New(time.Unix(c.CreatedTs, 0)),+ Url: c.Url,+ AuthorName: c.AuthorName,+ AuthorEmail: c.AuthorEmail,+ Added: c.Added,+ }+}+func convertToChangeHistorySource(source db.MigrationSource) v1pb.ChangeHistory_Source {switch source {case db.UI:diff --git a/backend/migrator/migrator.go b/backend/migrator/migrator.goindex 112779f369..5f39504e62 100644--- a/backend/migrator/migrator.go+++ b/backend/migrator/migrator.go@@ -24,6 +24,7 @@ import ("github.com/bytebase/bytebase/backend/plugin/db/util""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/utils"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store")//go:embed migration@@ -170,7 +171,7 @@ func backfillHistory(ctx context.Context, storeInstance *store.Store, bytebasePgSchema: h.Schema,SchemaPrev: h.SchemaPrev,ExecutionDurationNs: h.ExecutionDurationNs,- Payload: h.Payload,+ Payload: &storepb.InstanceChangeHistoryPayload{},}creates = append(creates, &changeHistory)@@ -351,7 +352,7 @@ func initializeSchema(ctx context.Context, storeInstance *store.Store, metadataDSchema: stmt,SchemaPrev: "",ExecutionDurationNs: 0,- Payload: "",+ Payload: nil,}); err != nil {return err}diff --git a/backend/plugin/db/driver.go b/backend/plugin/db/driver.goindex 815132e6aa..e320a7bcd9 100644--- a/backend/plugin/db/driver.go+++ b/backend/plugin/db/driver.go@@ -195,7 +195,7 @@ type MigrationInfo struct {Creator stringIssueID string// Payload contains JSON-encoded string of VCS push event if the migration is triggered by a VCS push event.- Payload string+ Payload *storepb.InstanceChangeHistoryPayload// UseSemanticVersion is whether version is a semantic version.// When UseSemanticVersion is set, version should be set to the format specified in Semantic Versioning 2.0.0 (https://semver.org/).// For example, for setting non-semantic version "hello", the values should be Version = "hello", UseSemanticVersion = false, SemanticVersionSuffix = "".diff --git a/backend/runner/taskrun/executor.go b/backend/runner/taskrun/executor.goindex 78f0ca698b..86ebd2cab9 100644--- a/backend/runner/taskrun/executor.go+++ b/backend/runner/taskrun/executor.go@@ -26,6 +26,8 @@ import (parser "github.com/bytebase/bytebase/backend/plugin/parser/sql""github.com/bytebase/bytebase/backend/plugin/parser/sql/transform"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"+vcsPlugin "github.com/bytebase/bytebase/backend/plugin/vcs""github.com/bytebase/bytebase/backend/store""github.com/bytebase/bytebase/backend/utils"@@ -121,14 +123,9 @@ func preMigration(ctx context.Context, stores *store.Store, profile config.Profi} else {mi.Source = db.VCSmi.Creator = vcsPushEvent.AuthorName- miPayload := &db.MigrationInfoPayload{- VCSPushEvent: vcsPushEvent,- }- bytes, err := json.Marshal(miPayload)- if err != nil {- return nil, errors.Wrap(err, "failed to prepare for database migration, unable to marshal vcs push event payload")+ mi.Payload = &storepb.InstanceChangeHistoryPayload{+ PushEvent: utils.ConvertVcsPushEvent(vcsPushEvent),}- mi.Payload = string(bytes)}statement = strings.TrimSpace(statement)diff --git a/backend/store/instance_change_history.go b/backend/store/instance_change_history.goindex 9c094e59d0..6946b00da6 100644--- a/backend/store/instance_change_history.go+++ b/backend/store/instance_change_history.go@@ -8,9 +8,11 @@ import ("strings""github.com/pkg/errors"+ "google.golang.org/protobuf/encoding/protojson""github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/db/util"+ storepb "github.com/bytebase/bytebase/proto/generated-go/store")// InstanceChangeHistoryMessage records the change history of an instance.@@ -35,7 +37,7 @@ type InstanceChangeHistoryMessage struct {Schema stringSchemaPrev stringExecutionDurationNs int64- Payload string+ Payload *storepb.InstanceChangeHistoryPayload// Output onlyUID string@@ -124,8 +126,9 @@ func (*Store) createInstanceChangeHistoryImpl(ctx context.Context, tx *Tx, creatcount := 1for _, create := range creates {- if create.Payload == "" {- create.Payload = "{}"+ payload, err := protojson.Marshal(create.Payload)+ if err != nil {+ return nil, err}values = append(values,create.CreatorID,@@ -144,7 +147,7 @@ func (*Store) createInstanceChangeHistoryImpl(ctx context.Context, tx *Tx, creatcreate.Schema,create.SchemaPrev,create.ExecutionDurationNs,- create.Payload,+ payload,)const countToPayload = 17var valueStr []string@@ -232,6 +235,11 @@ func convertInstanceChangeHistoryToMigrationHistory(change *InstanceChangeHistorreturn nil, err}+ payload, err := protojson.Marshal(change.Payload)+ if err != nil {+ return nil, err+ }+return &db.MigrationHistory{ID: change.UID,Creator: "",@@ -251,7 +259,7 @@ func convertInstanceChangeHistoryToMigrationHistory(change *InstanceChangeHistorSchemaPrev: change.SchemaPrev,ExecutionDurationNs: change.ExecutionDurationNs,IssueID: issueID,- Payload: change.Payload,+ Payload: string(payload),UseSemanticVersion: useSemanticVersion,SemanticVersionSuffix: semanticVersionSuffix,}, nil@@ -393,7 +401,7 @@ func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstancvar list []*InstanceChangeHistoryMessagefor rows.Next() {var changeHistory InstanceChangeHistoryMessage- var rowStatus string+ var rowStatus, payload stringvar instanceID, databaseID, issueID sql.NullInt32if err := rows.Scan(&changeHistory.UID,@@ -416,7 +424,7 @@ func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstanc&changeHistory.Schema,&changeHistory.SchemaPrev,&changeHistory.ExecutionDurationNs,- &changeHistory.Payload,+ &payload,&changeHistory.InstanceID,&changeHistory.DatabaseName,); err != nil {@@ -434,6 +442,10 @@ func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstancn := int(issueID.Int32)changeHistory.IssueUID = &n}+ changeHistory.Payload = &storepb.InstanceChangeHistoryPayload{}+ if err := protojson.Unmarshal([]byte(payload), changeHistory.Payload); err != nil {+ return nil, err+ }changeHistory.Deleted = convertRowStatusToDeleted(rowStatus)list = append(list, &changeHistory)diff --git a/backend/utils/utils.go b/backend/utils/utils.goindex 7372f2f8d4..1fa0ef40dc 100644--- a/backend/utils/utils.go+++ b/backend/utils/utils.go@@ -25,6 +25,7 @@ import ("github.com/bytebase/bytebase/backend/plugin/db""github.com/bytebase/bytebase/backend/plugin/db/oracle""github.com/bytebase/bytebase/backend/plugin/db/util"+ "github.com/bytebase/bytebase/backend/plugin/vcs""github.com/bytebase/bytebase/backend/store"storepb "github.com/bytebase/bytebase/proto/generated-go/store")@@ -845,3 +846,65 @@ func GetSecretMapFromDatabaseMessage(databaseMessage *store.DatabaseMessage) map}return materials}++func convertVcsPushEventType(vcsType vcs.Type) storepb.VcsType {+ switch vcsType {+ case "GITLAB":+ return storepb.VcsType_GITLAB+ case "GITHUB":+ return storepb.VcsType_GITHUB+ case "BITBUCKET":+ return storepb.VcsType_BITBUCKET+ default:+ return storepb.VcsType_VCS_TYPE_UNSPECIFIED+ }+}++func convertVcsPushEventCommits(commits []vcs.Commit) []*storepb.Commit {+ var result []*storepb.Commit+ for i := range commits {+ commit := &commits[i]+ result = append(result, &storepb.Commit{+ Id: commit.ID,+ Title: commit.Title,+ Message: commit.Message,+ CreatedTs: commit.CreatedTs,+ Url: commit.URL,+ AuthorName: commit.AuthorName,+ AuthorEmail: commit.AuthorEmail,+ AddedList: commit.AddedList,+ ModifiedList: commit.ModifiedList,+ })+ }+ return result+}++func convertVcsPushEventFileCommit(c *vcs.FileCommit) *storepb.FileCommit {+ return &storepb.FileCommit{+ Id: c.ID,+ Title: c.Title,+ Message: c.Message,+ CreatedTs: c.CreatedTs,+ Url: c.URL,+ AuthorName: c.AuthorName,+ AuthorEmail: c.AuthorEmail,+ Added: c.Added,+ }+}++// ConvertVcsPushEvent converts a vcs.pushEvent to a storepb.PushEvent.+func ConvertVcsPushEvent(pushEvent *vcs.PushEvent) *storepb.PushEvent {+ return &storepb.PushEvent{+ VcsType: convertVcsPushEventType(pushEvent.VCSType),+ BaseDir: pushEvent.BaseDirectory,+ Ref: pushEvent.Ref,+ Before: pushEvent.Before,+ After: pushEvent.After,+ RepositoryId: pushEvent.RepositoryID,+ RepositoryUrl: pushEvent.RepositoryURL,+ RepositoryFullPath: pushEvent.RepositoryFullPath,+ AuthorName: pushEvent.AuthorName,+ Commits: convertVcsPushEventCommits(pushEvent.CommitList),+ FileCommit: convertVcsPushEventFileCommit(&pushEvent.FileCommit),+ }+}