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(api): support create_time filter in changelogs API (#18849)
Vincent Huang5119228609ba
diff --git a/backend/api/mcp/gen/openapi.yaml b/backend/api/mcp/gen/openapi.yamlindex 1039b01896..41bbfa2dc9 100644--- a/backend/api/mcp/gen/openapi.yaml+++ b/backend/api/mcp/gen/openapi.yaml@@ -10986,13 +10986,15 @@ components:The syntax and semantics of CEL are documented at https://github.com/google/cel-specSupported filter:- status: the changelog status, support "==" operation. check Changelog.Status for available values.- type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ - status: the changelog status, support "==" operation. check Changelog.Status for available values.+ - type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ - create_time: the changelog create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.Example:status == "DONE"type in ["BASELINE", "MIGRATE"]status == "FAILED" && type == "SDL"+ create_time >= "2024-01-01T00:00:00Z" && create_time <= "2024-01-02T00:00:00Z"title: ListChangelogsRequestadditionalProperties: falsebytebase.v1.ListChangelogsResponse:diff --git a/backend/api/v1/database_service_changelog.go b/backend/api/v1/database_service_changelog.goindex 74a5868c62..9e9d7135c6 100644--- a/backend/api/v1/database_service_changelog.go+++ b/backend/api/v1/database_service_changelog.go@@ -2,6 +2,7 @@ package v1import ("context"+ "time""connectrpc.com/connect""google.golang.org/protobuf/types/known/timestamppb"@@ -85,6 +86,24 @@ func parseChangelogFilter(filter string, find *store.FindChangelogMessage) errordefault:return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unsupport variable %v", variable))}+ case celoperators.GreaterEquals, celoperators.LessEquals:+ variable, rawValue := getVariableAndValueFromExpr(expr)+ value, ok := rawValue.(string)+ if !ok {+ return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("expect string, got %T, hint: filter literals should be string", rawValue))+ }+ if variable != "create_time" {+ return connect.NewError(connect.CodeInvalidArgument, errors.Errorf(`">=" and "<=" are only supported for "create_time"`))+ }+ t, err := time.Parse(time.RFC3339, value)+ if err != nil {+ return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("failed to parse time %v, error: %v", value, err))+ }+ if functionName == celoperators.GreaterEquals {+ find.CreatedAtAfter = &t+ } else {+ find.CreatedAtBefore = &t+ }default:return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unexpected function %v", functionName))}diff --git a/backend/api/v1/database_service_changelog_test.go b/backend/api/v1/database_service_changelog_test.gonew file mode 100644index 0000000000..67af18030c--- /dev/null+++ b/backend/api/v1/database_service_changelog_test.go@@ -0,0 +1,138 @@+package v1++import (+ "testing"+ "time"++ "github.com/stretchr/testify/require"++ "github.com/bytebase/bytebase/backend/store"+)++func TestParseChangelogFilter(t *testing.T) {+ tests := []struct {+ name string+ filter string+ wantStatus *store.ChangelogStatus+ wantTypeList []string+ wantAfter *time.Time+ wantBefore *time.Time+ wantErr bool+ errContains string+ }{+ {+ name: "empty filter",+ filter: "",+ },+ {+ name: "status filter",+ filter: `status == "DONE"`,+ wantStatus: ptr(store.ChangelogStatusDone),+ },+ {+ name: "type filter with equals",+ filter: `type == "MIGRATE"`,+ wantTypeList: []string{"MIGRATE"},+ },+ {+ name: "type filter with in",+ filter: `type in ["BASELINE", "MIGRATE"]`,+ wantTypeList: []string{"BASELINE", "MIGRATE"},+ },+ {+ name: "create_time greater than or equal",+ filter: `create_time >= "2024-01-01T00:00:00Z"`,+ wantAfter: ptr(mustParseTime(t, "2024-01-01T00:00:00Z")),+ },+ {+ name: "create_time less than or equal",+ filter: `create_time <= "2024-12-31T23:59:59Z"`,+ wantBefore: ptr(mustParseTime(t, "2024-12-31T23:59:59Z")),+ },+ {+ name: "create_time range",+ filter: `create_time >= "2024-01-01T00:00:00Z" && create_time <= "2024-01-02T00:00:00Z"`,+ wantAfter: ptr(mustParseTime(t, "2024-01-01T00:00:00Z")),+ wantBefore: ptr(mustParseTime(t, "2024-01-02T00:00:00Z")),+ },+ {+ name: "combined status and time filter",+ filter: `status == "DONE" && create_time >= "2024-01-01T00:00:00Z"`,+ wantStatus: ptr(store.ChangelogStatusDone),+ wantAfter: ptr(mustParseTime(t, "2024-01-01T00:00:00Z")),+ },+ {+ name: "combined type and time filter",+ filter: `type in ["BASELINE", "MIGRATE"] && create_time <= "2024-12-31T23:59:59Z"`,+ wantTypeList: []string{"BASELINE", "MIGRATE"},+ wantBefore: ptr(mustParseTime(t, "2024-12-31T23:59:59Z")),+ },+ {+ name: "invalid time format",+ filter: `create_time >= "invalid-time"`,+ wantErr: true,+ errContains: "failed to parse time",+ },+ {+ name: "time comparison on wrong field",+ filter: `status >= "2024-01-01T00:00:00Z"`,+ wantErr: true,+ errContains: `">=" and "<=" are only supported for "create_time"`,+ },+ {+ name: "unsupported variable",+ filter: `unknown == "value"`,+ wantErr: true,+ errContains: "unsupport variable",+ },+ {+ name: "create_time with timezone offset",+ filter: `create_time >= "2024-01-01T00:00:00+05:30"`,+ wantAfter: ptr(mustParseTime(t, "2024-01-01T00:00:00+05:30")),+ },+ }++ for _, tt := range tests {+ t.Run(tt.name, func(t *testing.T) {+ find := &store.FindChangelogMessage{}+ err := parseChangelogFilter(tt.filter, find)++ if tt.wantErr {+ require.Error(t, err)+ if tt.errContains != "" {+ require.ErrorContains(t, err, tt.errContains)+ }+ return+ }++ require.NoError(t, err)++ if tt.wantStatus != nil {+ require.NotNil(t, find.Status)+ require.Equal(t, *tt.wantStatus, *find.Status)+ }+ if tt.wantTypeList != nil {+ require.Equal(t, tt.wantTypeList, find.TypeList)+ }+ if tt.wantAfter != nil {+ require.NotNil(t, find.CreatedAtAfter)+ require.Equal(t, *tt.wantAfter, *find.CreatedAtAfter)+ }+ if tt.wantBefore != nil {+ require.NotNil(t, find.CreatedAtBefore)+ require.Equal(t, *tt.wantBefore, *find.CreatedAtBefore)+ }+ })+ }+}++func mustParseTime(t *testing.T, s string) time.Time {+ t.Helper()+ parsedTime, err := time.Parse(time.RFC3339, s)+ require.NoError(t, err)+ return parsedTime+}++func ptr[T any](v T) *T {+ return &v+}diff --git a/backend/generated-go/v1/database_service.pb.go b/backend/generated-go/v1/database_service.pb.goindex b787895afb..d190b26f58 100644--- a/backend/generated-go/v1/database_service.pb.go+++ b/backend/generated-go/v1/database_service.pb.go@@ -5168,13 +5168,15 @@ type ListChangelogsRequest struct {// The syntax and semantics of CEL are documented at https://github.com/google/cel-spec//// Supported filter:- // status: the changelog status, support "==" operation. check Changelog.Status for available values.- // type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ // - status: the changelog status, support "==" operation. check Changelog.Status for available values.+ // - type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ // - create_time: the changelog create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.//// Example:// status == "DONE"// type in ["BASELINE", "MIGRATE"]// status == "FAILED" && type == "SDL"+ // create_time >= "2024-01-01T00:00:00Z" && create_time <= "2024-01-02T00:00:00Z"Filter string `protobuf:"bytes,5,opt,name=filter,proto3" json:"filter,omitempty"`unknownFields protoimpl.UnknownFieldssizeCache protoimpl.SizeCachediff --git a/backend/store/changelog.go b/backend/store/changelog.goindex f3975be980..3fe525a723 100644--- a/backend/store/changelog.go+++ b/backend/store/changelog.go@@ -43,8 +43,10 @@ type FindChangelogMessage struct {InstanceID *stringDatabaseName *string- TypeList []string- Status *ChangelogStatus+ TypeList []string+ Status *ChangelogStatus+ CreatedAtBefore *time.Time+ CreatedAtAfter *time.TimeLimit *intOffset *int@@ -198,6 +200,12 @@ func (s *Store) ListChangelogs(ctx context.Context, find *FindChangelogMessage)if len(find.TypeList) > 0 {q.And("changelog.payload->>'type' = ANY(?)", find.TypeList)}+ if v := find.CreatedAtBefore; v != nil {+ q.And("changelog.created_at <= ?", *v)+ }+ if v := find.CreatedAtAfter; v != nil {+ q.And("changelog.created_at >= ?", *v)+ }q.Space("ORDER BY changelog.id DESC")if v := find.Limit; v != nil {diff --git a/frontend/src/types/proto-es/v1/database_service_pb.d.ts b/frontend/src/types/proto-es/v1/database_service_pb.d.tsindex 2526d308c8..dc41d88f2b 100644--- a/frontend/src/types/proto-es/v1/database_service_pb.d.ts+++ b/frontend/src/types/proto-es/v1/database_service_pb.d.ts@@ -2950,13 +2950,15 @@ export declare type ListChangelogsRequest = Message<"bytebase.v1.ListChangelogsR* The syntax and semantics of CEL are documented at https://github.com/google/cel-spec** Supported filter:- * status: the changelog status, support "==" operation. check Changelog.Status for available values.- * type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ * - status: the changelog status, support "==" operation. check Changelog.Status for available values.+ * - type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ * - create_time: the changelog create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.** Example:* status == "DONE"* type in ["BASELINE", "MIGRATE"]* status == "FAILED" && type == "SDL"+ * create_time >= "2024-01-01T00:00:00Z" && create_time <= "2024-01-02T00:00:00Z"** @generated from field: string filter = 5;*/diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yamlindex f687d81645..a453f5449b 100644--- a/proto/gen/grpc-doc/openapi.yaml+++ b/proto/gen/grpc-doc/openapi.yaml@@ -1500,13 +1500,15 @@ paths:The syntax and semantics of CEL are documented at https://github.com/google/cel-specSupported filter:- status: the changelog status, support "==" operation. check Changelog.Status for available values.- type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ - status: the changelog status, support "==" operation. check Changelog.Status for available values.+ - type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ - create_time: the changelog create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.Example:status == "DONE"type in ["BASELINE", "MIGRATE"]status == "FAILED" && type == "SDL"+ create_time >= "2024-01-01T00:00:00Z" && create_time <= "2024-01-02T00:00:00Z"schema:type: stringresponses:diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.mdindex 678556a385..fd03d215a6 100644--- a/proto/gen/grpc-doc/v1/README.md+++ b/proto/gen/grpc-doc/v1/README.md@@ -3805,9 +3805,9 @@ When paginating, all other parameters provided must match the call that provided| view | [ChangelogView](#bytebase-v1-ChangelogView) | | || filter | [string](#string) | | Filter is used to filter changelogs returned in the list. The syntax and semantics of CEL are documented at https://github.com/google/cel-spec-Supported filter: status: the changelog status, support "==" operation. check Changelog.Status for available values. type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+Supported filter: - status: the changelog status, support "==" operation. check Changelog.Status for available values. - type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values. - create_time: the changelog create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.-Example: status == "DONE" type in ["BASELINE", "MIGRATE"] status == "FAILED" && type == "SDL" |+Example: status == "DONE" type in ["BASELINE", "MIGRATE"] status == "FAILED" && type == "SDL" create_time >= "2024-01-01T00:00:00Z" && create_time <= "2024-01-02T00:00:00Z" |diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.htmlindex 01b27968bc..f5e856baab 100644--- a/proto/gen/grpc-doc/v1/index.html+++ b/proto/gen/grpc-doc/v1/index.html@@ -10921,13 +10921,15 @@ the call that provided the page token. </p></td>The syntax and semantics of CEL are documented at https://github.com/google/cel-specSupported filter:-status: the changelog status, support "==" operation. check Changelog.Status for available values.-type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+- status: the changelog status, support "==" operation. check Changelog.Status for available values.+- type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+- create_time: the changelog create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.Example:status == "DONE"type in ["BASELINE", "MIGRATE"]-status == "FAILED" && type == "SDL" </p></td>+status == "FAILED" && type == "SDL"+create_time >= "2024-01-01T00:00:00Z" && create_time <= "2024-01-02T00:00:00Z" </p></td></tr></tbody>diff --git a/proto/v1/v1/database_service.proto b/proto/v1/v1/database_service.protoindex 3928f57986..ae417bd684 100644--- a/proto/v1/v1/database_service.proto+++ b/proto/v1/v1/database_service.proto@@ -1383,13 +1383,15 @@ message ListChangelogsRequest {// The syntax and semantics of CEL are documented at https://github.com/google/cel-spec//// Supported filter:- // status: the changelog status, support "==" operation. check Changelog.Status for available values.- // type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ // - status: the changelog status, support "==" operation. check Changelog.Status for available values.+ // - type: the changelog type, support "in" and "==" operation. check Changelog.Type for available values.+ // - create_time: the changelog create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.//// Example:// status == "DONE"// type in ["BASELINE", "MIGRATE"]// status == "FAILED" && type == "SDL"+ // create_time >= "2024-01-01T00:00:00Z" && create_time <= "2024-01-02T00:00:00Z"string filter = 5;}