From 4ad505374e42d97f2b8210928c8992e726f80102 Mon Sep 17 00:00:00 2001 From: Don Browne Date: Thu, 25 Jul 2024 11:30:31 +0100 Subject: [PATCH] Backfill `entity_type` in `evaluation_rule_entities` This backfill this column and makes it non nullable. The DB query used for listing evaluation history is also changed to use this column. --- ...81_evaluation_entity_type_migrate.down.sql | 15 +++++++++ ...0081_evaluation_entity_type_migrate.up.sql | 32 +++++++++++++++++++ database/query/eval_history.sql | 4 +-- internal/db/eval_history.sql.go | 6 ++-- internal/db/models.go | 2 +- internal/history/service.go | 5 +-- 6 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 database/migrations/000081_evaluation_entity_type_migrate.down.sql create mode 100644 database/migrations/000081_evaluation_entity_type_migrate.up.sql diff --git a/database/migrations/000081_evaluation_entity_type_migrate.down.sql b/database/migrations/000081_evaluation_entity_type_migrate.down.sql new file mode 100644 index 0000000000..b6271a7ae3 --- /dev/null +++ b/database/migrations/000081_evaluation_entity_type_migrate.down.sql @@ -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; diff --git a/database/migrations/000081_evaluation_entity_type_migrate.up.sql b/database/migrations/000081_evaluation_entity_type_migrate.up.sql new file mode 100644 index 0000000000..f7a7ffbbb9 --- /dev/null +++ b/database/migrations/000081_evaluation_entity_type_migrate.up.sql @@ -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; \ No newline at end of file diff --git a/database/query/eval_history.sql b/database/query/eval_history.sql index 18082c808a..fd9b3996fb 100644 --- a/database/query/eval_history.sql +++ b/database/query/eval_history.sql @@ -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[])) @@ -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[])) diff --git a/internal/db/eval_history.sql.go b/internal/db/eval_history.sql.go index 7086427f8c..37ac761606 100644 --- a/internal/db/eval_history.sql.go +++ b/internal/db/eval_history.sql.go @@ -144,7 +144,7 @@ type InsertEvaluationRuleEntityParams struct { RepositoryID uuid.NullUUID `json:"repository_id"` PullRequestID uuid.NullUUID `json:"pull_request_id"` ArtifactID uuid.NullUUID `json:"artifact_id"` - EntityType NullEntities `json:"entity_type"` + EntityType Entities `json:"entity_type"` } func (q *Queries) InsertEvaluationRuleEntity(ctx context.Context, arg InsertEvaluationRuleEntityParams) (uuid.UUID, error) { @@ -270,7 +270,7 @@ SELECT s.id::uuid AS evaluation_id, WHERE ($1::timestamp without time zone IS NULL OR $1 > s.evaluation_time) AND ($2::timestamp without time zone IS NULL OR $2 < s.evaluation_time) -- inclusion filters - AND ($3::entities[] IS NULL OR ri.entity_type = ANY($3::entities[])) + AND ($3::entities[] IS NULL OR ere.entity_type = ANY($3::entities[])) AND ($4::text[] IS NULL OR ere.repository_id IS NULL OR CONCAT(r.repo_owner, '/', r.repo_name) = ANY($4::text[])) AND ($4::text[] IS NULL OR ere.pull_request_id IS NULL OR pr.pr_number::text = ANY($4::text[])) AND ($4::text[] IS NULL OR ere.artifact_id IS NULL OR a.artifact_name = ANY($4::text[])) @@ -279,7 +279,7 @@ SELECT s.id::uuid AS evaluation_id, AND ($7::alert_status_types[] IS NULL OR ae.status = ANY($7::alert_status_types[])) AND ($8::eval_status_types[] IS NULL OR s.status = ANY($8::eval_status_types[])) -- exclusion filters - AND ($9::entities[] IS NULL OR ri.entity_type != ANY($9::entities[])) + AND ($9::entities[] IS NULL OR ere.entity_type != ANY($9::entities[])) AND ($10::text[] IS NULL OR ere.repository_id IS NULL OR CONCAT(r.repo_owner, '/', r.repo_name) != ANY($10::text[])) AND ($10::text[] IS NULL OR ere.pull_request_id IS NULL OR pr.pr_number::text != ANY($10::text[])) AND ($10::text[] IS NULL OR ere.artifact_id IS NULL OR a.artifact_name != ANY($10::text[])) diff --git a/internal/db/models.go b/internal/db/models.go index 4ee700ea50..434c730ec9 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -491,7 +491,7 @@ type EvaluationRuleEntity struct { RepositoryID uuid.NullUUID `json:"repository_id"` PullRequestID uuid.NullUUID `json:"pull_request_id"` ArtifactID uuid.NullUUID `json:"artifact_id"` - EntityType NullEntities `json:"entity_type"` + EntityType Entities `json:"entity_type"` } type EvaluationStatus struct { diff --git a/internal/history/service.go b/internal/history/service.go index 6719d2a14d..e8fa19ecb2 100644 --- a/internal/history/service.go +++ b/internal/history/service.go @@ -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 {