Skip to content

Commit

Permalink
Add database table and methods for profile selectors (#3731)
Browse files Browse the repository at this point in the history
* Add the selectors table

Adds the table for the profile selectors.

Related: #3720

* Add DB methods for selector management

Adds the needed SQL methods to manage profile selectors

Fixes: #3720
  • Loading branch information
jhrozek authored Jun 27, 2024
1 parent 28b8ece commit c3c9453
Show file tree
Hide file tree
Showing 8 changed files with 570 additions and 0 deletions.
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
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

0 comments on commit c3c9453

Please sign in to comment.