Skip to content

Commit

Permalink
Remove use of entity_profile_rules table
Browse files Browse the repository at this point in the history
PR #3903 replaced the one part of minder which queried the data in the
`entity_profile_rules` table. This PR removes the code which adds and
removes entries to this table. A future PR will drop the table outright.
  • Loading branch information
dmjb committed Jul 16, 2024
1 parent a175dd9 commit 1839a4e
Show file tree
Hide file tree
Showing 6 changed files with 6 additions and 229 deletions.
44 changes: 0 additions & 44 deletions database/mock/store.go

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

11 changes: 0 additions & 11 deletions database/query/profiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,6 @@ RETURNING *;
-- name: DeleteProfileForEntity :exec
DELETE FROM entity_profiles WHERE profile_id = $1 AND entity = $2;

-- name: GetProfileForEntity :one
SELECT * FROM entity_profiles WHERE profile_id = $1 AND entity = $2;

-- name: GetProfileByProjectAndID :many
WITH helper AS(
SELECT pr.id as profid,
Expand Down Expand Up @@ -132,14 +129,6 @@ AND (
DELETE FROM profiles
WHERE id = $1 AND project_id = $2;

-- name: UpsertRuleInstantiation :one
INSERT INTO entity_profile_rules (entity_profile_id, rule_type_id)
VALUES ($1, $2)
ON CONFLICT (entity_profile_id, rule_type_id) DO NOTHING RETURNING *;

-- name: DeleteRuleInstantiation :exec
DELETE FROM entity_profile_rules WHERE entity_profile_id = $1 AND rule_type_id = $2;

-- name: ListProfilesInstantiatingRuleType :many
SELECT DISTINCT(p.name)
FROM profiles AS p
Expand Down
61 changes: 0 additions & 61 deletions internal/db/profiles.sql.go

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

3 changes: 0 additions & 3 deletions internal/db/querier.go

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

Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/stacklok/minder/internal/profiles/models"
htmltemplate "html/template"
"strings"

"github.com/stacklok/minder/internal/db"

"github.com/google/go-github/v61/github"
"github.com/rs/zerolog"
"github.com/stacklok/minder/internal/db"
"github.com/stacklok/minder/internal/profiles/models"
"google.golang.org/protobuf/reflect/protoreflect"

enginerr "github.com/stacklok/minder/internal/engine/errors"
Expand Down
111 changes: 4 additions & 107 deletions internal/profiles/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (p *profileService) UpdateProfile(
minderv1.Entity_ENTITY_TASK_RUN: profile.GetTaskRun(),
minderv1.Entity_ENTITY_BUILD: profile.GetBuild(),
} {
if err = updateProfileRulesForEntity(ctx, ent, &updatedProfile, qtx, entRules, rules); err != nil {
if err = updateProfileRulesForEntity(ctx, ent, &updatedProfile, qtx, entRules); err != nil {
return nil, err
}

Expand Down Expand Up @@ -298,11 +298,6 @@ func (p *profileService) UpdateProfile(
}

unusedRuleStatuses := getUnusedOldRuleStatuses(rules, oldRules)
unusedRuleTypes := getUnusedOldRuleTypes(rules, oldRules)

if err := deleteUnusedRulesFromProfile(ctx, &updatedProfile, unusedRuleTypes, qtx); err != nil {
return nil, status.Errorf(codes.Internal, "error updating profile: %v", err)
}

if err := deleteRuleStatusesForProfile(ctx, &updatedProfile, unusedRuleStatuses, qtx); err != nil {
return nil, status.Errorf(codes.Internal, "error updating profile: %v", err)
Expand Down Expand Up @@ -371,7 +366,7 @@ func copyFieldValue(dstReflect, srcReflect protoreflect.Message, fieldDesc proto
dstList.Truncate(0)

// append all elements from the source list to the destination list
// effectivelly replacing the destination list with the source list
// effectively replacing the destination list with the source list
for i := 0; i < srcList.Len(); i++ {
dstList.Append(srcList.Get(i))
}
Expand Down Expand Up @@ -410,7 +405,7 @@ func createProfileRulesForEntity(
log.Printf("error marshalling %s rules: %v", entity, err)
return status.Errorf(codes.Internal, "error creating profile")
}
entProf, err := qtx.CreateProfileForEntity(ctx, db.CreateProfileForEntityParams{
_, err = qtx.CreateProfileForEntity(ctx, db.CreateProfileForEntityParams{
ProfileID: profile.ID,
Entity: entities.EntityTypeToDB(entity),
ContextualRules: marshalled,
Expand All @@ -420,27 +415,6 @@ func createProfileRulesForEntity(
return status.Errorf(codes.Internal, "error creating profile")
}

for idx := range rulesInProf {
ruleRef := rulesInProf[idx]

if ruleRef.Entity != entity {
continue
}

ruleID := ruleRef.RuleID

_, err := qtx.UpsertRuleInstantiation(ctx, db.UpsertRuleInstantiationParams{
EntityProfileID: entProf.ID,
RuleTypeID: ruleID,
})
if errors.Is(err, sql.ErrNoRows) {
log.Printf("the rule instantiation for rule already existed.")
} else if err != nil {
log.Printf("error creating rule instantiation: %v", err)
return status.Errorf(codes.Internal, "error creating profile")
}
}

return err
}

Expand Down Expand Up @@ -612,48 +586,12 @@ func (_ *profileService) getRulesFromProfile(
return rulesInProf, nil
}

func deleteUnusedRulesFromProfile(
ctx context.Context,
profile *db.Profile,
unusedRules []EntityAndRuleTuple,
querier db.Querier,
) error {
for _, rule := range unusedRules {
// get entity profile
log.Printf("getting profile for entity %s", rule.Entity)
entProf, err := querier.GetProfileForEntity(ctx, db.GetProfileForEntityParams{
ProfileID: profile.ID,
Entity: entities.EntityTypeToDB(rule.Entity),
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
log.Printf("skipping rule deletion for entity %s, profile not found", rule.Entity)
continue
}
log.Printf("error getting profile for entity %s: %v", rule.Entity, err)
return fmt.Errorf("error getting profile for entity %s: %w", rule.Entity, err)
}

log.Printf("deleting rule instantiation for rule %s for entity profile %s", rule.RuleID, entProf.ID)
if err := querier.DeleteRuleInstantiation(ctx, db.DeleteRuleInstantiationParams{
EntityProfileID: entProf.ID,
RuleTypeID: rule.RuleID,
}); err != nil {
log.Printf("error deleting rule instantiation: %v", err)
return fmt.Errorf("error deleting rule instantiation: %w", err)
}
}

return nil
}

func updateProfileRulesForEntity(
ctx context.Context,
entity minderv1.Entity,
profile *db.Profile,
qtx db.Querier,
rules []*minderv1.Profile_Rule,
rulesInProf RuleMapping,
) error {
if len(rules) == 0 {
return qtx.DeleteProfileForEntity(ctx, db.DeleteProfileForEntityParams{
Expand All @@ -667,7 +605,7 @@ func updateProfileRulesForEntity(
log.Printf("error marshalling %s rules: %v", entity, err)
return status.Errorf(codes.Internal, "error creating profile")
}
entProf, err := qtx.UpsertProfileForEntity(ctx, db.UpsertProfileForEntityParams{
_, err = qtx.UpsertProfileForEntity(ctx, db.UpsertProfileForEntityParams{
ProfileID: profile.ID,
Entity: entities.EntityTypeToDB(entity),
ContextualRules: marshalled,
Expand All @@ -677,25 +615,6 @@ func updateProfileRulesForEntity(
return err
}

for idx := range rulesInProf {
ruleRef := rulesInProf[idx]

if ruleRef.Entity != entity {
continue
}

_, err := qtx.UpsertRuleInstantiation(ctx, db.UpsertRuleInstantiationParams{
EntityProfileID: entProf.ID,
RuleTypeID: ruleRef.RuleID,
})
if errors.Is(err, sql.ErrNoRows) {
log.Printf("the rule instantiation for rule already existed.")
} else if err != nil {
log.Printf("error creating rule instantiation: %v", err)
return status.Errorf(codes.Internal, "error updating profile")
}
}

return err
}

Expand All @@ -713,28 +632,6 @@ func getUnusedOldRuleStatuses(
return unusedRuleStatuses
}

func getUnusedOldRuleTypes(newRules, oldRules RuleMapping) []EntityAndRuleTuple {
var unusedRuleTypes []EntityAndRuleTuple

oldRulesTypeMap := make(map[string]EntityAndRuleTuple)
for ruleTypeAndName, rule := range oldRules {
oldRulesTypeMap[ruleTypeAndName.RuleType] = rule
}

newRulesTypeMap := make(map[string]EntityAndRuleTuple)
for ruleTypeAndName, rule := range newRules {
newRulesTypeMap[ruleTypeAndName.RuleType] = rule
}

for ruleType, rule := range oldRulesTypeMap {
if _, ok := newRulesTypeMap[ruleType]; !ok {
unusedRuleTypes = append(unusedRuleTypes, rule)
}
}

return unusedRuleTypes
}

func deleteRuleStatusesForProfile(
ctx context.Context,
profile *db.Profile,
Expand Down

0 comments on commit 1839a4e

Please sign in to comment.