Skip to content

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

chore: add title.matches to search plans (#16823)

boojack5cf919221d91
diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.go
index 68948b1963..73237a9dfe 100644
--- a/backend/api/v1/plan_service.go
+++ b/backend/api/v1/plan_service.go
@@ -2,6 +2,7 @@ package v1
import (
"context"
+ "fmt"
"log/slog"
"slices"
"strings"
@@ -11,6 +12,7 @@ import (
"github.com/google/cel-go/cel"
celast "github.com/google/cel-go/common/ast"
celoperators "github.com/google/cel-go/common/operators"
+ celoverloads "github.com/google/cel-go/common/overloads"
"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
"google.golang.org/protobuf/testing/protocmp"
@@ -1041,14 +1043,26 @@ func (s *PlanService) buildPlanFindWithFilter(ctx context.Context, planFind *sto
return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("failed to parse filter %v, error: %v", filter, iss.String()))
}
- var parseFilter func(expr celast.Expr) (string, error)
- parseFilter = func(expr celast.Expr) (string, error) {
+ var getFilter func(expr celast.Expr) (string, error)
+ var positionalArgs []any
+
+ parseToSQL := func(variable string, value any) (string, error) {
+ switch variable {
+ case "title":
+ positionalArgs = append(positionalArgs, value)
+ return fmt.Sprintf("plan.name = $%d", len(positionalArgs)), nil
+ default:
+ return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unsupported variable %q", variable))
+ }
+ }
+
+ getFilter = func(expr celast.Expr) (string, error) {
switch expr.Kind() {
case celast.CallKind:
functionName := expr.AsCall().FunctionName()
switch functionName {
case celoperators.LogicalAnd:
- return getSubConditionFromExpr(expr, parseFilter, "AND")
+ return getSubConditionFromExpr(expr, getFilter, "AND")
case celoperators.Equals:
variable, value := getVariableAndValueFromExpr(expr)
switch variable {
@@ -1057,25 +1071,28 @@ func (s *PlanService) buildPlanFindWithFilter(ctx context.Context, planFind *sto
if err != nil {
return "", connect.NewError(connect.CodeInternal, errors.Errorf("failed to get user %v with error %v", value, err.Error()))
}
- planFind.CreatorID = &user.ID
+ positionalArgs = append(positionalArgs, user.ID)
+ return fmt.Sprintf("plan.creator_id = $%d", len(positionalArgs)), nil
case "has_pipeline":
hasPipeline, ok := value.(bool)
if !ok {
return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf(`"has_pipeline" should be bool`))
}
if !hasPipeline {
- planFind.NoPipeline = true
+ return "plan.pipeline_id IS NULL", nil
}
+ return "plan.pipeline_id IS NOT NULL", nil
case "has_issue":
hasIssue, ok := value.(bool)
if !ok {
return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf(`"has_issue" should be bool`))
}
if !hasIssue {
- planFind.NoIssue = true
+ return "issue.id IS NULL", nil
}
+ return "issue.id IS NOT NULL", nil
default:
- return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unsupport variable %q with %v operator", variable, celoperators.Equals))
+ return parseToSQL(variable, value)
}
case celoperators.GreaterEquals, celoperators.LessEquals:
variable, rawValue := getVariableAndValueFromExpr(expr)
@@ -1090,20 +1107,48 @@ func (s *PlanService) buildPlanFindWithFilter(ctx context.Context, planFind *sto
if err != nil {
return "", errors.Errorf("failed to parse time %v, error: %v", value, err)
}
+ positionalArgs = append(positionalArgs, t)
if functionName == celoperators.GreaterEquals {
- planFind.CreatedAtAfter = &t
- } else {
- planFind.CreatedAtBefore = &t
+ return fmt.Sprintf("plan.created_at >= $%d", len(positionalArgs)), nil
+ }
+ return fmt.Sprintf("plan.created_at <= $%d", len(positionalArgs)), nil
+ case celoverloads.Matches:
+ variable := expr.AsCall().Target().AsIdent()
+ args := expr.AsCall().Args()
+ if len(args) != 1 {
+ return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf(`invalid args for %q`, variable))
+ }
+ value := args[0].AsLiteral().Value()
+ strValue, ok := value.(string)
+ if !ok {
+ return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf("expect string, got %T, hint: filter literals should be string", value))
}
+ strValue = strings.ToLower(strValue)
+
+ switch variable {
+ case "title":
+ return "LOWER(plan.name) LIKE '%" + strValue + "%'", nil
+ default:
+ return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf(`only "title" supports %q operator, but found %q`, celoverloads.Matches, variable))
+ }
+ default:
+ return "", connect.NewError(connect.CodeInvalidArgument, errors.Errorf("unsupported function %v", functionName))
}
default:
return "", errors.Errorf("unexpected expr kind %v", expr.Kind())
}
- return "", nil
}
- if _, err := parseFilter(ast.NativeRep().Expr()); err != nil {
- return connect.NewError(connect.CodeInvalidArgument, errors.Errorf("failed to parse filter, error: %v", err))
+ where, err := getFilter(ast.NativeRep().Expr())
+ if err != nil {
+ return err
+ }
+
+ if where != "" {
+ planFind.Filter = &store.ListResourceFilter{
+ Where: "(" + where + ")",
+ Args: positionalArgs,
+ }
}
return nil
diff --git a/backend/store/plan.go b/backend/store/plan.go
index b4cbb951f6..b14f784772 100644
--- a/backend/store/plan.go
+++ b/backend/store/plan.go
@@ -33,19 +33,15 @@ type PlanMessage struct {
// FindPlanMessage is the message to find a plan.
type FindPlanMessage struct {
- UID *int64
- ProjectID *string
- ProjectIDs *[]string
- CreatorID *int
- PipelineID *int
- CreatedAtBefore *time.Time
- CreatedAtAfter *time.Time
+ UID *int64
+ ProjectID *string
+ ProjectIDs *[]string
+ PipelineID *int
Limit *int
Offset *int
- NoIssue bool
- NoPipeline bool
+ Filter *ListResourceFilter
}
// UpdatePlanMessage is the message to update a plan.
@@ -127,6 +123,10 @@ func (s *Store) GetPlan(ctx context.Context, find *FindPlanMessage) (*PlanMessag
// ListPlans retrieves a list of plans.
func (s *Store) ListPlans(ctx context.Context, find *FindPlanMessage) ([]*PlanMessage, error) {
where, args := []string{"TRUE"}, []any{}
+ if filter := find.Filter; filter != nil {
+ where = append(where, filter.Where)
+ args = append(args, filter.Args...)
+ }
if v := find.UID; v != nil {
where, args = append(where, fmt.Sprintf("plan.id = $%d", len(args)+1)), append(args, *v)
}
@@ -134,26 +134,15 @@ func (s *Store) ListPlans(ctx context.Context, find *FindPlanMessage) ([]*PlanMe
where, args = append(where, fmt.Sprintf("plan.project = $%d", len(args)+1)), append(args, *v)
}
if v := find.ProjectIDs; v != nil {
- where, args = append(where, fmt.Sprintf("plan.project = ANY($%d)", len(args)+1)), append(args, *v)
+ if len(*v) == 0 {
+ where = append(where, "FALSE")
+ } else {
+ where, args = append(where, fmt.Sprintf("plan.project = ANY($%d)", len(args)+1)), append(args, *v)
+ }
}
if v := find.PipelineID; v != nil {
where, args = append(where, fmt.Sprintf("plan.pipeline_id = $%d", len(args)+1)), append(args, *v)
}
- if v := find.CreatorID; v != nil {
- where, args = append(where, fmt.Sprintf("plan.creator_id = $%d", len(args)+1)), append(args, *v)
- }
- if v := find.CreatedAtBefore; v != nil {
- where, args = append(where, fmt.Sprintf("plan.created_at < $%d", len(args)+1)), append(args, *v)
- }
- if v := find.CreatedAtAfter; v != nil {
- where, args = append(where, fmt.Sprintf("plan.created_at > $%d", len(args)+1)), append(args, *v)
- }
- if v := find.NoIssue; v {
- where = append(where, "issue.id IS NULL")
- }
- if v := find.NoPipeline; v {
- where = append(where, "plan.pipeline_id IS NULL")
- }
query := fmt.Sprintf(`
SELECT
diff --git a/frontend/src/store/modules/v1/plan.ts b/frontend/src/store/modules/v1/plan.ts
index b8a8774dcf..ee676cd93e 100644
--- a/frontend/src/store/modules/v1/plan.ts
+++ b/frontend/src/store/modules/v1/plan.ts
@@ -17,6 +17,7 @@ import { useUserStore } from "../user";
export interface PlanFind {
project: string;
+ query?: string;
creator?: string;
createdTsAfter?: number;
createdTsBefore?: number;
@@ -26,6 +27,9 @@ export interface PlanFind {
export const buildPlanFilter = (find: PlanFind): string => {
const filter: string[] = [];
+ if (find.query) {
+ filter.push(`title.matches("${find.query.trim().toLowerCase()}")`);
+ }
if (find.creator) {
filter.push(`creator == "${find.creator}"`);
}
@@ -60,6 +64,7 @@ export const buildPlanFindBySearchParams = (
const filter: PlanFind = {
...defaultFind,
project: `projects/${projectScope?.value ?? "-"}`,
+ query: params.query,
createdTsAfter: createdTsRange?.[0],
createdTsBefore: createdTsRange?.[1],
creator: getValueFromSearchParams(params, "creator", "users/"),
diff --git a/frontend/src/types/proto-es/v1/plan_service_pb.d.ts b/frontend/src/types/proto-es/v1/plan_service_pb.d.ts
index fc87f486cc..95a054fac1 100644
--- a/frontend/src/types/proto-es/v1/plan_service_pb.d.ts
+++ b/frontend/src/types/proto-es/v1/plan_service_pb.d.ts
@@ -142,10 +142,13 @@ export declare type SearchPlansRequest = Message<"bytebase.v1.SearchPlansRequest
* - create_time: issue create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.
* - has_pipeline: the plan has pipeline or not, support "==" operator, the value should be "true" or "false".
* - has_issue: the plan has issue or not, support "==" operator, the value should be "true" or "false".
+ * - title: the plan title, support "==" operator for exact match and ".matches()" operator for case-insensitive substring match.
*
* For example:
* creator == "users/[email protected]" && create_time >= "2025-01-02T15:04:05Z07:00"
* has_pipeline == false && has_issue == true
+ * title == "My Plan"
+ * title.matches("database migration")
*
* @generated from field: string filter = 4;
*/
diff --git a/frontend/src/views/project/ProjectPlanDashboard.vue b/frontend/src/views/project/ProjectPlanDashboard.vue
index af5cbc7b20..a8ce3f166a 100644
--- a/frontend/src/views/project/ProjectPlanDashboard.vue
+++ b/frontend/src/views/project/ProjectPlanDashboard.vue
@@ -132,6 +132,7 @@ const planSearchParams = computed(() => {
},
];
return {
+ query: state.params.query.trim().toLowerCase(),
scopes: [...state.params.scopes, ...defaultScopes],
} as SearchParams;
});
diff --git a/proto/gen/grpc-doc/openapi.yaml b/proto/gen/grpc-doc/openapi.yaml
index 90280d7991..5d3ffa4de2 100644
--- a/proto/gen/grpc-doc/openapi.yaml
+++ b/proto/gen/grpc-doc/openapi.yaml
@@ -13067,10 +13067,13 @@ components:
- create_time: issue create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.
- has_pipeline: the plan has pipeline or not, support "==" operator, the value should be "true" or "false".
- has_issue: the plan has issue or not, support "==" operator, the value should be "true" or "false".
+ - title: the plan title, support "==" operator for exact match and ".matches()" operator for case-insensitive substring match.
For example:
creator == "users/[email protected]" && create_time >= "2025-01-02T15:04:05Z07:00"
has_pipeline == false && has_issue == true
+ title == "My Plan"
+ title.matches("database migration")
SearchPlansResponse:
type: object
properties:
diff --git a/proto/gen/grpc-doc/v1/README.md b/proto/gen/grpc-doc/v1/README.md
index e491b53e67..43c49d25fc 100644
--- a/proto/gen/grpc-doc/v1/README.md
+++ b/proto/gen/grpc-doc/v1/README.md
@@ -7490,9 +7490,9 @@ When paginating, all other parameters provided to `ListPlans` must match the cal
When paginating, all other parameters provided to `SearchPlans` must match the call that provided the page token. |
| filter | [string](#string) | | Filter is used to filter plans returned in the list. The syntax and semantics of CEL are documented at https://github.com/google/cel-spec
-Supported filters: - creator: the plan creator full name in &#34;users/{email or id}&#34; format, support &#34;==&#34; operator. - create_time: issue create time in &#34;2006-01-02T15:04:05Z07:00&#34; format, support &#34;&gt;=&#34; or &#34;&lt;=&#34; operator. - has_pipeline: the plan has pipeline or not, support &#34;==&#34; operator, the value should be &#34;true&#34; or &#34;false&#34;. - has_issue: the plan has issue or not, support &#34;==&#34; operator, the value should be &#34;true&#34; or &#34;false&#34;.
+Supported filters: - creator: the plan creator full name in &#34;users/{email or id}&#34; format, support &#34;==&#34; operator. - create_time: issue create time in &#34;2006-01-02T15:04:05Z07:00&#34; format, support &#34;&gt;=&#34; or &#34;&lt;=&#34; operator. - has_pipeline: the plan has pipeline or not, support &#34;==&#34; operator, the value should be &#34;true&#34; or &#34;false&#34;. - has_issue: the plan has issue or not, support &#34;==&#34; operator, the value should be &#34;true&#34; or &#34;false&#34;. - title: the plan title, support &#34;==&#34; operator for exact match and &#34;.matches()&#34; operator for case-insensitive substring match.
-For example: creator == &#34;users/[email protected]&#34; &amp;&amp; create_time &gt;= &#34;2025-01-02T15:04:05Z07:00&#34; has_pipeline == false &amp;&amp; has_issue == true |
+For example: creator == &#34;users/[email protected]&#34; &amp;&amp; create_time &gt;= &#34;2025-01-02T15:04:05Z07:00&#34; has_pipeline == false &amp;&amp; has_issue == true title == &#34;My Plan&#34; title.matches(&#34;database migration&#34;) |
diff --git a/proto/gen/grpc-doc/v1/index.html b/proto/gen/grpc-doc/v1/index.html
index 8598cde28a..3ce1c1c7df 100644
--- a/proto/gen/grpc-doc/v1/index.html
+++ b/proto/gen/grpc-doc/v1/index.html
@@ -20937,10 +20937,13 @@ Supported filters:
- create_time: issue create time in &#34;2006-01-02T15:04:05Z07:00&#34; format, support &#34;&gt;=&#34; or &#34;&lt;=&#34; operator.
- has_pipeline: the plan has pipeline or not, support &#34;==&#34; operator, the value should be &#34;true&#34; or &#34;false&#34;.
- has_issue: the plan has issue or not, support &#34;==&#34; operator, the value should be &#34;true&#34; or &#34;false&#34;.
+- title: the plan title, support &#34;==&#34; operator for exact match and &#34;.matches()&#34; operator for case-insensitive substring match.
For example:
creator == &#34;users/[email protected]&#34; &amp;&amp; create_time &gt;= &#34;2025-01-02T15:04:05Z07:00&#34;
-has_pipeline == false &amp;&amp; has_issue == true </p></td>
+has_pipeline == false &amp;&amp; has_issue == true
+title == &#34;My Plan&#34;
+title.matches(&#34;database migration&#34;) </p></td>
</tr>
</tbody>
diff --git a/proto/generated-go/v1/plan_service.pb.go b/proto/generated-go/v1/plan_service.pb.go
index 9955cec8c3..c3a51840b7 100644
--- a/proto/generated-go/v1/plan_service.pb.go
+++ b/proto/generated-go/v1/plan_service.pb.go
@@ -447,10 +447,13 @@ type SearchPlansRequest struct {
// - create_time: issue create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.
// - has_pipeline: the plan has pipeline or not, support "==" operator, the value should be "true" or "false".
// - has_issue: the plan has issue or not, support "==" operator, the value should be "true" or "false".
+ // - title: the plan title, support "==" operator for exact match and ".matches()" operator for case-insensitive substring match.
//
// For example:
// creator == "users/[email protected]" && create_time >= "2025-01-02T15:04:05Z07:00"
// has_pipeline == false && has_issue == true
+ // title == "My Plan"
+ // title.matches("database migration")
Filter string `protobuf:"bytes,4,opt,name=filter,proto3" json:"filter,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
diff --git a/proto/v1/v1/plan_service.proto b/proto/v1/v1/plan_service.proto
index e44eb9bf9d..8a179b555f 100644
--- a/proto/v1/v1/plan_service.proto
+++ b/proto/v1/v1/plan_service.proto
@@ -168,10 +168,13 @@ message SearchPlansRequest {
// - create_time: issue create time in "2006-01-02T15:04:05Z07:00" format, support ">=" or "<=" operator.
// - has_pipeline: the plan has pipeline or not, support "==" operator, the value should be "true" or "false".
// - has_issue: the plan has issue or not, support "==" operator, the value should be "true" or "false".
+ // - title: the plan title, support "==" operator for exact match and ".matches()" operator for case-insensitive substring match.
//
// For example:
// creator == "users/[email protected]" && create_time >= "2025-01-02T15:04:05Z07:00"
// has_pipeline == false && has_issue == true
+ // title == "My Plan"
+ // title.matches("database migration")
string filter = 4;
}