Skip to content
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

Extend the Profile List database calls to include selectors #3786

Merged
merged 2 commits into from
Jul 8, 2024

Conversation

jhrozek
Copy link
Contributor

@jhrozek jhrozek commented Jul 4, 2024

Summary

Extend the Profile List database calls to include selectors

Since the profile selectors need to be included when listing profiles, we need to extend the two profile list calls to list selectors as well.

Because the selectors are stored in a different table, but we still need to retrieve them in the same Go struct defined in models, we define a new SQL type in a migration that represents the selectors and tell sqlc to use it in sqlc.yaml.

This new Go struct needs a Scan method as well to convert raw SQL rows into structs as well.

Co-authored-by: Michelangelo Mori [email protected]

Fixes: #3721

Change Type

  • Bug fix (resolves an issue without affecting existing features)
  • Feature (adds new functionality without breaking changes)
  • Breaking change (may impact existing functionalities or require documentation updates)
  • Documentation (updates or additions to documentation)
  • Refactoring or test improvements (no bug fixes or new functionality)

Testing

There are unit tests and I tested the code as part of a larger branch, too.

Review Checklist:

  • Reviewed my own code for quality and clarity.
  • Added comments to complex or tricky code sections.
  • Updated any affected documentation.
  • Included tests that validate the fix or feature.
  • Checked that related changes are merged.

internal/db/domain.go Outdated Show resolved Hide resolved
internal/db/models.go Outdated Show resolved Hide resolved
internal/db/profiles.sql.go Outdated Show resolved Hide resolved
@jhrozek jhrozek changed the title WIP: DB List WIP: List Profiles along with their selectors Jul 4, 2024
@jhrozek
Copy link
Contributor Author

jhrozek commented Jul 4, 2024

@blkt 👀

@coveralls
Copy link

Coverage Status

coverage: 52.011% (+0.009%) from 52.002%
when pulling a69dec2 on jhrozek:selector_db
into ff2be84 on stacklok:main.

@jhrozek jhrozek force-pushed the selector_db branch 3 times, most recently from 8bf5f0a to 58a29a1 Compare July 5, 2024 19:51
@coveralls
Copy link

Coverage Status

coverage: 52.213% (+0.009%) from 52.204%
when pulling 58a29a1 on jhrozek:selector_db
into 6274fe5 on stacklok:main.

@coveralls
Copy link

Coverage Status

coverage: 52.208% (+0.004%) from 52.204%
when pulling 58a29a1 on jhrozek:selector_db
into 6274fe5 on stacklok:main.

@coveralls
Copy link

Coverage Status

coverage: 52.208% (+0.004%) from 52.204%
when pulling 58a29a1 on jhrozek:selector_db
into 6274fe5 on stacklok:main.

@jhrozek
Copy link
Contributor Author

jhrozek commented Jul 5, 2024

@blkt thank you for the help on tidying up the models!

I did some follow-up cleanup of the code and I think it's ready for review now. I'm not sure if you can give the final ack given that you are the coauthor on the PR now.

@jhrozek jhrozek changed the title WIP: List Profiles along with their selectors Extend the Profile List database calls to include selectors Jul 5, 2024
@jhrozek jhrozek marked this pull request as ready for review July 5, 2024 20:09
@jhrozek jhrozek requested a review from a team as a code owner July 5, 2024 20:09
blkt
blkt previously approved these changes Jul 6, 2024
Copy link
Contributor

@blkt blkt left a comment

Choose a reason for hiding this comment

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

LGTM! 🎉

sqlc.yaml Outdated
Comment on lines 29 to 32
go_type:
import: "github.com/google/uuid"
type: "UUID"
Copy link
Contributor

Choose a reason for hiding this comment

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

question: why is this override necessary now while it wasn't before?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it wasn't, sorry.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(longer reply: This was a copy-paste from sqlc docs which I didn't remove by accident)

Comment on lines 66 to 82
WITH helper AS(
SELECT pr.id as profid,
ARRAY_AGG(ROW(ps.id, ps.profile_id, ps.entity, ps.selector, ps.comment)::profile_selector) FILTER (WHERE ps.id IS NOT NULL) AS selectors
FROM profiles pr
LEFT 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
JOIN helper ON profiles.id = helper.profid
WHERE profiles.project_id = $1;
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): I think you could get rid of the FILTER clause by moving the LEFT JOIN on the outer query. I expect it to work the same given the result is a slice.

Suggested change
WITH helper AS(
SELECT pr.id as profid,
ARRAY_AGG(ROW(ps.id, ps.profile_id, ps.entity, ps.selector, ps.comment)::profile_selector) FILTER (WHERE ps.id IS NOT NULL) AS selectors
FROM profiles pr
LEFT 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
JOIN helper ON profiles.id = helper.profid
WHERE profiles.project_id = $1;
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;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion taken!

Comment on lines 85 to 99
WITH helper AS(
SELECT pr.id as profid,
ARRAY_AGG(ROW(ps.id, ps.profile_id, ps.entity, ps.selector, ps.comment)::profile_selector) FILTER (WHERE ps.id IS NOT NULL) AS selectors
FROM profiles pr
LEFT 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
JOIN helper ON profiles.id = helper.profid
Copy link
Contributor

Choose a reason for hiding this comment

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

suggestion (non-blocking): I think you could get rid of the FILTER clause by moving the LEFT JOIN on the outer query. I expect it to work the same given the result is a slice.

Suggested change
WITH helper AS(
SELECT pr.id as profid,
ARRAY_AGG(ROW(ps.id, ps.profile_id, ps.entity, ps.selector, ps.comment)::profile_selector) FILTER (WHERE ps.id IS NOT NULL) AS selectors
FROM profiles pr
LEFT 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
JOIN helper ON profiles.id = helper.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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

suggestion taken!

Since the profile selectors need to be included when listing profiles,
we need to extend the two profile list calls to list selectors as well.

Because the selectors are stored in a different table, but we still need to
retrieve them in the same Go struct defined in models, we define a new SQL
type in a migration that represents the selectors and tell sqlc to use it in
sqlc.yaml.

This new Go struct needs a Scan method as well to convert raw SQL rows
into structs as well.

Fixes: mindersec#3721

Co-authored-by: Michelangelo Mori <[email protected]>
@coveralls
Copy link

coveralls commented Jul 7, 2024

Coverage Status

coverage: 52.208% (+0.004%) from 52.204%
when pulling 15fc001 on jhrozek:selector_db
into 6274fe5 on stacklok:main.

@jhrozek jhrozek merged commit 11c145f into mindersec:main Jul 8, 2024
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Extend Profile database calls with selector support
3 participants