Skip to content

Commit

Permalink
Remove per-entity tables
Browse files Browse the repository at this point in the history
Signed-off-by: Juan Antonio Osorio <[email protected]>
  • Loading branch information
JAORMX committed Aug 28, 2024
1 parent 2e45c75 commit 1769938
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 1,589 deletions.
Empty file.
23 changes: 23 additions & 0 deletions database/migrations/000101_remove_entities_tables.up.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;

-- remove the repositories, artifacts and pull_request tables

DROP TABLE IF EXISTS repositories;
DROP TABLE IF EXISTS artifacts;
DROP TABLE IF EXISTS pull_requests;

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

Large diffs are not rendered by default.

34 changes: 0 additions & 34 deletions database/query/artifacts.sql

This file was deleted.

60 changes: 9 additions & 51 deletions database/query/eval_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -99,21 +99,9 @@ INSERT INTO alert_events(
SELECT s.id::uuid AS evaluation_id,
s.evaluation_time as evaluated_at,
ere.entity_type,
-- entity id
CAST(
CASE
WHEN ere.repository_id IS NOT NULL THEN r.id
WHEN ere.pull_request_id IS NOT NULL THEN pr.id
WHEN ere.artifact_id IS NOT NULL THEN a.id
END AS uuid
) AS entity_id,
ere.entity_instance_id as new_entity_id,
ere.entity_instance_id as entity_id,
-- raw fields for entity names
r.repo_owner,
r.repo_name,
pr.pr_number,
a.artifact_name,
j.id as project_id,
ei.name as entity_name,
-- rule type, name, and profile
rt.name AS rule_type,
ri.name AS rule_name,
Expand All @@ -133,9 +121,7 @@ FROM evaluation_statuses s
JOIN rule_instances ri ON ere.rule_id = ri.id
JOIN rule_type rt ON ri.rule_type_id = rt.id
JOIN profiles p ON ri.profile_id = p.id
LEFT JOIN repositories r ON ere.repository_id = r.id
LEFT JOIN pull_requests pr ON ere.pull_request_id = pr.id
LEFT JOIN artifacts a ON ere.artifact_id = a.id
LEFT JOIN entity_instances ei ON ei.id = ere.entity_instance_id
LEFT JOIN remediation_events re ON re.evaluation_id = s.id
LEFT JOIN alert_events ae ON ae.evaluation_id = s.id
LEFT JOIN projects j ON r.project_id = j.id
Expand All @@ -145,21 +131,9 @@ WHERE s.id = sqlc.arg(evaluation_id) AND j.id = sqlc.arg(project_id);
SELECT s.id::uuid AS evaluation_id,
s.evaluation_time as evaluated_at,
ere.entity_type,
-- entity id
CAST(
CASE
WHEN ere.repository_id IS NOT NULL THEN r.id
WHEN ere.pull_request_id IS NOT NULL THEN pr.id
WHEN ere.artifact_id IS NOT NULL THEN a.id
END AS uuid
) AS entity_id,
ere.entity_instance_id as new_entity_id,
ere.entity_instance_id as entity_id,
-- raw fields for entity names
r.repo_owner,
r.repo_name,
pr.pr_number,
a.artifact_name,
j.id as project_id,
ei.name as entity_name,
-- rule type, name, and profile
rt.name AS rule_type,
ri.name AS rule_name,
Expand All @@ -179,9 +153,7 @@ SELECT s.id::uuid AS evaluation_id,
JOIN rule_instances ri ON ere.rule_id = ri.id
JOIN rule_type rt ON ri.rule_type_id = rt.id
JOIN profiles p ON ri.profile_id = p.id
LEFT JOIN repositories r ON ere.repository_id = r.id
LEFT JOIN pull_requests pr ON ere.pull_request_id = pr.id
LEFT JOIN artifacts a ON ere.artifact_id = a.id
LEFT JOIN entity_instances ei ON ei.id = ere.entity_instance_id
LEFT JOIN remediation_events re ON re.evaluation_id = s.id
LEFT JOIN alert_events ae ON ae.evaluation_id = s.id
LEFT JOIN projects j ON r.project_id = j.id
Expand Down Expand Up @@ -219,25 +191,11 @@ SELECT s.id::uuid AS evaluation_id,
SELECT s.evaluation_time,
s.id,
ere.rule_id,
-- entity type
CAST(
CASE
WHEN ere.repository_id IS NOT NULL THEN 1
WHEN ere.pull_request_id IS NOT NULL THEN 2
WHEN ere.artifact_id IS NOT NULL THEN 3
END AS integer
) AS entity_type,
-- entity id
CAST(
CASE
WHEN ere.repository_id IS NOT NULL THEN ere.repository_id
WHEN ere.pull_request_id IS NOT NULL THEN ere.pull_request_id
WHEN ere.artifact_id IS NOT NULL THEN ere.artifact_id
END AS uuid
) AS entity_id,
ere.entity_instance_id as new_entity_id
ei.entity_type,
ere.entity_instance_id as entity_id
FROM evaluation_statuses s
JOIN evaluation_rule_entities ere ON s.rule_entity_id = ere.id
LEFT JOIN entity_instances AS ei ON ei.id = ere.entity_instance_id
LEFT JOIN latest_evaluation_statuses l
ON l.rule_entity_id = s.rule_entity_id
AND l.evaluation_history_id = s.id
Expand Down
21 changes: 6 additions & 15 deletions database/query/profile_status.sql
Original file line number Diff line number Diff line change
Expand Up @@ -66,38 +66,29 @@ SELECT
ad.alert_metadata,
ad.alert_last_updated,
ed.id AS rule_evaluation_id,
ere.repository_id,
ere.entity_type,
ei.id AS entity_id,
ei.entity_type,
ri.name AS rule_name,
repo.repo_name,
repo.repo_owner,
repo.provider,
ei.name AS entity_name,
ei.provider_id,
rt.name AS rule_type_name,
rt.severity_value as rule_type_severity_value,
rt.id AS rule_type_id,
rt.guidance as rule_type_guidance,
rt.display_name as rule_type_display_name,
-- TODO: store entity ID directly in evaluation_rule_entities
CASE
WHEN ere.entity_type = 'artifact'::entities THEN ere.artifact_id
WHEN ere.entity_type = 'repository'::entities THEN ere.repository_id
WHEN ere.entity_type = 'pull_request'::entities THEN ere.pull_request_id
END::uuid as entity_id,
rt.release_phase as rule_type_release_phase
FROM latest_evaluation_statuses les
INNER JOIN evaluation_rule_entities ere ON ere.id = les.rule_entity_id
INNER JOIN entity_instances ei ON ei.id = ere.entity_instance_id
INNER JOIN eval_details ed ON ed.id = les.evaluation_history_id
INNER JOIN remediation_details rd ON rd.evaluation_id = les.evaluation_history_id
INNER JOIN alert_details ad ON ad.evaluation_id = les.evaluation_history_id
INNER JOIN rule_instances AS ri ON ri.id = ere.rule_id
INNER JOIN rule_type rt ON rt.id = ri.rule_type_id
LEFT JOIN repositories repo ON repo.id = ere.repository_id
WHERE les.profile_id = $1 AND
(
CASE
WHEN sqlc.narg(entity_type)::entities = 'repository' AND ere.repository_id = sqlc.narg(entity_id)::UUID THEN true
WHEN sqlc.narg(entity_type)::entities = 'artifact' AND ere.artifact_id = sqlc.narg(entity_id)::UUID THEN true
WHEN sqlc.narg(entity_type)::entities = 'pull_request' AND ere.pull_request_id = sqlc.narg(entity_id)::UUID THEN true
WHEN ei.entity_id = sqlc.narg(entity_id)::UUID THEN true
WHEN sqlc.narg(entity_id)::UUID IS NULL THEN true
ELSE false
END
Expand Down
28 changes: 0 additions & 28 deletions database/query/pull_requests.sql

This file was deleted.

88 changes: 0 additions & 88 deletions database/query/repositories.sql

This file was deleted.

Loading

0 comments on commit 1769938

Please sign in to comment.