Skip to content

Commit

Permalink
fix: relation_type
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe committed Mar 15, 2024
1 parent e652f1f commit d7e7f0b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
6 changes: 2 additions & 4 deletions tests/config_relationship_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,24 +250,22 @@ var _ = ginkgo.Describe("Config relationship recursive", ginkgo.Ordered, func()
var _ = ginkgo.Describe("Config relationship", ginkgo.Ordered, func() {
ginkgo.It("should return OUTGOING relationships", func() {
var relatedConfigs []models.RelatedConfig
err := DefaultContext.DB().Raw("SELECT * FROM related_configs(?)", dummy.KubernetesCluster.ID).Find(&relatedConfigs).Error
err := DefaultContext.DB().Raw("SELECT * FROM related_configs(?, 'outgoing')", dummy.KubernetesCluster.ID).Find(&relatedConfigs).Error
Expect(err).To(BeNil())

Expect(len(relatedConfigs)).To(Equal(2))
for _, rc := range relatedConfigs {
Expect(rc.Relation).To(Equal("ClusterNode"))
Expect(rc.RelationType).To(Equal(models.RelatedConfigTypeOutgoing))
Expect(rc.ID.String()).To(BeElementOf([]string{dummy.KubernetesNodeA.ID.String(), dummy.KubernetesNodeB.ID.String()}))
}
})

ginkgo.It("should return INCOMING relationships", func() {
var relatedConfigs []models.RelatedConfig
err := DefaultContext.DB().Raw("SELECT * FROM related_configs(?, 'all', false)", dummy.KubernetesNodeA.ID).Find(&relatedConfigs).Error
err := DefaultContext.DB().Raw("SELECT * FROM related_configs(?, 'incoming', false)", dummy.KubernetesNodeA.ID).Find(&relatedConfigs).Error
Expect(err).To(BeNil())

Expect(len(relatedConfigs)).To(Equal(1))
Expect(relatedConfigs[0].Relation).To(Equal("ClusterNode"))
Expect(relatedConfigs[0].RelationType).To(Equal(models.RelatedConfigTypeIncoming))
Expect(relatedConfigs[0].ID.String()).To(Equal(dummy.KubernetesCluster.ID.String()))
})
Expand Down
10 changes: 5 additions & 5 deletions views/006_config_views.sql
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ CREATE OR REPLACE FUNCTION related_config_ids_recursive (
config_id UUID,
type_filter TEXT DEFAULT 'outgoing',
max_depth INT DEFAULT 5
) RETURNS TABLE (id UUID,related_id UUID,relation_type TEXT,direction TEXT, depth INT) AS $$
) RETURNS TABLE (id UUID, related_id UUID, relation_type TEXT, direction TEXT, depth INT) AS $$
BEGIN

IF type_filter NOT IN ('incoming', 'outgoing', 'all') THEN
Expand All @@ -426,7 +426,7 @@ END IF;
IF type_filter = 'outgoing' THEN
RETURN query
WITH RECURSIVE cte (config_id, related_id, relation, depth) AS (
SELECT parent.related_id, parent.config_id as related_id, parent.relation, 1::int
SELECT parent.related_id, parent.config_id as related_id, 'outgoing', 1::int
FROM config_relationships parent
WHERE parent.config_id = related_config_ids_recursive.config_id
AND deleted_at IS NULL
Expand All @@ -444,8 +444,8 @@ IF type_filter = 'outgoing' THEN
ORDER BY cte.depth asc;
ELSEIF type_filter = 'incoming' THEN
RETURN query
WITH RECURSIVE cte (config_id,related_id, relation,depth) AS (
SELECT parent.config_id, parent.related_id as related_id, parent.relation, 1::int
WITH RECURSIVE cte (config_id, related_id,relation, depth) AS (
SELECT parent.config_id, parent.related_id as related_id, 'incoming', 1::int
FROM config_relationships parent
WHERE parent.related_id = related_config_ids_recursive.config_id
AND deleted_at IS NULL
Expand Down Expand Up @@ -593,7 +593,7 @@ BEGIN
END IF;

RETURN query
SELECT cc.id, cc.config_id, c.name, c.type, cc.external_created_by, cc.created_at, cc.severity, cc.change_type, cc.source, cc.summary,cc.created_by,c.agent_id
SELECT cc.id, cc.config_id, c.name, c.type, cc.external_created_by, cc.created_at, cc.severity, cc.change_type, cc.source, cc.summary, cc.created_by, c.agent_id
FROM config_changes cc
LEFT JOIN config_items c on c.id = cc.config_id
WHERE cc.config_id = lookup_id
Expand Down

0 comments on commit d7e7f0b

Please sign in to comment.