From 42e4e054c26d7bc69b161fc6c8b1871a7126f1d3 Mon Sep 17 00:00:00 2001 From: Mirko Teodorovic Date: Thu, 1 Oct 2020 17:03:35 +0200 Subject: [PATCH] NOISSUE - Fix group retrieval when parent id is not specified (#1247) * fix group retrieval Signed-off-by: Mirko Teodorovic * refactor for better readability Signed-off-by: Mirko Teodorovic --- users/postgres/groups.go | 48 ++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 22 deletions(-) diff --git a/users/postgres/groups.go b/users/postgres/groups.go index 1d294897..5798d3cc 100644 --- a/users/postgres/groups.go +++ b/users/postgres/groups.go @@ -153,16 +153,28 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID if err != nil { return users.GroupPage{}, errors.Wrap(errRetrieveDB, err) } + if mq != "" { + mq = fmt.Sprintf("WHERE %s", mq) + } - q := fmt.Sprintf(`WITH RECURSIVE subordinates AS ( - SELECT id, owner_id, parent_id, name, description, metadata - FROM groups - WHERE id = :id - UNION - SELECT groups.id, groups.owner_id, groups.parent_id, groups.name, groups.description, groups.metadata - FROM groups - INNER JOIN subordinates s ON s.id = groups.parent_id %s - ) SELECT * FROM subordinates ORDER BY id LIMIT :limit OFFSET :offset`, mq) + cq := fmt.Sprintf("SELECT COUNT(*) FROM groups %s", mq) + sq := fmt.Sprintf("SELECT id, owner_id, parent_id, name, description, metadata FROM groups %s", mq) + q := fmt.Sprintf("%s ORDER BY id LIMIT :limit OFFSET :offset", sq) + + if groupID != "" { + sq = fmt.Sprintf( + `WITH RECURSIVE subordinates AS ( + SELECT id, owner_id, parent_id, name, description, metadata + FROM groups + WHERE id = :id + UNION + SELECT groups.id, groups.owner_id, groups.parent_id, groups.name, groups.description, groups.metadata + FROM groups + INNER JOIN subordinates s ON s.id = groups.parent_id %s + )`, mq) + q = fmt.Sprintf("%s SELECT * FROM subordinates ORDER BY id LIMIT :limit OFFSET :offset", sq) + cq = fmt.Sprintf("%s SELECT COUNT(*) FROM subordinates", sq) + } dbPage, err := toDBGroupPage("", groupID, offset, limit, gm) if err != nil { @@ -173,7 +185,6 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID if err != nil { return users.GroupPage{}, errors.Wrap(errSelectDb, err) } - defer rows.Close() var items []users.Group @@ -189,16 +200,6 @@ func (gr groupRepository) RetrieveAllWithAncestors(ctx context.Context, groupID items = append(items, gr) } - cq := fmt.Sprintf(`WITH RECURSIVE subordinates AS ( - SELECT id, owner_id, parent_id, name, description, metadata - FROM groups - WHERE id = :id - UNION - SELECT groups.id, groups.owner_id, groups.parent_id, groups.name, groups.description, groups.metadata - FROM groups - INNER JOIN subordinates s ON s.id = groups.parent_id %s - ) SELECT COUNT(*) FROM subordinates`, mq) - total, err := total(ctx, gr.db, cq, dbPage) if err != nil { return users.GroupPage{}, errors.Wrap(errSelectDb, err) @@ -222,9 +223,12 @@ func (gr groupRepository) Memberships(ctx context.Context, userID string, offset return users.GroupPage{}, errors.Wrap(errRetrieveDB, err) } + if mq != "" { + mq = fmt.Sprintf("AND %s", mq) + } q := fmt.Sprintf(`SELECT g.id, g.owner_id, g.parent_id, g.name, g.description, g.metadata FROM group_relations gr, groups g - WHERE gr.group_id = g.id and gr.user_id = :userID + WHERE gr.group_id = g.id and gr.user_id = :userID %s ORDER BY id LIMIT :limit OFFSET :offset;`, mq) params := map[string]interface{}{ @@ -424,7 +428,7 @@ func getGroupsMetadataQuery(m users.Metadata) ([]byte, string, error) { mq := "" mb := []byte("{}") if len(m) > 0 { - mq = ` AND groups.metadata @> :metadata` + mq = `groups.metadata @> :metadata` b, err := json.Marshal(m) if err != nil {