Skip to content

Commit

Permalink
fix: deletion logic in push topology
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmehrotra committed Sep 24, 2024
1 parent ca45d4e commit 194960d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
5 changes: 5 additions & 0 deletions push/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ var _ = ginkgo.Describe("Push", ginkgo.Ordered, func() {
tree, err := query.Topology(DefaultContext, query.TopologyOptions{ID: componentLaptop.ID.String(), Depth: 10, NoCache: true})
Expect(err).To(BeNil())

// Mess up ids to ensure pushed IDs do not matter and deletion works correctly
tree.Components[0].ID = uuid.New()
tree.Components[0].Components[0].ID = uuid.New()
tree.Components[0].Components[1].ID = uuid.New()

httpClient := http.NewClient()
endpoint := fmt.Sprintf("http://localhost:%d/push/topology", echoServerPort)
resp, err := httpClient.R(DefaultContext).
Expand Down
7 changes: 3 additions & 4 deletions push/topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,18 @@ func PushTopology(c echo.Context) error {
}

data.AgentID = agentID
compIDs := []uuid.UUID{data.ID}
for _, c := range data.Components.Walk() {
c.AgentID = agentID
c.TopologyID = &topologyObj.ID
compIDs = append(compIDs, c.ID)
}

if err := dutytopology.SaveComponent(ctx, &data); err != nil {
returnedIDs, err := dutytopology.SaveComponent(ctx, &data)

Check failure on line 63 in push/topology.go

View workflow job for this annotation

GitHub Actions / lint

assignment mismatch: 2 variables but dutytopology.SaveComponent returns 1 value (typecheck)

Check failure on line 63 in push/topology.go

View workflow job for this annotation

GitHub Actions / lint

assignment mismatch: 2 variables but dutytopology.SaveComponent returns 1 value) (typecheck)
if err != nil {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "error saving components: %v", dutydb.ErrorDetails(err)))
}

var idsToDelete []string
if err := ctx.DB().Model(&models.Component{}).Select("id").Where("topology_id = ?", data.TopologyID).Where("id NOT IN ?", compIDs).Find(&idsToDelete).Error; err != nil {
if err := ctx.DB().Model(&models.Component{}).Select("id").Where("topology_id = ?", data.TopologyID).Where("id NOT IN ?", returnedIDs).Find(&idsToDelete).Error; err != nil {
return dutyAPI.WriteError(c, dutyAPI.Errorf(dutyAPI.EINTERNAL, "error querying old components: %v", dutydb.ErrorDetails(err)))
}

Expand Down

0 comments on commit 194960d

Please sign in to comment.