Query history: Add migration endpoint (#47551)

* Add endpoint for migration

* Check for createdAt

* Query history: Remove returning of dtos

* Query history: Fix CreatedAt

* Refactor based on suggestions

* Insert into table in batches
This commit is contained in:
Ivana Huckova
2022-04-14 09:33:41 +02:00
committed by GitHub
parent 17e44c306c
commit aceedb3a32
5 changed files with 218 additions and 0 deletions

View File

@ -19,6 +19,8 @@ func (s *QueryHistoryService) registerAPIEndpoints() {
entities.Post("/star/:uid", middleware.ReqSignedIn, routing.Wrap(s.starHandler))
entities.Delete("/star/:uid", middleware.ReqSignedIn, routing.Wrap(s.unstarHandler))
entities.Patch("/:uid", middleware.ReqSignedIn, routing.Wrap(s.patchCommentHandler))
// Remove migrate endpoint in Grafana v10 as breaking change
entities.Post("/migrate", middleware.ReqSignedIn, routing.Wrap(s.migrateHandler))
})
}
@ -117,3 +119,17 @@ func (s *QueryHistoryService) unstarHandler(c *models.ReqContext) response.Respo
return response.JSON(http.StatusOK, QueryHistoryResponse{Result: query})
}
func (s *QueryHistoryService) migrateHandler(c *models.ReqContext) response.Response {
cmd := MigrateQueriesToQueryHistoryCommand{}
if err := web.Bind(c.Req, &cmd); err != nil {
return response.Error(http.StatusBadRequest, "bad request data", err)
}
totalCount, starredCount, err := s.MigrateQueriesToQueryHistory(c.Req.Context(), c.SignedInUser, cmd)
if err != nil {
return response.Error(http.StatusInternalServerError, "Failed to migrate query history", err)
}
return response.JSON(http.StatusOK, QueryHistoryMigrationResponse{Message: "Query history successfully migrated", TotalCount: totalCount, StarredCount: starredCount})
}