diff --git a/database/migrations/000087_migrate_rule_statuses.up.sql b/database/migrations/000087_migrate_rule_statuses.up.sql similarity index 81% rename from database/migrations/000087_migrate_rule_statuses.up.sql rename to database/migrations/000087_migrate_rule_statuses.up.sql index 95baa71c6a..54061a8145 100644 --- a/database/migrations/000087_migrate_rule_statuses.up.sql +++ b/database/migrations/000087_migrate_rule_statuses.up.sql @@ -48,10 +48,28 @@ WHERE re.migrated = FALSE; -- evaluation_rule_entities. The other tables we need to migrate have FK -- references to rule_evaluations, so by reusing the same PK ID, we can -- simplify some of the subsequent queries. -INSERT INTO evaluation_rule_entities (id, rule_id, repository_id, pull_request_id, artifact_id, entity_type) -SELECT id, rule_instance_id AS rule_id, repository_id, pull_request_id, artifact_id, entity AS entity_type +-- +-- In an ideal world, we could have a single query which just copies the three +-- entity IDs. However, the rule_evaluations table sometimes has a repository_id +-- for a non-repo entity. Since evaluation_rule_entities has the constraint that +-- only one column is set, copy the rows over type-by-type. +INSERT INTO evaluation_rule_entities (id, rule_id, pull_request_id, entity_type) +SELECT id, rule_instance_id AS rule_id, pull_request_id, entity AS entity_type +FROM rule_evaluations AS re +WHERE re.id IN (SELECT rule_evaluation_id FROM temp_migrate_rule_evaluations) +AND re.entity = 'pull_request'::entities; + +INSERT INTO evaluation_rule_entities (id, rule_id, artifact_id, entity_type) +SELECT id, rule_instance_id AS rule_id, artifact_id, entity AS entity_type +FROM rule_evaluations AS re +WHERE re.id IN (SELECT rule_evaluation_id FROM temp_migrate_rule_evaluations) +AND re.entity = 'artifact'::entities; + +INSERT INTO evaluation_rule_entities (id, rule_id, repository_id, entity_type) +SELECT id, rule_instance_id AS rule_id, repository_id, entity AS entity_type FROM rule_evaluations AS re -WHERE re.id IN (SELECT rule_evaluation_id FROM temp_migrate_rule_evaluations); +WHERE re.id IN (SELECT rule_evaluation_id FROM temp_migrate_rule_evaluations) +AND re.entity = 'repository'::entities; -- Migrate the rule details into evaluation_statuses. INSERT INTO evaluation_statuses (rule_entity_id, status, details, evaluation_time) diff --git a/internal/db/models.go b/internal/db/models.go index f628528318..95d07bc0ce 100644 --- a/internal/db/models.go +++ b/internal/db/models.go @@ -734,6 +734,11 @@ type Subscription struct { CurrentVersion string `json:"current_version"` } +type TempMigrateRuleEvaluation struct { + RuleEvaluationID uuid.UUID `json:"rule_evaluation_id"` + EvaluationStatusID uuid.NullUUID `json:"evaluation_status_id"` +} + type User struct { ID int32 `json:"id"` IdentitySubject string `json:"identity_subject"`