-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1270 from jhrozek/pr_in_db
Store PRs in the database to avoid special-casing them during evaluation
- Loading branch information
Showing
17 changed files
with
1,702 additions
and
1,201 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
database/migrations/000003_rule_evaluations_pr_num.down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
-- Copyright 2023 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. | ||
|
||
-- Postgres can't remove a value for an enum type. So, we can't really | ||
-- do a down migration. Instead, we'll just leave this here as a | ||
-- reminder that we can't remove this value. | ||
|
||
-- Drop the existing unique index on rule_evaluations | ||
DROP INDEX IF EXISTS rule_evaluations_results_idx; | ||
|
||
-- Recreate the unique index without COALESCE on pull_request_id | ||
CREATE UNIQUE INDEX rule_evaluations_results_idx | ||
ON rule_evaluations(profile_id, repository_id, COALESCE(artifact_id, '00000000-0000-0000-0000-000000000000'::UUID), entity, rule_type_id) | ||
|
||
-- Remove the pr reference from rule_evaluations | ||
ALTER TABLE rule_evaluations DROP COLUMN IF EXISTS pull_request_id; | ||
|
||
-- Drop the existing unique index on rule_evaluations | ||
DROP INDEX IF EXISTS pr_in_repo_unique; | ||
|
||
-- Drop the pull_requests table | ||
DROP TABLE IF EXISTS pull_requests; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- Copyright 2023 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. | ||
|
||
-- pull_requests table | ||
CREATE TABLE pull_requests ( | ||
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY, | ||
repository_id UUID NOT NULL REFERENCES repositories(id) ON DELETE CASCADE, | ||
pr_number BIGINT NOT NULL, -- BIGINT because GitHub PR numbers are 64-bit integers | ||
created_at TIMESTAMP NOT NULL DEFAULT NOW(), | ||
updated_at TIMESTAMP NOT NULL DEFAULT NOW() | ||
); | ||
|
||
-- Add unique constraint on repository_id and pr_number. Needed for upserts. | ||
CREATE UNIQUE INDEX pr_in_repo_unique ON pull_requests (repository_id, pr_number); | ||
|
||
-- Add pull_request_id to rule_evaluations | ||
ALTER TABLE rule_evaluations | ||
ADD COLUMN pull_request_id UUID REFERENCES pull_requests(id) ON DELETE CASCADE; | ||
|
||
-- Drop the existing unique index on rule_evaluations | ||
DROP INDEX IF EXISTS rule_evaluations_results_idx; | ||
|
||
-- Recreate the unique index with COALESCE on pull_request_id | ||
CREATE UNIQUE INDEX rule_evaluations_results_idx | ||
ON rule_evaluations(profile_id, repository_id, COALESCE(artifact_id, '00000000-0000-0000-0000-000000000000'::UUID), entity, rule_type_id, COALESCE(pull_request_id, '00000000-0000-0000-0000-000000000000'::UUID)); | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
-- name: CreatePullRequest :one | ||
INSERT INTO pull_requests ( | ||
repository_id, | ||
pr_number | ||
) VALUES ($1, $2) RETURNING *; | ||
|
||
-- name: UpsertPullRequest :one | ||
INSERT INTO pull_requests ( | ||
repository_id, | ||
pr_number | ||
) VALUES ($1, $2) | ||
ON CONFLICT (repository_id, pr_number) | ||
DO UPDATE SET | ||
updated_at = NOW() | ||
WHERE pull_requests.repository_id = $1 AND pull_requests.pr_number = $2 | ||
RETURNING *; | ||
|
||
-- name: GetPullRequest :one | ||
SELECT * FROM pull_requests | ||
WHERE repository_id = $1 AND pr_number = $2; | ||
|
||
-- name: DeletePullRequest :exec | ||
DELETE FROM pull_requests | ||
WHERE repository_id = $1 AND pr_number = $2; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.