cli: remove migration files if api returns error (close #4312) (#4319)

When using console from CLI and a migration is created, the files are written first and an API call is made to Hasura to execute that migration. There was a bug which caused this file to remain when the API call to Hasura failed. This commit fixes the bug by deleting the files if Hasura API call fails and propagates the error to console.
This commit is contained in:
Aravind Shankar 2020-04-08 10:54:47 +05:30 committed by GitHub
parent d5e61716f8
commit 325771e406
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 24 deletions

View File

@ -11,6 +11,7 @@ The order and collapsed state of columns is now persisted across page navigation
### Bug fixes and improvements ### Bug fixes and improvements
- cli: clean up migration files created during a failed migrate api (close #4312) (#4319)
- cli: add support for multiple versions of plugin (close #4105) - cli: add support for multiple versions of plugin (close #4105)
- cli: template assets path in console HTML for unversioned builds - cli: template assets path in console HTML for unversioned builds
- console: allow customising graphql field names for columns of views (close #3689) (#4255) - console: allow customising graphql field names for columns of views (close #3689) (#4255)

View File

@ -84,9 +84,10 @@ func MigrateAPI(c *gin.Context) {
c.JSON(http.StatusOK, status) c.JSON(http.StatusOK, status)
case "POST": case "POST":
var request Request var request Request
var err error
// Bind Request body to Request struct // Bind Request body to Request struct
if err := c.BindJSON(&request); err != nil { if err = c.BindJSON(&request); err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "request_parse_error", Message: err.Error()}) c.JSON(http.StatusInternalServerError, &Response{Code: "request_parse_error", Message: err.Error()})
return return
} }
@ -135,14 +136,14 @@ func MigrateAPI(c *gin.Context) {
} }
if sqlUp.String() != "" { if sqlUp.String() != "" {
err := createOptions.SetSQLUp(sqlUp.String()) err = createOptions.SetSQLUp(sqlUp.String())
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()}) c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()})
return return
} }
} }
if sqlDown.String() != "" { if sqlDown.String() != "" {
err := createOptions.SetSQLDown(sqlDown.String()) err = createOptions.SetSQLDown(sqlDown.String())
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()}) c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()})
return return
@ -150,25 +151,16 @@ func MigrateAPI(c *gin.Context) {
} }
if sqlUp.String() != "" || sqlDown.String() != "" { if sqlUp.String() != "" || sqlDown.String() != "" {
err := createOptions.Create() err = createOptions.Create()
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()}) c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()})
return return
} }
defer func() {
if err != nil {
err := createOptions.Delete()
if err != nil {
logger.Debug(err)
}
}
}()
} else { } else {
timestamp = 0 timestamp = 0
} }
} else { } else {
err := createOptions.SetMetaUp(request.Up) err = createOptions.SetMetaUp(request.Up)
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()}) c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()})
return return
@ -184,19 +176,19 @@ func MigrateAPI(c *gin.Context) {
c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()}) c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()})
return return
} }
defer func() {
if err != nil {
err = createOptions.Delete()
if err != nil {
logger.Debug(err)
}
}
}()
} }
defer func() {
if err != nil && timestamp != 0 {
err := createOptions.Delete()
if err != nil {
logger.Debug(err)
}
}
}()
// Rescan file system // Rescan file system
err := t.ReScan() err = t.ReScan()
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()}) c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
return return