Skip to content

Commit

Permalink
Backfill entity_type in evaluation_rule_entities
Browse files Browse the repository at this point in the history
This backfill this column and makes it non nullable. The DB query used
for listing evaluation history is also changed to use this column.
  • Loading branch information
dmjb committed Jul 29, 2024
1 parent c7a8e15 commit 4ad5053
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
15 changes: 15 additions & 0 deletions database/migrations/000081_evaluation_entity_type_migrate.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- 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.

ALTER TABLE evaluation_rule_entities ALTER COLUMN entity_type DROP NOT NULL;
32 changes: 32 additions & 0 deletions database/migrations/000081_evaluation_entity_type_migrate.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
-- 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;

-- backfill rows which don't have an entity type
UPDATE evaluation_rule_entities
SET entity_type = (CASE
WHEN artifact_id IS NOT NULL
THEN 'artifact'::entities
WHEN pull_request_id IS NOT NULL
THEN 'pull_request'::entities
WHEN repository_id IS NOT NULL
THEN 'repository'::entities
END)
WHERE entity_type IS NULL;

-- make field mandatory
ALTER TABLE evaluation_rule_entities ALTER COLUMN entity_type SET NOT NULL;

COMMIT;
4 changes: 2 additions & 2 deletions database/query/eval_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ SELECT s.id::uuid AS evaluation_id,
WHERE (sqlc.narg(next)::timestamp without time zone IS NULL OR sqlc.narg(next) > s.evaluation_time)
AND (sqlc.narg(prev)::timestamp without time zone IS NULL OR sqlc.narg(prev) < s.evaluation_time)
-- inclusion filters
AND (sqlc.slice(entityTypes)::entities[] IS NULL OR ri.entity_type = ANY(sqlc.slice(entityTypes)::entities[]))
AND (sqlc.slice(entityTypes)::entities[] IS NULL OR ere.entity_type = ANY(sqlc.slice(entityTypes)::entities[]))
AND (sqlc.slice(entityNames)::text[] IS NULL OR ere.repository_id IS NULL OR CONCAT(r.repo_owner, '/', r.repo_name) = ANY(sqlc.slice(entityNames)::text[]))
AND (sqlc.slice(entityNames)::text[] IS NULL OR ere.pull_request_id IS NULL OR pr.pr_number::text = ANY(sqlc.slice(entityNames)::text[]))
AND (sqlc.slice(entityNames)::text[] IS NULL OR ere.artifact_id IS NULL OR a.artifact_name = ANY(sqlc.slice(entityNames)::text[]))
Expand All @@ -153,7 +153,7 @@ SELECT s.id::uuid AS evaluation_id,
AND (sqlc.slice(alerts)::alert_status_types[] IS NULL OR ae.status = ANY(sqlc.slice(alerts)::alert_status_types[]))
AND (sqlc.slice(statuses)::eval_status_types[] IS NULL OR s.status = ANY(sqlc.slice(statuses)::eval_status_types[]))
-- exclusion filters
AND (sqlc.slice(notEntityTypes)::entities[] IS NULL OR ri.entity_type != ANY(sqlc.slice(notEntityTypes)::entities[]))
AND (sqlc.slice(notEntityTypes)::entities[] IS NULL OR ere.entity_type != ANY(sqlc.slice(notEntityTypes)::entities[]))
AND (sqlc.slice(notEntityNames)::text[] IS NULL OR ere.repository_id IS NULL OR CONCAT(r.repo_owner, '/', r.repo_name) != ANY(sqlc.slice(notEntityNames)::text[]))
AND (sqlc.slice(notEntityNames)::text[] IS NULL OR ere.pull_request_id IS NULL OR pr.pr_number::text != ANY(sqlc.slice(notEntityNames)::text[]))
AND (sqlc.slice(notEntityNames)::text[] IS NULL OR ere.artifact_id IS NULL OR a.artifact_name != ANY(sqlc.slice(notEntityNames)::text[]))
Expand Down
6 changes: 3 additions & 3 deletions internal/db/eval_history.sql.go

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

2 changes: 1 addition & 1 deletion internal/db/models.go

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

5 changes: 1 addition & 4 deletions internal/history/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,7 @@ func (e *evaluationHistoryService) StoreEvaluationStatus(
RepositoryID: params.RepositoryID,
PullRequestID: params.PullRequestID,
ArtifactID: params.ArtifactID,
EntityType: db.NullEntities{
Entities: entityType,
Valid: true,
},
EntityType: entityType,
},
)
if err != nil {
Expand Down

0 comments on commit 4ad5053

Please sign in to comment.