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: changelog view (#14900)
p0ny86572ce861a1
diff --git a/backend/api/v1/database_service_changelog.go b/backend/api/v1/database_service_changelog.goindex 2c887fc1bb..f34849f880 100644--- a/backend/api/v1/database_service_changelog.go+++ b/backend/api/v1/database_service_changelog.go@@ -55,12 +55,14 @@ func (s *DatabaseService) ListChangelogs(ctx context.Context, request *v1pb.List}limitPlusOne := offset.limit + 1- // TODO(p0ny): support view.find := &store.FindChangelogMessage{DatabaseUID: &database.UID,Limit: &limitPlusOne,Offset: &offset.offset,}+ if request.View == v1pb.ChangelogView_CHANGELOG_VIEW_FULL {+ find.ShowFull = true+ }filters, err := ParseFilter(request.Filter)if err != nil {@@ -111,8 +113,14 @@ func (s *DatabaseService) GetChangelog(ctx context.Context, request *v1pb.GetChareturn nil, status.Error(codes.InvalidArgument, err.Error())}- // TODO(p0ny): support view.- changelog, err := s.store.GetChangelog(ctx, changelogUID)+ find := &store.FindChangelogMessage{+ UID: &changelogUID,+ }+ if request.View == v1pb.ChangelogView_CHANGELOG_VIEW_FULL {+ find.ShowFull = true+ }++ changelog, err := s.store.GetChangelog(ctx, find)if err != nil {return nil, status.Errorf(codes.Internal, "failed to list changelogs, errors: %v", err)}@@ -220,21 +228,9 @@ func (s *DatabaseService) convertToChangelog(ctx context.Context, d *store.Datab}if sheet := c.Payload.GetSheet(); sheet != "" {- _, sheetUID, err := common.GetProjectResourceIDSheetUID(sheet)- if err != nil {- return nil, errors.Wrapf(err, "failed to get sheetUID from %q", sheet)- }- sheetM, err := s.store.GetSheet(ctx, &store.FindSheetMessage{UID: &sheetUID})- if err != nil {- return nil, errors.Wrapf(err, "failed to get sheet %q", sheet)- }- if sheetM == nil {- return nil, errors.Errorf("sheet %q not found", sheet)- }-cl.StatementSheet = sheet- cl.Statement = sheetM.Statement- cl.StatementSize = sheetM.Size+ cl.Statement = c.Statement+ cl.StatementSize = c.StatementSize}if id := c.Payload.GetRevision(); id != 0 {@@ -248,22 +244,12 @@ func (s *DatabaseService) convertToChangelog(ctx context.Context, d *store.Databcl.Creator = common.FormatUserEmail(creator.Email)if v := c.PrevSyncHistoryUID; v != nil {- id := *v- h, err := s.store.GetSyncHistoryByUID(ctx, id)- if err != nil {- return nil, errors.Wrapf(err, "failed to get sync history %d", id)- }- cl.PrevSchema = h.Schema+ cl.PrevSchema = c.PrevSchemacl.PrevSchemaSize = int64(len(cl.PrevSchema))}if v := c.SyncHistoryUID; v != nil {- id := *v- h, err := s.store.GetSyncHistoryByUID(ctx, id)- if err != nil {- return nil, errors.Wrapf(err, "failed to get sync history %d", id)- }- cl.Schema = h.Schema+ cl.Schema = c.Schemacl.SchemaSize = int64(len(cl.Schema))}diff --git a/backend/runner/schemasync/syncer.go b/backend/runner/schemasync/syncer.goindex 44b643d212..cc1ed5f69a 100644--- a/backend/runner/schemasync/syncer.go+++ b/backend/runner/schemasync/syncer.go@@ -588,6 +588,7 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.DatabasTypeList: []string{string(db.Migrate), string(db.Baseline)},HasSyncHistory: true,Limit: &limit,+ ShowFull: true,})if err != nil {return errors.Wrapf(err, "failed to list changelogs")@@ -600,15 +601,11 @@ func (s *Syncer) SyncDatabaseSchema(ctx context.Context, database *store.Databasif changelog.SyncHistoryUID == nil {return errors.Errorf("expect sync history but get nil")}- syncHistory, err := s.store.GetSyncHistoryByUID(ctx, *changelog.SyncHistoryUID)- if err != nil {- return errors.Wrapf(err, "failed to get sync history")- }latestSchema := string(rawDump)- if syncHistory.Schema != latestSchema {+ if changelog.Schema != latestSchema {anomalyPayload := &storepb.AnomalyDatabaseSchemaDriftPayload{Version: changelog.Payload.GetVersion(),- Expect: syncHistory.Schema,+ Expect: changelog.Schema,Actual: latestSchema,}payload, err := protojson.Marshal(anomalyPayload)diff --git a/backend/store/changelog.go b/backend/store/changelog.goindex 5aee0d3f73..7fa891c971 100644--- a/backend/store/changelog.go+++ b/backend/store/changelog.go@@ -34,6 +34,11 @@ type ChangelogMessage struct {UID int64CreatorUID intCreatedTime time.Time++ PrevSchema string+ Schema string+ Statement string+ StatementSize int64}type FindChangelogMessage struct {@@ -46,6 +51,8 @@ type FindChangelogMessage struct {Limit *intOffset *int+ // If false, PrevSchema, Schema are truncated+ ShowFull boolHasSyncHistory bool}@@ -168,6 +175,23 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)args = append(args, find.TypeList)}+ truncateSize := 512+ if common.IsDev() {+ truncateSize = 4+ }+ shPreField := fmt.Sprintf("LEFT(sh_pre.raw_dump, %d)", truncateSize)+ if find.ShowFull {+ shPreField = "sh_pre.raw_dump"+ }+ shCurField := fmt.Sprintf("LEFT(sh_cur.raw_dump, %d)", truncateSize)+ if find.ShowFull {+ shCurField = "sh_cur.raw_dump"+ }+ sheetField := fmt.Sprintf("LEFT(sheet_blob.content, %d)", truncateSize)+ if find.ShowFull {+ sheetField = "sheet_blob.content"+ }+query := fmt.Sprintf(`SELECTchangelog.id,@@ -177,11 +201,23 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)changelog.status,changelog.prev_sync_history_id,changelog.sync_history_id,- payload+ COALESCE(%s, ''),+ COALESCE(%s, ''),+ COALESCE(%s, ''),+ OCTET_LENGTH(sheet_blob.content),+ changelog.payloadFROM changelog+ LEFT JOIN sync_history sh_pre ON sh_pre.id = changelog.prev_sync_history_id+ LEFT JOIN sync_history sh_cur ON sh_cur.id = changelog.sync_history_id+ LEFT JOIN sheet ON sheet.id::text = split_part(changelog.payload->>'sheet', '/', 4)+ LEFT JOIN sheet_blob ON sheet.sha256 = sheet_blob.sha256WHERE %sORDER BY changelog.id DESC- `, strings.Join(where, " AND "))+ `,+ shPreField,+ shCurField,+ sheetField,+ strings.Join(where, " AND "))if v := find.Limit; v != nil {query += fmt.Sprintf(" LIMIT %d", *v)}@@ -210,6 +246,10 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)&c.Status,&c.PrevSyncHistoryUID,&c.SyncHistoryUID,+ &c.PrevSchema,+ &c.Schema,+ &c.Statement,+ &c.StatementSize,&payload,); err != nil {return nil, errors.Wrapf(err, "failed to scan")@@ -229,10 +269,8 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)return changelogs, nil}-func (s *Store) GetChangelog(ctx context.Context, uid int64) (*ChangelogMessage, error) {- changelogs, err := s.ListChangelogs(ctx, &FindChangelogMessage{- UID: &uid,- })+func (s *Store) GetChangelog(ctx context.Context, find *FindChangelogMessage) (*ChangelogMessage, error) {+ changelogs, err := s.ListChangelogs(ctx, find)if err != nil {return nil, err}@@ -240,7 +278,7 @@ func (s *Store) GetChangelog(ctx context.Context, uid int64) (*ChangelogMessage,return nil, nil}if len(changelogs) > 1 {- return nil, errors.Errorf("found %d changelogs with UID %d, expect 1", len(changelogs), uid)+ return nil, errors.Errorf("found %d changelogs with find %v, expect 1", len(changelogs), *find)}return changelogs[0], nil}diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.goindex db2d4d7b93..d4937e6b38 100644--- a/backend/tests/schema_update_test.go+++ b/backend/tests/schema_update_test.go@@ -93,6 +93,7 @@ func TestSchemaAndDataUpdate(t *testing.T) {resp, err := ctl.databaseServiceClient.ListChangelogs(ctx, &v1pb.ListChangelogsRequest{Parent: database.Name,+ View: v1pb.ChangelogView_CHANGELOG_VIEW_FULL,})a.NoError(err)changelogs := resp.Changelogsdiff --git a/backend/tests/sync_schema_test.go b/backend/tests/sync_schema_test.goindex f4292ddb26..8296c94769 100644--- a/backend/tests/sync_schema_test.go+++ b/backend/tests/sync_schema_test.go@@ -107,6 +107,7 @@ DROP SCHEMA "schema_a";resp, err := ctl.databaseServiceClient.ListChangelogs(ctx, &v1pb.ListChangelogsRequest{Parent: database.Name,+ View: v1pb.ChangelogView_CHANGELOG_VIEW_FULL,})a.NoError(err)changelogs := resp.Changelogsdiff --git a/backend/tests/vcs_test.go b/backend/tests/vcs_test.goindex 656c9533d3..5169ebf6a6 100644--- a/backend/tests/vcs_test.go+++ b/backend/tests/vcs_test.go@@ -262,6 +262,7 @@ func TestVCS(t *testing.T) {resp, err := ctl.databaseServiceClient.ListChangelogs(ctx, &v1pb.ListChangelogsRequest{Parent: database.Name,+ View: v1pb.ChangelogView_CHANGELOG_VIEW_FULL,})a.NoError(err)changelogs := resp.Changelogs