diff --git a/database/migrations/000082_backfill_latest_eval_profile_id.down.sql b/database/migrations/000082_backfill_latest_eval_profile_id.down.sql new file mode 100644 index 0000000000..9af0630bfd --- /dev/null +++ b/database/migrations/000082_backfill_latest_eval_profile_id.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 latest_evaluation_statuses ALTER COLUMN profile_id DROP NOT NULL; diff --git a/database/migrations/000082_backfill_latest_eval_profile_id.up.sql b/database/migrations/000082_backfill_latest_eval_profile_id.up.sql new file mode 100644 index 0000000000..e88238c733 --- /dev/null +++ b/database/migrations/000082_backfill_latest_eval_profile_id.up.sql @@ -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; + +-- backfill rows which don't have a profile ID +UPDATE latest_evaluation_statuses +SET profile_id = ri.profile_id +FROM rule_instances AS ri +JOIN evaluation_rule_entities AS ere ON ere.rule_id = ri.id +JOIN latest_evaluation_statuses AS les ON les.rule_entity_id = ere.id +WHERE les.profile_id IS NULL; + +-- make field mandatory +ALTER TABLE latest_evaluation_statuses ALTER COLUMN profile_id SET NOT NULL; + +COMMIT; \ No newline at end of file diff --git a/internal/db/eval_history.sql.go b/internal/db/eval_history.sql.go index 37ac761606..39f7ff16e2 100644 --- a/internal/db/eval_history.sql.go +++ b/internal/db/eval_history.sql.go @@ -493,9 +493,9 @@ SET evaluation_history_id = $2 ` type UpsertLatestEvaluationStatusParams struct { - RuleEntityID uuid.UUID `json:"rule_entity_id"` - EvaluationHistoryID uuid.UUID `json:"evaluation_history_id"` - ProfileID uuid.NullUUID `json:"profile_id"` + RuleEntityID uuid.UUID `json:"rule_entity_id"` + EvaluationHistoryID uuid.UUID `json:"evaluation_history_id"` + ProfileID uuid.UUID `json:"profile_id"` } func (q *Queries) UpsertLatestEvaluationStatus(ctx context.Context, arg UpsertLatestEvaluationStatusParams) error { diff --git a/internal/db/models.go b/internal/db/models.go index e86920b00b..9bf3733eab 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -513,9 +513,9 @@ type FlushCache struct { } type LatestEvaluationStatus struct { - RuleEntityID uuid.UUID `json:"rule_entity_id"` - EvaluationHistoryID uuid.UUID `json:"evaluation_history_id"` - ProfileID uuid.NullUUID `json:"profile_id"` + RuleEntityID uuid.UUID `json:"rule_entity_id"` + EvaluationHistoryID uuid.UUID `json:"evaluation_history_id"` + ProfileID uuid.UUID `json:"profile_id"` } type Profile struct { diff --git a/internal/history/service.go b/internal/history/service.go index e8fa19ecb2..b744ecad09 100644 --- a/internal/history/service.go +++ b/internal/history/service.go @@ -143,10 +143,7 @@ func (_ *evaluationHistoryService) createNewStatus( db.UpsertLatestEvaluationStatusParams{ RuleEntityID: ruleEntityID, EvaluationHistoryID: newEvaluationID, - ProfileID: uuid.NullUUID{ - UUID: profileID, - Valid: true, - }, + ProfileID: profileID, }, ) if err != nil {