Skip to content

chore: allow search by build params in workspace search filter #12694

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Mar 22, 2024
Prev Previous commit
Next Next commit
exact matching is working
  • Loading branch information
Emyrk committed Mar 21, 2024
commit 5c01cf5969d30edd42d95be62650a552eccd9f9c
2 changes: 2 additions & 0 deletions coderd/database/modelqueries.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ func (q *sqlQuerier) GetAuthorizedWorkspaces(ctx context.Context, arg GetWorkspa
// The name comment is for metric tracking
query := fmt.Sprintf("-- name: GetAuthorizedWorkspaces :many\n%s", filtered)
rows, err := q.db.QueryContext(ctx, query,
pq.Array(arg.ParamNames),
pq.Array(arg.ParamValues),
arg.Deleted,
arg.Status,
arg.OwnerID,
Expand Down
3 changes: 3 additions & 0 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 73 additions & 41 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 29 additions & 1 deletion coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,16 @@ WHERE
);

-- name: GetWorkspaces :many
WITH filtered_workspaces AS (
WITH
-- build_params is used to filter by build parameters if present.
-- It has to be a CTE because the set returning function 'unnest' cannot
-- be used in a WHERE clause.
build_params AS (
SELECT
LOWER(unnest(@param_names :: text[])) AS name,
LOWER(unnest(@param_values :: text[])) AS value
),
filtered_workspaces AS (
SELECT
workspaces.*,
COALESCE(template.name, 'unknown') as template_name,
Expand Down Expand Up @@ -200,6 +209,25 @@ WHERE
)
ELSE true
END
-- @param_value will match param name an value.
-- requires 2 arrays, @param_names and @param_values to be passed in.
-- Array index must match between the 2 arrays for name=value
AND CASE WHEN array_length(@param_names :: text[], 1) > 0 THEN
EXISTS (
SELECT
1
FROM
workspace_build_parameters
INNER JOIN
build_params
ON
LOWER(workspace_build_parameters.name) = build_params.name AND
LOWER(workspace_build_parameters.value) = build_params.value AND
workspace_build_parameters.workspace_build_id = latest_build.id
)
ELSE true
END

-- Filter by owner_name
AND CASE
WHEN @owner_username :: text != '' THEN
Expand Down
2 changes: 2 additions & 0 deletions coderd/searchquery/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ func Workspaces(query string, page codersdk.Pagination, agentInactiveDisconnectT
filter.HasParam = append(filter.HasParam, p.name)
continue
}
filter.ParamNames = append(filter.ParamNames, p.name)
filter.ParamValues = append(filter.ParamValues, *p.value)
}

parser.ErrorExcessParams(values)
Expand Down