Skip to content

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

chore: update filter for change history (#11746)

ecmadaoa803d74393cf
diff --git a/backend/api/v1/database_service.go b/backend/api/v1/database_service.go
index ad21254613..6537ee7ef5 100644
--- a/backend/api/v1/database_service.go
+++ b/backend/api/v1/database_service.go
@@ -204,11 +204,11 @@ func getDatabaseFind(filter string) (*store.FindDatabaseMessage, error) {
find := &store.FindDatabaseMessage{}
for _, spec := range filters {
+ if spec.operator != comparatorTypeEqual {
+ return nil, status.Errorf(codes.InvalidArgument, `only support "=" operation for filter`)
+ }
switch spec.key {
case "instance":
- if spec.operator != comparatorTypeEqual {
- return nil, status.Errorf(codes.InvalidArgument, `only support "=" operation for "instance" filter`)
- }
instanceID, err := common.GetInstanceID(spec.value)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
@@ -217,10 +217,6 @@ func getDatabaseFind(filter string) (*store.FindDatabaseMessage, error) {
find.InstanceID = &instanceID
}
case "project":
- if spec.operator != comparatorTypeEqual {
- return nil, status.Errorf(codes.InvalidArgument, `only support "=" operation for "project" filter`)
- }
-
projectID, err := common.GetProjectID(spec.value)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
@@ -228,6 +224,8 @@ func getDatabaseFind(filter string) (*store.FindDatabaseMessage, error) {
if projectID != "-" {
find.ProjectID = &projectID
}
+ default:
+ return nil, status.Errorf(codes.InvalidArgument, "invalid filter key %q", spec.key)
}
}
@@ -896,9 +894,31 @@ func (s *DatabaseService) ListChangeHistories(ctx context.Context, request *v1pb
if request.View == v1pb.ChangeHistoryView_CHANGE_HISTORY_VIEW_FULL {
find.ShowFull = true
}
- if request.Filter != "" {
- find.ResourcesFilter = &request.Filter
+
+ filters, err := parseFilter(request.Filter)
+ if err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, err.Error())
}
+ for _, expr := range filters {
+ if expr.operator != comparatorTypeEqual {
+ return nil, status.Errorf(codes.InvalidArgument, `only support "=" operation for filter`)
+ }
+ switch expr.key {
+ case "source":
+ changeSource := db.MigrationSource(expr.value)
+ find.Source = &changeSource
+ case "type":
+ for _, changeType := range strings.Split(expr.value, " | ") {
+ find.TypeList = append(find.TypeList, db.MigrationType(changeType))
+ }
+ case "table":
+ resourcesFilter := expr.value
+ find.ResourcesFilter = &resourcesFilter
+ default:
+ return nil, status.Errorf(codes.InvalidArgument, "invalid filter key %q", expr.key)
+ }
+ }
+
changeHistories, err := s.store.ListInstanceChangeHistory(ctx, find)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list change history, error: %v", err)
diff --git a/backend/store/instance_change_history.go b/backend/store/instance_change_history.go
index 065732af56..73e4c3208f 100644
--- a/backend/store/instance_change_history.go
+++ b/backend/store/instance_change_history.go
@@ -66,6 +66,7 @@ type FindInstanceChangeHistoryMessage struct {
SheetID *int
Source *db.MigrationSource
Version *model.Version
+ TypeList []db.MigrationType
ResourcesFilter *string
Limit *int
Offset *int
@@ -483,6 +484,14 @@ func (s *Store) ListInstanceChangeHistory(ctx context.Context, find *FindInstanc
}
}
+ changeTypes := []string{}
+ for _, changeType := range find.TypeList {
+ changeTypes = append(changeTypes, fmt.Sprintf(`'%s'`, string(changeType)))
+ }
+ if len(changeTypes) > 0 {
+ where = append(where, fmt.Sprintf("instance_change_history.type IN (%s)", strings.Join(changeTypes, ",")))
+ }
+
statementField := "COALESCE(sheet.statement, instance_change_history.statement)"
if !find.ShowFull {
statementField = fmt.Sprintf("LEFT(%s, %d)", statementField, find.TruncateSize)
diff --git a/backend/tests/change_history_test.go b/backend/tests/change_history_test.go
index b3067103f5..676e4f3381 100644
--- a/backend/tests/change_history_test.go
+++ b/backend/tests/change_history_test.go
@@ -29,24 +29,20 @@ var (
wantStatements []string
}{
{
- filter: fmt.Sprintf(`tableExists("%s", "public", "t2")`, databaseName),
+ filter: fmt.Sprintf(`table = "tableExists('%s', 'public', 't2')"`, databaseName),
wantStatements: []string{
statements[1],
statements[2],
},
},
{
- filter: fmt.Sprintf(`tableExists("%s", "public", "t2") && tableExists("%s", "public", "t3")`, databaseName, databaseName),
+ filter: fmt.Sprintf(`table = "tableExists('%s', 'public', 't2') && tableExists('%s', 'public', 't3')"`, databaseName, databaseName),
wantStatements: []string{
statements[1],
},
},
{
- filter: fmt.Sprintf(`
- (tableExists("%s", "public", "t2") && tableExists("%s", "public", "t3"))
- ||
- tableExists("%s", "public", "t1")
- `, databaseName, databaseName, databaseName),
+ filter: fmt.Sprintf(`table = "(tableExists('%s', 'public', 't2') && tableExists('%s', 'public', 't3')) || tableExists('%s', 'public', 't1')"`, databaseName, databaseName, databaseName),
wantStatements: []string{
statements[0],
statements[1],
diff --git a/frontend/src/types/proto/v1/database_service.ts b/frontend/src/types/proto/v1/database_service.ts
index 59101ba4b7..32fbf734ac 100644
--- a/frontend/src/types/proto/v1/database_service.ts
+++ b/frontend/src/types/proto/v1/database_service.ts
@@ -1361,7 +1361,16 @@ export interface ListChangeHistoriesRequest {
view: ChangeHistoryView;
/**
* The filter of the change histories.
- * Follow the CEL syntax.
+ * follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+ * Support filter by type, source or table.
+ * For example:
+ * table = "tableExists('{database}', '{schema}', '{table}')"
+ * table = "tableExists('db', 'public', 'table1') || tableExists('db', 'public', 'table2')"
+ * type = "MIGRATE | DATA"
+ * source = "UI"
+ * source = "VCS"
+ *
+ * The table filter follow the CEL syntax.
* currently, we have one function for CEL:
* - tableExists(database, schema, table): return true if the table exists in changed resources.
*
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index 9bb6e52b2c..13b56ab75f 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -1333,7 +1333,16 @@ paths:
in: query
description: |-
The filter of the change histories.
- Follow the CEL syntax.
+ follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+ Support filter by type, source or table.
+ For example:
+ table = "tableExists('{database}', '{schema}', '{table}')"
+ table = "tableExists('db', 'public', 'table1') || tableExists('db', 'public', 'table2')"
+ type = "MIGRATE | DATA"
+ source = "UI"
+ source = "VCS"
+
+ The table filter follow the CEL syntax.
currently, we have one function for CEL:
- tableExists(database, schema, table): return true if the table exists in changed resources.
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index 5602db66f1..f9dee3d989 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -2438,7 +2438,9 @@ IndexMetadata is the metadata for indexes.
When paginating, all other parameters provided to `ListChangeHistories` must match the call that provided the page token. |
| view | [ChangeHistoryView](#bytebase-v1-ChangeHistoryView) | | |
-| filter | [string](#string) | | The filter of the change histories. Follow the CEL syntax. currently, we have one function for CEL: - tableExists(database, schema, table): return true if the table exists in changed resources.
+| filter | [string](#string) | | The filter of the change histories. follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax. Support filter by type, source or table. For example: table = "tableExists('{database}', '{schema}', '{table}')" table = "tableExists('db', 'public', 'table1') || tableExists('db', 'public', 'table2')" type = "MIGRATE | DATA" source = "UI" source = "VCS"
+
+The table filter follow the CEL syntax. currently, we have one function for CEL: - tableExists(database, schema, table): return true if the table exists in changed resources.
examples: Use tableExists("db", "public", "table1") to filter the change histories which have the table "table1" in the schema "public" of the database "db". For MySQL, the schema is always "", such as tableExists("db", "", "table1").
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index db88eacbc3..4d934df4c9 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -7387,7 +7387,16 @@ the call that provided the page token. </p></td>
<td><a href="#string">string</a></td>
<td></td>
<td><p>The filter of the change histories.
-Follow the CEL syntax.
+follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+Support filter by type, source or table.
+For example:
+table = &#34;tableExists(&#39;{database}&#39;, &#39;{schema}&#39;, &#39;{table}&#39;)&#34;
+table = &#34;tableExists(&#39;db&#39;, &#39;public&#39;, &#39;table1&#39;) || tableExists(&#39;db&#39;, &#39;public&#39;, &#39;table2&#39;)&#34;
+type = &#34;MIGRATE | DATA&#34;
+source = &#34;UI&#34;
+source = &#34;VCS&#34;
+
+The table filter follow the CEL syntax.
currently, we have one function for CEL:
- tableExists(database, schema, table): return true if the table exists in changed resources.
diff --git a/proto/generated-go/v1/database_service.pb.go b/proto/generated-go/v1/database_service.pb.go
index f886ad0f59..2ae85d6cbf 100644
--- a/proto/generated-go/v1/database_service.pb.go
+++ b/proto/generated-go/v1/database_service.pb.go
@@ -4908,7 +4908,16 @@ type ListChangeHistoriesRequest struct {
PageToken string `protobuf:"bytes,3,opt,name=page_token,json=pageToken,proto3" json:"page_token,omitempty"`
View ChangeHistoryView `protobuf:"varint,4,opt,name=view,proto3,enum=bytebase.v1.ChangeHistoryView" json:"view,omitempty"`
// The filter of the change histories.
- // Follow the CEL syntax.
+ // follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+ // Support filter by type, source or table.
+ // For example:
+ // table = "tableExists('{database}', '{schema}', '{table}')"
+ // table = "tableExists('db', 'public', 'table1') || tableExists('db', 'public', 'table2')"
+ // type = "MIGRATE | DATA"
+ // source = "UI"
+ // source = "VCS"
+ //
+ // The table filter follow the CEL syntax.
// currently, we have one function for CEL:
// - tableExists(database, schema, table): return true if the table exists in changed resources.
//
diff --git a/proto/v1/database_service.proto b/proto/v1/database_service.proto
index 8cb50890c0..4d7217ae4a 100644
--- a/proto/v1/database_service.proto
+++ b/proto/v1/database_service.proto
@@ -1117,7 +1117,16 @@ message ListChangeHistoriesRequest {
ChangeHistoryView view = 4;
// The filter of the change histories.
- // Follow the CEL syntax.
+ // follow the [ebnf](https://en.wikipedia.org/wiki/Extended_Backus%E2%80%93Naur_form) syntax.
+ // Support filter by type, source or table.
+ // For example:
+ // table = "tableExists('{database}', '{schema}', '{table}')"
+ // table = "tableExists('db', 'public', 'table1') || tableExists('db', 'public', 'table2')"
+ // type = "MIGRATE | DATA"
+ // source = "UI"
+ // source = "VCS"
+ //
+ // The table filter follow the CEL syntax.
// currently, we have one function for CEL:
// - tableExists(database, schema, table): return true if the table exists in changed resources.
//