Skip to content

Commit

Permalink
Support empty profiles by utilizing views (#2936)
Browse files Browse the repository at this point in the history
* Don't fail validation of an empty profile

* Add a view over profiles and entity_profiles

* Join over view, not the entity_profiles table

* Remove unused query
  • Loading branch information
jhrozek authored Apr 4, 2024
1 parent 1de308e commit a4c9b01
Show file tree
Hide file tree
Showing 13 changed files with 143 additions and 162 deletions.
19 changes: 19 additions & 0 deletions database/migrations/000046_entity_profiles_views.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- 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 VIEW IF EXISTS profiles_with_entity_profiles;

COMMIT;
21 changes: 21 additions & 0 deletions database/migrations/000046_entity_profiles_views.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
-- 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 VIEW profiles_with_entity_profiles AS(
SELECT entity_profiles.*, profiles.id as profid FROM profiles LEFT JOIN entity_profiles ON profiles.id = entity_profiles.profile_id
);

COMMIT;
15 changes: 0 additions & 15 deletions database/mock/store.go

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

10 changes: 3 additions & 7 deletions database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ 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 * FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id
SELECT * FROM profiles JOIN profiles_with_entity_profiles ON profiles.id = profiles_with_entity_profiles.profid
WHERE profiles.project_id = $1 AND profiles.id = $2;

-- name: GetProfileByID :one
Expand All @@ -52,16 +52,12 @@ SELECT * FROM profiles WHERE id = $1 AND project_id = $2 FOR UPDATE;
-- name: GetProfileByNameAndLock :one
SELECT * FROM profiles WHERE lower(name) = lower(sqlc.arg(name)) AND project_id = $1 FOR UPDATE;

-- name: GetEntityProfileByProjectAndName :many
SELECT * FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id
WHERE profiles.project_id = $1 AND lower(profiles.name) = lower(sqlc.arg(name));

-- name: ListProfilesByProjectID :many
SELECT sqlc.embed(profiles), sqlc.embed(entity_profiles) FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id
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
WHERE profiles.project_id = $1;

-- name: ListProfilesByProjectIDAndLabel :many
SELECT sqlc.embed(profiles), sqlc.embed(entity_profiles) FROM profiles JOIN entity_profiles ON profiles.id = entity_profiles.profile_id
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
WHERE profiles.project_id = $1
AND (
-- the most common case first, if the include_labels is empty, we list profiles with no labels
Expand Down
10 changes: 8 additions & 2 deletions internal/controlplane/handlers_profile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,16 @@ func TestCreateProfile(t *testing.T) {
name: "Create profile with no rules",
profile: &minderv1.CreateProfileRequest{
Profile: &minderv1.Profile{
Name: "test",
Name: "test_norules",
},
},
result: &minderv1.CreateProfileResponse{
Profile: &minderv1.Profile{
Name: "test_norules",
Alert: proto.String("on"),
Remediate: proto.String("off"),
},
},
wantErr: `Couldn't create profile: validation failed: profile must have at least one rule`,
},
{
name: "Create profile with valid name and rules",
Expand Down
11 changes: 11 additions & 0 deletions internal/db/models.go

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

152 changes: 42 additions & 110 deletions internal/db/profiles.sql.go

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

1 change: 0 additions & 1 deletion internal/db/querier.go

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

Loading

0 comments on commit a4c9b01

Please sign in to comment.