Skip to content

feat: workspace filter query supported in backend #2232

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 38 commits into from
Jun 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
1a14a8c
feat: add support for template in workspace filter
f0ssel Jun 7, 2022
e3bd559
get working
f0ssel Jun 8, 2022
91d7d84
make gen
f0ssel Jun 8, 2022
2941b58
feat: Implement workspace search filter to support names
Emyrk Jun 10, 2022
70f4304
Use new query param parser for pagination fields
Emyrk Jun 10, 2022
0f7bb21
Fix fake db implementation
Emyrk Jun 10, 2022
5687dbe
Add unit test for parsing query params
Emyrk Jun 10, 2022
18e5247
Fix linting
Emyrk Jun 10, 2022
9a1b182
Fix search
Emyrk Jun 10, 2022
fab5d8c
Maintain old behavior
Emyrk Jun 10, 2022
15754c5
Linting
Emyrk Jun 10, 2022
f3123fe
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 10, 2022
d1c6319
Remove excessive calls, use filters on a single query
Emyrk Jun 10, 2022
e5ec365
Remove unused code
Emyrk Jun 10, 2022
21683d2
Add unit test to keep fake db clean
Emyrk Jun 10, 2022
fc48397
Fix typo
Emyrk Jun 10, 2022
5310164
Drop like name search on template
Emyrk Jun 10, 2022
c64ed18
Fix linting
Emyrk Jun 10, 2022
c6e3a57
Move WorkspaceSearchQuery to workspaces.go
Emyrk Jun 10, 2022
7821aa4
Search all templates with name
Emyrk Jun 10, 2022
d3091f0
Add more complex filter unit test
Emyrk Jun 10, 2022
ebcb831
Fix unit test to not violate pg constraint
Emyrk Jun 10, 2022
9368c48
Drop owner_id from query params
Emyrk Jun 10, 2022
b5f5705
Remove unused code/params
Emyrk Jun 10, 2022
9c9a5e2
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 10, 2022
4512a9b
Remove field from ts
Emyrk Jun 10, 2022
e9e913d
Address PR comments
Emyrk Jun 10, 2022
2aac32b
Fix js test
Emyrk Jun 10, 2022
c8813cc
Fix unit test
Emyrk Jun 10, 2022
ee35935
PR feedback
Emyrk Jun 13, 2022
4ad585e
Rename vague function name
Emyrk Jun 13, 2022
6251493
WorkspaceSearchQuery now returns db filter
Emyrk Jun 13, 2022
ac8dddd
Correct unit test and typescript
Emyrk Jun 13, 2022
727239e
Merge remote-tracking branch 'origin/main' into stevenmasley/workspac…
Emyrk Jun 13, 2022
92dcbc8
fmt
Emyrk Jun 13, 2022
0a51e02
Use !== over !=
Emyrk Jun 13, 2022
5a1272c
Fix js unit test
Emyrk Jun 13, 2022
5b5039b
Js linting
Emyrk Jun 13, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
148 changes: 67 additions & 81 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,41 +321,47 @@ func (q *fakeQuerier) GetWorkspacesWithFilter(_ context.Context, arg database.Ge

workspaces := make([]database.Workspace, 0)
for _, workspace := range q.workspaces {
if arg.OrganizationID != uuid.Nil && workspace.OrganizationID != arg.OrganizationID {
continue
}
if arg.OwnerID != uuid.Nil && workspace.OwnerID != arg.OwnerID {
continue
}
if arg.OwnerUsername != "" {
owner, err := q.GetUserByID(context.Background(), workspace.OwnerID)
if err == nil && arg.OwnerUsername != owner.Username {
continue
}
}
if arg.TemplateName != "" {
templates, err := q.GetTemplatesWithFilter(context.Background(), database.GetTemplatesWithFilterParams{
ExactName: arg.TemplateName,
})
// Add to later param
if err == nil {
for _, t := range templates {
arg.TemplateIds = append(arg.TemplateIds, t.ID)
}
}
}
if !arg.Deleted && workspace.Deleted {
continue
}
if arg.Name != "" && !strings.Contains(workspace.Name, arg.Name) {
continue
}
workspaces = append(workspaces, workspace)
}

return workspaces, nil
}

