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

Add database table and methods for profile selectors #3731

Merged
merged 2 commits into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions database/migrations/000071_profile_selectors.down.sql
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;
28 changes: 28 additions & 0 deletions database/migrations/000071_profile_selectors.up.sql
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;
74 changes: 74 additions & 0 deletions database/mock/store.go

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

24 changes: 24 additions & 0 deletions database/query/selectors.sql
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
Copy link
Contributor Author

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:

INSERT INTO profile_selectors (id, profile_id, entity, selector, comment)
VALUES (
   CASE WHEN $1::uuid = '00000000-0000-0000-0000-000000000000' THEN gen_random_uuid() ELSE $1::uuid END,
   $2::uuid, $3, $4, $5
)
ON CONFLICT (id) DO UPDATE
    SET profile_id = $2::uuid,
        entity = $3,
        selector = $4,
        comment = $5
RETURNING id, profile_id, entity, selector, comment;

but that breaks sqlc - it wasn't able to infer the type of $1 and generated parameters with a column named Column_1 and type interface{}. Trying to use sqlc.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.

Copy link
Member

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 the upsert 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.

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;
8 changes: 8 additions & 0 deletions internal/db/models.go

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

5 changes: 5 additions & 0 deletions internal/db/querier.go

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

139 changes: 139 additions & 0 deletions internal/db/selectors.sql.go

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

Loading