Skip to content

Commit

Permalink
Define evaluation history schemas (#3565)
Browse files Browse the repository at this point in the history
Fixes #3555

Implements the new database schema for storing evaluation history. Compared with the existing tables for profile/rule status, this will track every single evaluation, and not just the most recent state. This will be used to build an API for querying the history of evaluations for a given rule/entity pair.

Queries and logic to fill the tables will be implemented in the next PR.
  • Loading branch information
dmjb authored Jun 10, 2024
1 parent f7fa491 commit 55f84a9
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 0 deletions.
24 changes: 24 additions & 0 deletions database/migrations/000064_evaluation_history.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
-- 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;

DROP TABLE IF EXISTS rule_entity_evaluations;
DROP TABLE IF EXISTS evaluation_history;
DROP TABLE IF EXISTS evaluation_instance;
DROP TABLE IF EXISTS latest_evaluation_state;
DROP TABLE IF EXISTS remediation_events;
DROP TABLE IF EXISTS alert_events;

COMMIT;
67 changes: 67 additions & 0 deletions database/migrations/000064_evaluation_history.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
-- 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;

CREATE TABLE rule_entity_evaluations(
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
rule_id UUID NOT NULL REFERENCES rule_instances(id) ON DELETE CASCADE,
-- Copying the same pattern of linking to entity types as used in rule_evaluations
repository_id UUID REFERENCES repositories(id),
pull_request_id UUID REFERENCES pull_requests(id),
artifact_id UUID REFERENCES artifacts(id),
UNIQUE(rule_id, repository_id),
UNIQUE(rule_id, pull_request_id),
UNIQUE(rule_id, artifact_id),
-- exactly one entity ID column must be set
CONSTRAINT one_entity_id CHECK (num_nonnulls(repository_id, artifact_id, pull_request_id) = 1)
);

CREATE TABLE evaluation_history(
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
rule_entity_id UUID NOT NULL REFERENCES rule_entity_evaluations(id) ON DELETE CASCADE,
status eval_status_types NOT NULL,
details TEXT NOT NULL
);

CREATE TABLE evaluation_instance(
evaluation_id UUID NOT NULL REFERENCES evaluation_history(id) ON DELETE CASCADE,
evaluation_time TIMESTAMP NOT NULL DEFAULT NOW(),
PRIMARY KEY (evaluation_id, evaluation_time)
);

CREATE TABLE latest_evaluation_state(
rule_entity_id UUID NOT NULL PRIMARY KEY REFERENCES rule_entity_evaluations(id) ON DELETE CASCADE,
evaluation_history_id UUID NOT NULL REFERENCES evaluation_history(id)
);

CREATE TABLE remediation_events(
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
evaluation_id UUID NOT NULL REFERENCES evaluation_history(id),
status remediation_status_types NOT NULL,
details TEXT NOT NULL,
metadata JSONB NOT NULL DEFAULT '{}'::JSONB,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

CREATE TABLE alert_events(
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
evaluation_id UUID NOT NULL REFERENCES evaluation_history(id),
status alert_status_types NOT NULL,
details TEXT NOT NULL,
metadata JSONB NOT NULL DEFAULT '{}'::JSONB,
created_at TIMESTAMP NOT NULL DEFAULT NOW()
);

COMMIT;
43 changes: 43 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 55f84a9

Please sign in to comment.