-
Notifications
You must be signed in to change notification settings - Fork 37
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
Add database table and methods for profile selectors #3731
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
-- Copyright 2024 Stacklok, Inc | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
|
||
BEGIN; | ||
|
||
-- drop the index on the project column | ||
DROP INDEX IF EXISTS idx_profile_selectors_on_profile; | ||
|
||
-- drop the profile_selectors table | ||
DROP TABLE IF EXISTS profile_selectors; | ||
|
||
COMMIT; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-- Copyright 2024 Stacklok, Inc | ||
-- | ||
-- Licensed under the Apache License, Version 2.0 (the "License"); | ||
-- you may not use this file except in compliance with the License. | ||
-- You may obtain a copy of the License at | ||
-- | ||
-- http://www.apache.org/licenses/LICENSE-2.0 | ||
-- | ||
-- Unless required by applicable law or agreed to in writing, software | ||
-- distributed under the License is distributed on an "AS IS" BASIS, | ||
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
-- See the License for the specific language governing permissions and | ||
-- limitations under the License. | ||
|
||
BEGIN; | ||
|
||
CREATE TABLE IF NOT EXISTS profile_selectors ( | ||
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY, | ||
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE, | ||
entity entities, -- this is nullable since it can be applicable to all | ||
selector TEXT NOT NULL, -- CEL expression | ||
comment TEXT NOT NULL -- optional comment (can be empty string) | ||
); | ||
|
||
-- Ensure we have performant search based on profile_id | ||
CREATE INDEX idx_profile_selectors_on_profile ON profile_selectors(profile_id); | ||
|
||
COMMIT; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- name: CreateSelector :one | ||
INSERT INTO profile_selectors (profile_id, entity, selector, comment) | ||
VALUES ($1, $2, $3, $4) | ||
RETURNING id, profile_id, entity, selector, comment; | ||
|
||
-- name: GetSelectorsByProfileID :many | ||
SELECT id, profile_id, entity, selector, comment | ||
FROM profile_selectors | ||
WHERE profile_id = $1; | ||
|
||
-- name: UpdateSelector :one | ||
UPDATE profile_selectors | ||
SET entity = $2, selector = $3, comment = $4 | ||
WHERE id = $1 | ||
RETURNING id, profile_id, entity, selector, comment; | ||
|
||
-- name: DeleteSelector :exec | ||
DELETE FROM profile_selectors | ||
WHERE id = $1; | ||
|
||
-- name: GetSelectorByID :one | ||
SELECT id, profile_id, entity, selector, comment | ||
FROM profile_selectors | ||
WHERE id = $1; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really wanted to add an upsert, but since the only column to conflict on would be the ID, the upsert would be able to add a zero UUID. I tried working around that with a little more SQL code, like:
but that breaks sqlc - it wasn't able to infer the type of $1 and generated parameters with a column named
Column_1
and typeinterface{}
. Trying to usesqlc.arg(id)
instead of$1
didn't work either, because sqlc apparently requires that all parameters be used in the query.So I went with just an update now and will handle the upsert case in the code instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If
profile_id
is not unique, I'm not sure what theupsert
would look like unless you always have the UUID already created (e.g. client-side generation).profile_id
is not marked as unique at the moment, so I'm not sure that upsert is particularly useful -- you'll know whether you have an existing UUID or not, and can switch between the insert and update pretty easily.