Skip to content

Commit

Permalink
Define rule instance table for Minder (#3459)
Browse files Browse the repository at this point in the history
Rule instances are currently stored as serialized protobuf structures in
the database, grouped by entity type. A separate join table exists to
track the relationship between a profile-entity and the rule types which
are used. This arrangement leads to some rather awkward code and
database queries making the profile-related code in Minder more complex
than it should be.

This PR adds in a definition of a dedicated rule instance table.
Subsequent PRs will migrate data over to it and then change the codebase
to query this table instead of the current tables.
  • Loading branch information
dmjb authored May 31, 2024
1 parent 2d6a0ca commit 19a62a2
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 8 deletions.
20 changes: 20 additions & 0 deletions database/migrations/000060_rule_instances.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
-- 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 rule_instances;
ALTER TABLE entity_profiles DROP COLUMN migrated;

COMMIT;
39 changes: 39 additions & 0 deletions database/migrations/000060_rule_instances.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
-- 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 IF NOT EXISTS rule_instances(
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
profile_id UUID NOT NULL REFERENCES profiles(id) ON DELETE CASCADE,
rule_type_id UUID NOT NULL REFERENCES rule_type(id),
name TEXT NOT NULL,
entity_type entities NOT NULL,
def JSONB, -- stores the values needed by the rule type's `def`
params JSONB, -- stores the values needed by the rule type's `params`
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW(),
-- equivalent to constraints enforced by rule validation
UNIQUE (profile_id, entity_type, name)
);

-- this reflects likely access patterns for this table - retrieving all rule
-- instances for a given profile, and all rules by profile and entity type
CREATE INDEX idx_rule_instances_profile ON rule_instances(profile_id);
CREATE INDEX idx_rule_instances_profile_entity ON rule_instances(profile_id, entity_type);

-- this will be used for migration purposes
ALTER TABLE entity_profiles ADD COLUMN migrated BOOL DEFAULT FALSE NOT NULL;

COMMIT;
16 changes: 13 additions & 3 deletions database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,25 @@ WHERE id = $1 AND project_id = $2 RETURNING *;
INSERT INTO entity_profiles (
entity,
profile_id,
contextual_rules) VALUES ($1, $2, sqlc.arg(contextual_rules)::jsonb) RETURNING *;
contextual_rules,
migrated
) VALUES (
$1,
$2,
sqlc.arg(contextual_rules)::jsonb,
FALSE
) RETURNING *;

-- name: UpsertProfileForEntity :one
INSERT INTO entity_profiles (
entity,
profile_id,
contextual_rules) VALUES ($1, $2, sqlc.arg(contextual_rules)::jsonb)
contextual_rules,
migrated
) VALUES ($1, $2, sqlc.arg(contextual_rules)::jsonb, false)
ON CONFLICT (entity, profile_id) DO UPDATE SET
contextual_rules = sqlc.arg(contextual_rules)::jsonb
contextual_rules = sqlc.arg(contextual_rules)::jsonb,
migrated = FALSE
RETURNING *;

-- name: DeleteProfileForEntity :exec
Expand Down
13 changes: 13 additions & 0 deletions internal/db/models.go

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

23 changes: 18 additions & 5 deletions internal/db/profiles.sql.go

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

0 comments on commit 19a62a2

Please sign in to comment.