Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rule_instance_id column on rule_evaluations #4103

Merged
merged 1 commit into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions database/migrations/000088_fix_rule_instance_id.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.

-- Nothing to undo
93 changes: 93 additions & 0 deletions database/migrations/000088_fix_rule_instance_id.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
-- 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;

UPDATE rule_evaluations
SET rule_instance_id = ri.id
FROM rule_instances AS ri
WHERE rule_evaluations.profile_id = ri.profile_id
AND rule_evaluations.rule_name = ri.name
AND rule_evaluations.rule_type_id = ri.rule_type_id
AND rule_evaluations.entity = ri.entity_type;

-- the previous migration may have flagged some rows as migrated which should
-- not have been migrated. Redo them.

UPDATE rule_evaluations AS re
SET migrated = TRUE
FROM evaluation_rule_entities AS ere
WHERE re.entity = 'pull_request'::entities
AND ere.entity_type = 'pull_request'::entities
AND ere.pull_request_id = re.pull_request_id
AND ere.rule_id = re.rule_instance_id;

UPDATE rule_evaluations AS re
SET migrated = TRUE
FROM evaluation_rule_entities AS ere
WHERE re.entity = 'artifact'::entities
AND ere.entity_type = 'artifact'::entities
AND ere.artifact_id = re.artifact_id
AND ere.rule_id = re.rule_instance_id;

UPDATE rule_evaluations AS re
SET migrated = TRUE
FROM evaluation_rule_entities AS ere
WHERE re.entity = 'repository'::entities
AND ere.entity_type = 'repository'::entities
AND ere.repository_id = re.repository_id
AND ere.rule_id = re.rule_instance_id;

-- ensure that any non-migrated rows are set to false

UPDATE rule_evaluations
SET migrated = FALSE
WHERE entity = 'artifact'::entities
AND id IN (
SELECT re.id FROM rule_evaluations AS re
LEFT JOIN evaluation_rule_entities AS ere
ON re.artifact_id = ere.artifact_id
AND re.rule_instance_id = ere.rule_id
AND re.entity = 'artifact'::entities
AND ere.entity_type = 'artifact'::entities
WHERE ere.id IS NULL
);

UPDATE rule_evaluations
SET migrated = FALSE
WHERE entity = 'pull_request'::entities
AND id IN (
SELECT re.id FROM rule_evaluations AS re
LEFT JOIN evaluation_rule_entities AS ere
ON re.pull_request_id = ere.pull_request_id
AND re.rule_instance_id = ere.rule_id
AND re.entity = 'pull_request'::entities
AND ere.entity_type = 'pull_request'::entities
WHERE ere.id IS NULL
);

UPDATE rule_evaluations
SET migrated = FALSE
WHERE entity = 'repository'::entities
AND id IN (
SELECT re.id FROM rule_evaluations AS re
LEFT JOIN evaluation_rule_entities AS ere
ON re.repository_id = ere.repository_id
AND re.rule_instance_id = ere.rule_id
AND re.entity = 'repository'::entities
AND ere.entity_type = 'repository'::entities
WHERE ere.id IS NULL
);

COMMIT;