Skip to content

Commit f5769d2

Browse files
authored
Various changes to the eval history schema service (#3637)
These relate to feedback from the design review, plus making some changes in light of writing code which uses these tables: 1) Use `ON DELETE CASCADE` for all foreign key references. 2) Use an array of timestamps to store the evaluation instances instead of keeping them in a separate table. 3) Change some of the table names. The original names were not particular consistent or intuitive. Since Minder does not use any of these tables at this point, there should be no impact from these changes.
1 parent 31e0ca3 commit f5769d2

File tree

3 files changed

+99
-17
lines changed

3 files changed

+99
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Copyright 2024 Stacklok, Inc
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
15+
BEGIN;
16+
17+
-- change tables back to old names
18+
ALTER TABLE evaluation_rule_entities RENAME TO rule_entity_evaluations;
19+
ALTER TABLE evaluation_statuses RENAME TO evaluation_history;
20+
ALTER TABLE latest_evaluation_statuses RENAME TO latest_evaluation_state;
21+
22+
-- recreate FKs without ON DELETE CASCADE
23+
ALTER TABLE rule_entity_evaluations DROP CONSTRAINT evaluation_rule_entities_artifact_id_fkey;
24+
ALTER TABLE rule_entity_evaluations DROP CONSTRAINT evaluation_rule_entities_repository_id_fkey;
25+
ALTER TABLE rule_entity_evaluations DROP CONSTRAINT evaluation_rule_entities_pull_request_id_fkey;
26+
ALTER TABLE remediation_events DROP CONSTRAINT remediation_events_evaluation_id_fkey;
27+
ALTER TABLE alert_events DROP CONSTRAINT alert_events_evaluation_id_fkey;
28+
29+
ALTER TABLE rule_entity_evaluations ADD FOREIGN KEY (artifact_id) REFERENCES artifacts(id);
30+
ALTER TABLE rule_entity_evaluations ADD FOREIGN KEY (repository_id) REFERENCES repositories(id);
31+
ALTER TABLE rule_entity_evaluations ADD FOREIGN KEY (pull_request_id) REFERENCES pull_requests(id);
32+
ALTER TABLE remediation_events ADD FOREIGN KEY (evaluation_id) REFERENCES evaluation_statuses(id);
33+
ALTER TABLE alert_events ADD FOREIGN KEY (evaluation_id) REFERENCES evaluation_statuses(id);
34+
35+
-- recreate evaluation_instance table
36+
ALTER TABLE evaluation_history DROP COLUMN evaluation_times;
37+
CREATE TABLE evaluation_instance(
38+
evaluation_id UUID NOT NULL REFERENCES evaluation_history(id) ON DELETE CASCADE,
39+
evaluation_time TIMESTAMP NOT NULL DEFAULT NOW(),
40+
PRIMARY KEY (evaluation_id, evaluation_time)
41+
);
42+
43+
COMMIT;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
-- Copyright 2024 Stacklok, Inc
2+
--
3+
-- Licensed under the Apache License, Version 2.0 (the "License");
4+
-- you may not use this file except in compliance with the License.
5+
-- You may obtain a copy of the License at
6+
--
7+
-- http://www.apache.org/licenses/LICENSE-2.0
8+
--
9+
-- Unless required by applicable law or agreed to in writing, software
10+
-- distributed under the License is distributed on an "AS IS" BASIS,
11+
-- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
-- See the License for the specific language governing permissions and
13+
-- limitations under the License.
14+
15+
BEGIN;
16+
17+
-- these changes reflect feedback during design review plus some changes I've
18+
-- wanted to make after writing queries around these tables.
19+
20+
-- give some of the tables better names
21+
-- these tables are not yet used in the codebase, so renaming them should be low risk
22+
ALTER TABLE rule_entity_evaluations RENAME TO evaluation_rule_entities;
23+
ALTER TABLE evaluation_history RENAME TO evaluation_statuses;
24+
ALTER TABLE latest_evaluation_state RENAME to latest_evaluation_statuses;
25+
26+
-- ensure FK cascades are set for entities, and for the alerts/remediations
27+
ALTER TABLE evaluation_rule_entities DROP CONSTRAINT rule_entity_evaluations_artifact_id_fkey;
28+
ALTER TABLE evaluation_rule_entities DROP CONSTRAINT rule_entity_evaluations_repository_id_fkey;
29+
ALTER TABLE evaluation_rule_entities DROP CONSTRAINT rule_entity_evaluations_pull_request_id_fkey;
30+
ALTER TABLE remediation_events DROP CONSTRAINT remediation_events_evaluation_id_fkey;
31+
ALTER TABLE alert_events DROP CONSTRAINT alert_events_evaluation_id_fkey;
32+
33+
ALTER TABLE evaluation_rule_entities ADD FOREIGN KEY (artifact_id) REFERENCES artifacts(id) ON DELETE CASCADE;
34+
ALTER TABLE evaluation_rule_entities ADD FOREIGN KEY (repository_id) REFERENCES repositories(id) ON DELETE CASCADE;
35+
ALTER TABLE evaluation_rule_entities ADD FOREIGN KEY (pull_request_id) REFERENCES pull_requests(id) ON DELETE CASCADE;
36+
ALTER TABLE remediation_events ADD FOREIGN KEY (evaluation_id) REFERENCES evaluation_statuses(id) ON DELETE CASCADE;
37+
ALTER TABLE alert_events ADD FOREIGN KEY (evaluation_id) REFERENCES evaluation_statuses(id) ON DELETE CASCADE;
38+
39+
-- use an array of timestamps instead of a separate table when tracking evaluation instances
40+
DROP TABLE IF EXISTS evaluation_instance;
41+
ALTER TABLE evaluation_statuses ADD COLUMN evaluation_times TIMESTAMP[] NOT NULL DEFAULT ARRAY[NOW()]::TIMESTAMP[];
42+
43+
COMMIT;

internal/db/models.go

+13-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)