Skip to content

Commit

Permalink
migrate from rule_evaluations to evaluation_rule_entities type by type
Browse files Browse the repository at this point in the history
  • Loading branch information
dmjb committed Aug 6, 2024
1 parent 090e737 commit 501c986
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 5 additions & 0 deletions internal/db/models.go

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

0 comments on commit 501c986

Please sign in to comment.