Skip to content

Commit

Permalink
GetProfileByProjectAndID includes selectors (#3833)
Browse files Browse the repository at this point in the history
We'll want to have the selectors in the reply to use in profile
handlers.

Relates: #3723
  • Loading branch information
jhrozek authored Jul 10, 2024
1 parent 9fbf426 commit 881c672
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
17 changes: 16 additions & 1 deletion database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,22 @@ DELETE FROM entity_profiles WHERE profile_id = $1 AND entity = $2;
SELECT * FROM entity_profiles WHERE profile_id = $1 AND entity = $2;

-- name: GetProfileByProjectAndID :many
SELECT sqlc.embed(profiles), sqlc.embed(profiles_with_entity_profiles) FROM profiles JOIN profiles_with_entity_profiles ON profiles.id = profiles_with_entity_profiles.profid
WITH helper AS(
SELECT pr.id as profid,
ARRAY_AGG(ROW(ps.id, ps.profile_id, ps.entity, ps.selector, ps.comment)::profile_selector) AS selectors
FROM profiles pr
JOIN profile_selectors ps
ON pr.id = ps.profile_id
WHERE pr.project_id = $1
GROUP BY pr.id
)
SELECT
sqlc.embed(profiles),
sqlc.embed(profiles_with_entity_profiles),
helper.selectors::profile_selector[] AS profiles_with_selectors
FROM profiles
JOIN profiles_with_entity_profiles ON profiles.id = profiles_with_entity_profiles.profid
LEFT JOIN helper ON profiles.id = helper.profid
WHERE profiles.project_id = $1 AND profiles.id = $2;

-- name: GetProfileByID :one
Expand Down
19 changes: 18 additions & 1 deletion internal/db/profiles.sql.go

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

29 changes: 29 additions & 0 deletions internal/db/profiles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,35 @@ func TestProfileListWithSelectors(t *testing.T) {
require.Contains(t, rows[genSelIdx].ProfilesWithSelectors, genericSel)
})

t.Run("Get profile by project and ID", func(t *testing.T) {
t.Parallel()

oneResult, err := testQueries.GetProfileByProjectAndID(context.Background(), GetProfileByProjectAndIDParams{
ProjectID: randomEntities.proj.ID,
ID: oneSelectorProfile.ID,
})
require.NoError(t, err)
require.Len(t, oneResult, 1)
require.Len(t, oneResult[0].ProfilesWithSelectors, 1)
require.Contains(t, oneResult[0].ProfilesWithSelectors, oneSel)

noResult, err := testQueries.GetProfileByProjectAndID(context.Background(), GetProfileByProjectAndIDParams{
ProjectID: randomEntities.proj.ID,
ID: noSelectors.ID,
})
require.NoError(t, err)
require.Len(t, noResult, 1)
require.Len(t, noResult[0].ProfilesWithSelectors, 0)

multiResult, err := testQueries.GetProfileByProjectAndID(context.Background(), GetProfileByProjectAndIDParams{
ProjectID: randomEntities.proj.ID,
ID: multiSelectorProfile.ID,
})
require.NoError(t, err)
require.Len(t, multiResult, 1)
require.Len(t, multiResult[0].ProfilesWithSelectors, 3)
require.Subset(t, multiResult[0].ProfilesWithSelectors, []ProfileSelector{mulitSel1, mulitSel2, mulitSel3})
})
}

func TestProfileLabels(t *testing.T) {
Expand Down

0 comments on commit 881c672

Please sign in to comment.