Skip to content

Commit

Permalink
fix: panic on db.Gorm
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Aug 29, 2024
1 parent f1e04e2 commit 5989288
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 64 deletions.
3 changes: 0 additions & 3 deletions cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ var Serve = &cobra.Command{
Use: "serve",
PreRun: PreRun,
Run: func(cmd *cobra.Command, args []string) {
// PostgREST needs to know how it is exposed to create the correct links
db.HttpEndpoint = api.PublicURL + "/db"

ctx, _, err := duty.Start("mission-control")
if err != nil {
logger.Fatalf(err.Error())
Expand Down
9 changes: 7 additions & 2 deletions cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"

"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/flanksource/duty/types"
"github.com/flanksource/incident-commander/db"
"github.com/flanksource/incident-commander/db/models"
"github.com/flanksource/incident-commander/utils"
"github.com/spf13/cobra"
Expand All @@ -19,6 +19,11 @@ var Sync = &cobra.Command{
Use: "sync",
PreRun: PreRun,
Run: func(cmd *cobra.Command, args []string) {
ctx, _, err := duty.Start("mission-control")
if err != nil {
logger.Fatalf(err.Error())
}

cwd, _ := os.Getwd()
for _, file := range args {
data, err := readFile(file)
Expand All @@ -42,7 +47,7 @@ var Sync = &cobra.Command{
}
rule.Spec = types.JSON(spec)

tx := db.Gorm.Table("incident_rules").Clauses(clause.OnConflict{
tx := ctx.DB().Table("incident_rules").Clauses(clause.OnConflict{
Columns: []clause.Column{{Name: "name"}},
UpdateAll: true,
}).Create(&rule)
Expand Down
14 changes: 7 additions & 7 deletions db/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,14 @@ func PersistTeamComponents(ctx context.Context, teamComps []api.TeamComponent) e
}).Create(teamComps).Error
}

func LookupRelatedComponentIDs(componentID string, maxDepth int) ([]string, error) {
func LookupRelatedComponentIDs(ctx context.Context, componentID string, maxDepth int) ([]string, error) {
var componentIDs []string

var childRows []struct {
ChildID string
ParentID string
}
if err := Gorm.Raw(`SELECT child_id, parent_id FROM lookup_component_children(?, ?)`, componentID, maxDepth).
if err := ctx.DB().Raw(`SELECT child_id, parent_id FROM lookup_component_children(?, ?)`, componentID, maxDepth).
Scan(&childRows).Error; err != nil {
return componentIDs, err
}
Expand All @@ -60,27 +60,27 @@ func LookupRelatedComponentIDs(componentID string, maxDepth int) ([]string, erro
}

var relatedRows []string
if err := Gorm.Raw(`SELECT id FROM lookup_component_relations(?)`, componentID).
if err := ctx.DB().Raw(`SELECT id FROM lookup_component_relations(?)`, componentID).
Scan(&relatedRows).Error; err != nil {
return componentIDs, err
}
componentIDs = append(componentIDs, relatedRows...)
return componentIDs, nil
}

func LookupIncidentsByComponent(componentID string) ([]string, error) {
func LookupIncidentsByComponent(ctx context.Context, componentID string) ([]string, error) {
var incidentIDs []string
if err := Gorm.Raw(`SELECT id FROM lookup_component_incidents(?)`, componentID).
if err := ctx.DB().Raw(`SELECT id FROM lookup_component_incidents(?)`, componentID).
Scan(&incidentIDs).Error; err != nil {
return incidentIDs, err
}

return incidentIDs, nil
}

func LookupConfigsByComponent(componentID string) ([]string, error) {
func LookupConfigsByComponent(ctx context.Context, componentID string) ([]string, error) {
var configIDs []string
err := Gorm.Raw(`SELECT config_id FROM config_component_relationships WHERE component_id = ?`, componentID).
err := ctx.DB().Raw(`SELECT config_id FROM config_component_relationships WHERE component_id = ?`, componentID).
Scan(&configIDs).Error
if err != nil {
return configIDs, err
Expand Down
6 changes: 3 additions & 3 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"github.com/google/uuid"
)

func LookupRelatedConfigIDs(configID string, maxDepth int) ([]string, error) {
func LookupRelatedConfigIDs(ctx context.Context, configID string, maxDepth int) ([]string, error) {
var configIDs []string

var rows []struct {
ChildID string
ParentID string
}
if err := Gorm.Raw(`SELECT child_id, parent_id FROM lookup_config_children(?, ?)`, configID, maxDepth).
if err := ctx.DB().Raw(`SELECT child_id, parent_id FROM lookup_config_children(?, ?)`, configID, maxDepth).
Scan(&rows).Error; err != nil {
return configIDs, err
}
Expand All @@ -24,7 +24,7 @@ func LookupRelatedConfigIDs(configID string, maxDepth int) ([]string, error) {
}

var relatedRows []string
if err := Gorm.Raw(`SELECT id FROM lookup_config_relations(?)`, configID).
if err := ctx.DB().Raw(`SELECT id FROM lookup_config_relations(?)`, configID).
Scan(&relatedRows).Error; err != nil {
return configIDs, err
}
Expand Down
21 changes: 0 additions & 21 deletions db/init.go

This file was deleted.

4 changes: 2 additions & 2 deletions db/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ func DeleteNotification(ctx context.Context, id string) error {
return ctx.DB().Delete(&models.Notification{}, "id = ?", id).Error
}

func UpdateNotificationError(id string, err string) error {
return Gorm.Model(&models.Notification{}).Where("id = ?", id).Update("error", err).Error
func UpdateNotificationError(ctx context.Context, id string, err string) error {
return ctx.DB().Model(&models.Notification{}).Where("id = ?", id).Update("error", err).Error
}

func DeleteNotificationSendHistory(ctx context.Context, days int) (int64, error) {
Expand Down
2 changes: 1 addition & 1 deletion notification/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ func (t *notificationHandler) addNotificationEvent(ctx context.Context, event mo
}
}
if valid, err := expression.Eval(n.Filter, celEnv, allEnvVars); err != nil {
logs.IfError(db.UpdateNotificationError(id, err.Error()), "failed to update notification")
logs.IfError(db.UpdateNotificationError(ctx, id, err.Error()), "failed to update notification")
continue
} else if !valid {
continue
Expand Down
4 changes: 2 additions & 2 deletions notification/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ func CreateNotificationSendPayloads(ctx context.Context, event models.Event, n *

for _, cn := range teamSpec.Notifications {
if valid, err := expression.Eval(cn.Filter, celEnv, allEnvVars); err != nil {
logs.IfError(db.UpdateNotificationError(n.ID.String(), err.Error()), "failed to update notification")
logs.IfError(db.UpdateNotificationError(ctx, n.ID.String(), err.Error()), "failed to update notification")
} else if !valid {
continue
}
Expand All @@ -295,7 +295,7 @@ func CreateNotificationSendPayloads(ctx context.Context, event models.Event, n *

for _, cn := range n.CustomNotifications {
if valid, err := expression.Eval(cn.Filter, celEnv, allEnvVars); err != nil {
logs.IfError(db.UpdateNotificationError(n.ID.String(), err.Error()), "failed to update notification")
logs.IfError(db.UpdateNotificationError(ctx, n.ID.String(), err.Error()), "failed to update notification")
} else if !valid {
continue
}
Expand Down
28 changes: 14 additions & 14 deletions snapshot/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (
"github.com/flanksource/duty/models"

"github.com/flanksource/duty"
dutyContext "github.com/flanksource/duty/context"
"github.com/flanksource/incident-commander/components"
"github.com/flanksource/incident-commander/db"
)

func getColumnNames(table string) (string, error) {
func getColumnNames(ctx dutyContext.Context, table string) (string, error) {
var columns string
err := db.Gorm.Raw(`SELECT string_agg(column_name, ',') from information_schema.columns where table_name = ?`, table).
err := ctx.DB().Raw(`SELECT string_agg(column_name, ',') from information_schema.columns where table_name = ?`, table).
Scan(&columns).Error

if err != nil {
Expand All @@ -32,14 +32,14 @@ func generateCSVDumpQuery(table, columns, idField, whereClause string) string {
`, columns, table, idField, whereClause)
}

func dumpTable(table string, idField, whereClause string, ids []string, csvDirectory string) error {
func dumpTable(ctx dutyContext.Context, table string, idField, whereClause string, ids []string, csvDirectory string) error {
var rows []map[string]any
columnNames, err := getColumnNames(table)
columnNames, err := getColumnNames(ctx, table)
if err != nil {
return err
}
query := generateCSVDumpQuery(table, columnNames, idField, whereClause)
err = db.Gorm.Raw(query, ids).Scan(&rows).Error
err = ctx.DB().Raw(query, ids).Scan(&rows).Error
if err != nil {
return err
}
Expand All @@ -54,7 +54,7 @@ func dumpComponents(ctx SnapshotContext, componentIDs []string) error {

var allComponents models.Components
for _, componentID := range componentIDs {
response, err := duty.QueryTopology(context.Background(), db.Pool, duty.TopologyOptions{
response, err := duty.QueryTopology(context.Background(), ctx.Context.Pool(), duty.TopologyOptions{
ID: componentID,
})
if err != nil {
Expand All @@ -75,7 +75,7 @@ func dumpComponents(ctx SnapshotContext, componentIDs []string) error {
return err
}

err = dumpTable("components", "id", "?", componentIDs, ctx.Directory)
err = dumpTable(ctx.Context, "components", "id", "?", componentIDs, ctx.Directory)
if err != nil {
return err
}
Expand All @@ -93,18 +93,18 @@ func dumpIncidents(ctx SnapshotContext, incidentIDs []string) error {
return nil
}

err := dumpTable("incidents", "id", "?", incidentIDs, ctx.Directory)
err := dumpTable(ctx.Context, "incidents", "id", "?", incidentIDs, ctx.Directory)
if err != nil {
return err
}

err = dumpTable("hypotheses", "incident_id", "?", incidentIDs, ctx.Directory)
err = dumpTable(ctx.Context, "hypotheses", "incident_id", "?", incidentIDs, ctx.Directory)
if err != nil {
return err
}

whereClause := `SELECT id FROM hypotheses WHERE incident_id IN (?)`
err = dumpTable("evidences", "hypothesis_id", whereClause, incidentIDs, ctx.Directory)
err = dumpTable(ctx.Context, "evidences", "hypothesis_id", whereClause, incidentIDs, ctx.Directory)
if err != nil {
return err
}
Expand All @@ -116,17 +116,17 @@ func dumpConfigs(ctx SnapshotContext, configIDs []string) error {
return nil
}

err := dumpTable("config_items", "id", "?", configIDs, ctx.Directory)
err := dumpTable(ctx.Context, "config_items", "id", "?", configIDs, ctx.Directory)
if err != nil {
return err
}

err = dumpTable("config_changes", "config_id", "?", configIDs, ctx.Directory)
err = dumpTable(ctx.Context, "config_changes", "config_id", "?", configIDs, ctx.Directory)
if err != nil {
return err
}

err = dumpTable("config_analysis", "config_id", "?", configIDs, ctx.Directory)
err = dumpTable(ctx.Context, "config_analysis", "config_id", "?", configIDs, ctx.Directory)
if err != nil {
return err
}
Expand Down
19 changes: 10 additions & 9 deletions snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/flanksource/commons/collections"
"github.com/flanksource/commons/files"
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty/context"

"github.com/flanksource/incident-commander/db"
)
Expand Down Expand Up @@ -56,13 +57,13 @@ func topologySnapshot(ctx SnapshotContext, componentID string, related bool) err
componentIDs := []string{componentID}
resources.componentIDs = append(resources.componentIDs, componentIDs...)
if related {
relatedResources, err := fetchRelatedIDsForComponent(componentIDs)
relatedResources, err := fetchRelatedIDsForComponent(ctx.Context, componentIDs)
if err != nil {
return err
}
resources.merge(relatedResources)

relatedConfigResources, err := fetchRelatedIDsForConfig(relatedResources.configIDs)
relatedConfigResources, err := fetchRelatedIDsForConfig(ctx.Context, relatedResources.configIDs)
if err != nil {
return err
}
Expand All @@ -83,7 +84,7 @@ func configSnapshot(ctx SnapshotContext, configID string, related bool) error {
configIDs := []string{configID}
resources.configIDs = append(resources.configIDs, configIDs...)
if related {
relatedResources, err := fetchRelatedIDsForConfig(resources.configIDs)
relatedResources, err := fetchRelatedIDsForConfig(ctx.Context, resources.configIDs)
if err != nil {
return err
}
Expand All @@ -93,27 +94,27 @@ func configSnapshot(ctx SnapshotContext, configID string, related bool) error {
return resources.dump(ctx)
}

func fetchRelatedIDsForComponent(componentIDs []string) (resource, error) {
func fetchRelatedIDsForComponent(ctx context.Context, componentIDs []string) (resource, error) {
var related resource
related.componentIDs = append(related.configIDs, componentIDs...)

for _, componentID := range componentIDs {
// Fetch related componentIDs
relatedComponentIDs, err := db.LookupRelatedComponentIDs(componentID, -1)
relatedComponentIDs, err := db.LookupRelatedComponentIDs(ctx, componentID, -1)
if err != nil {
return related, err
}
related.componentIDs = append(related.componentIDs, relatedComponentIDs...)

// Fetch related incidentIDs
incidentIDs, err := db.LookupIncidentsByComponent(componentID)
incidentIDs, err := db.LookupIncidentsByComponent(ctx, componentID)
if err != nil {
return related, err
}
related.incidentIDs = append(related.incidentIDs, incidentIDs...)

// Fetch related configIDs
configIDs, err := db.LookupConfigsByComponent(componentID)
configIDs, err := db.LookupConfigsByComponent(ctx, componentID)
if err != nil {
return related, err
}
Expand All @@ -123,12 +124,12 @@ func fetchRelatedIDsForComponent(componentIDs []string) (resource, error) {
return related, nil
}

func fetchRelatedIDsForConfig(configIDs []string) (resource, error) {
func fetchRelatedIDsForConfig(ctx context.Context, configIDs []string) (resource, error) {
var related resource
related.configIDs = append(related.configIDs, configIDs...)

for _, configID := range configIDs {
relatedConfigIDs, err := db.LookupRelatedConfigIDs(configID, -1)
relatedConfigIDs, err := db.LookupRelatedConfigIDs(ctx, configID, -1)
if err != nil {
return related, err
}
Expand Down

0 comments on commit 5989288

Please sign in to comment.