func (q *fakeQuerier) GetWorkspacesByTemplateID(_ context.Context, arg database.GetWorkspacesByTemplateIDParams) ([]database.Workspace, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

workspaces := make([]database.Workspace, 0)
for _, workspace := range q.workspaces {
if workspace.TemplateID.String() != arg.TemplateID.String() {
continue
}
if workspace.Deleted != arg.Deleted {
continue
if len(arg.TemplateIds) > 0 {
match := false
for _, id := range arg.TemplateIds {
if workspace.TemplateID == id {
match = true
break
}
}
if !match {
continue
}
}
workspaces = append(workspaces, workspace)
}
if len(workspaces) == 0 {
return nil, sql.ErrNoRows
}

return workspaces, nil
}

Expand Down Expand Up @@ -641,25 +647,6 @@ func (q *fakeQuerier) GetWorkspaceBuildByWorkspaceIDAndBuildNumber(_ context.Con
return database.WorkspaceBuild{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetWorkspacesByOrganizationIDs(_ context.Context, req database.GetWorkspacesByOrganizationIDsParams) ([]database.Workspace, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

workspaces := make([]database.Workspace, 0)
for _, workspace := range q.workspaces {
for _, id := range req.Ids {
if workspace.OrganizationID != id {
continue
}
if workspace.Deleted != req.Deleted {
continue
}
workspaces = append(workspaces, workspace)
}
}
return workspaces, nil
}

func (q *fakeQuerier) GetOrganizations(_ context.Context) ([]database.Organization, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down Expand Up @@ -786,6 +773,44 @@ func (q *fakeQuerier) UpdateTemplateMetaByID(_ context.Context, arg database.Upd
return sql.ErrNoRows
}

func (q *fakeQuerier) GetTemplatesWithFilter(_ context.Context, arg database.GetTemplatesWithFilterParams) ([]database.Template, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

var templates []database.Template
for _, template := range q.templates {
if template.Deleted != arg.Deleted {
continue
}
if arg.OrganizationID != uuid.Nil && template.OrganizationID != arg.OrganizationID {
continue
}

if arg.ExactName != "" && !strings.EqualFold(template.Name, arg.ExactName) {
continue
}

if len(arg.Ids) > 0 {
match := false
for _, id := range arg.Ids {
if template.ID == id {
match = true
break
}
}
if !match {
continue
}
}
templates = append(templates, template)
}
if len(templates) > 0 {
return templates, nil
}

return nil, sql.ErrNoRows
}

func (q *fakeQuerier) GetTemplateVersionsByTemplateID(_ context.Context, arg database.GetTemplateVersionsByTemplateIDParams) (version []database.TemplateVersion, err error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down Expand Up @@ -923,45 +948,6 @@ func (q *fakeQuerier) GetParameterValueByScopeAndName(_ context.Context, arg dat
return database.ParameterValue{}, sql.ErrNoRows
}

func (q *fakeQuerier) GetTemplatesByOrganization(_ context.Context, arg database.GetTemplatesByOrganizationParams) ([]database.Template, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

templates := make([]database.Template, 0)
for _, template := range q.templates {
if template.Deleted != arg.Deleted {
continue
}
if template.OrganizationID != arg.OrganizationID {
continue
}
templates = append(templates, template)
}
if len(templates) == 0 {
return nil, sql.ErrNoRows
}
return templates, nil
}

func (q *fakeQuerier) GetTemplatesByIDs(_ context.Context, ids []uuid.UUID) ([]database.Template, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()

templates := make([]database.Template, 0)
for _, template := range q.templates {
for _, id := range ids {
if template.ID.String() != id.String() {
continue
}
templates = append(templates, template)
}
}
if len(templates) == 0 {
return nil, sql.ErrNoRows
}
return templates, nil
}

func (q *fakeQuerier) GetOrganizationMemberByUserID(_ context.Context, arg database.GetOrganizationMemberByUserIDParams) (database.OrganizationMember, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
61 changes: 61 additions & 0 deletions coderd/database/databasefake/databasefake_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package databasefake_test

import (
"fmt"
"reflect"
"testing"

"github.com/coder/coder/coderd/database"

"github.com/coder/coder/coderd/database/databasefake"
)

// TestExactMethods will ensure the fake database does not hold onto excessive
// functions. The fake database is a manual implementation, so it is possible
// we forget to delete functions that we remove. This unit test just ensures
// we remove the extra methods.
func TestExactMethods(t *testing.T) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This helps find which methods are not required on the fake. Just good for keeping that struct clean.

t.Parallel()

// extraFakeMethods contains the extra allowed methods that are not a part
// of the database.Store interface.
extraFakeMethods := map[string]string{
// Example
// "SortFakeLists": "Helper function used",
}

fake := reflect.TypeOf(databasefake.New())
fakeMethods := methods(fake)

store := reflect.TypeOf((*database.Store)(nil)).Elem()
storeMethods := methods(store)

// Store should be a subset
for k := range storeMethods {
_, ok := fakeMethods[k]
if !ok {
panic(fmt.Sprintf("This should never happen. FakeDB missing method %s, so doesn't fit the interface", k))
}
delete(storeMethods, k)
delete(fakeMethods, k)
}

for k := range fakeMethods {
_, ok := extraFakeMethods[k]
if ok {
continue
}
// If you are seeing this error, you have an extra function not required
// for the database.Store. If you still want to keep it, add it to
// 'extraFakeMethods' to allow it.
t.Errorf("Fake method '%s()' is excessive and not needed to fit interface, delete it", k)
}
}

func methods(rt reflect.Type) map[string]bool {
methods := make(map[string]bool)
for i := 0; i < rt.NumMethod(); i++ {
methods[rt.Method(i).Name] = true
}
return methods
}
5 changes: 1 addition & 4 deletions coderd/database/querier.go

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

Loading