Skip to content

Commit

Permalink
fix: unsupported data type: &[]
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Oct 15, 2024
1 parent e03ce61 commit 6063127
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions pkg/topology/component_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/query"
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/pkg/errors"
"gorm.io/gorm"
"gorm.io/gorm/clause"
Expand All @@ -18,6 +19,12 @@ import (
"github.com/flanksource/canary-checker/pkg/utils"
)

// ComponentConfigsRelationship
type ComponentConfigsRelationship struct {
ComponentID string
ConfigIDs pq.StringArray `gorm:"type:[]text"`
}

var ComponentConfigRun = &job.Job{
Name: "ComponentConfigRun",
Schedule: "@every 2m",
Expand All @@ -35,24 +42,21 @@ var ComponentConfigRun = &job.Job{
return fmt.Errorf("error getting components: %w", err)
}

var existingRelationships []struct {
ComponentID uuid.UUID
ConfigIDs []uuid.UUID
}
var existingRelationships []ComponentConfigsRelationship
if err := db.Model(&models.ConfigComponentRelationship{}).
Select("component_id, ARRAY_AGG(config_id) AS config_ids").
Where("deleted_at IS NULL").
Group("component_id").Find(&existingRelationships).Error; err != nil {
return fmt.Errorf("error fetching existing config_component_relationships: %w", err)
}

existingConfigIDsByComponentID := make(map[uuid.UUID][]uuid.UUID)
existingConfigIDsByComponentID := make(map[string][]string)
for _, er := range existingRelationships {
existingConfigIDsByComponentID[er.ComponentID] = er.ConfigIDs
}

for _, component := range components {
if err := SyncComponentConfigRelationship(run.Context, component, existingConfigIDsByComponentID[component.ID]); err != nil {
if err := SyncComponentConfigRelationship(run.Context, component, existingConfigIDsByComponentID[component.ID.String()]); err != nil {
run.History.AddError(fmt.Sprintf("error persisting config relationships: %v", err))
continue
}
Expand Down Expand Up @@ -99,12 +103,12 @@ func PersistConfigComponentRelationship(db *gorm.DB, configID, componentID uuid.
}).Create(&relationship).Error
}

func SyncComponentConfigRelationship(ctx context.Context, component pkg.Component, existingConfigIDs []uuid.UUID) error {
func SyncComponentConfigRelationship(ctx context.Context, component pkg.Component, existingConfigIDs []string) error {
if len(component.Configs) == 0 {
return nil
}

var newConfigsIDs []uuid.UUID
var newConfigsIDs []string
var relationshipsToPersist []models.ConfigComponentRelationship

for _, config := range component.Configs {
Expand All @@ -114,9 +118,9 @@ func SyncComponentConfigRelationship(ctx context.Context, component pkg.Componen
}

for _, dbConfigID := range dbConfigIDs {
newConfigsIDs = append(newConfigsIDs, dbConfigID)
newConfigsIDs = append(newConfigsIDs, dbConfigID.String())

if collections.Contains(existingConfigIDs, dbConfigID) {
if collections.Contains(existingConfigIDs, dbConfigID.String()) {
continue
}

Expand Down

0 comments on commit 6063127

Please sign in to comment.