2018-06-24 16:40:48 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2021-03-08 14:59:35 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/internal/hasura"
|
|
|
|
|
2021-01-18 20:11:05 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli"
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
|
|
)
|
|
|
|
|
|
|
|
func MetadataAPI(c *gin.Context) {
|
2019-09-18 08:36:16 +03:00
|
|
|
// Get migrate instance
|
2021-01-18 20:11:05 +03:00
|
|
|
ecPtr, ok := c.Get("ec")
|
2018-06-24 16:40:48 +03:00
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to url.URL
|
2021-01-18 20:11:05 +03:00
|
|
|
ec, ok := ecPtr.(*cli.ExecutionContext)
|
|
|
|
if !ok {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: "cannot get execution context"})
|
|
|
|
return
|
|
|
|
}
|
2021-03-08 14:59:35 +03:00
|
|
|
t, err := migrate.NewMigrate(ec, false, "", hasura.SourceKindPG)
|
2021-01-18 20:11:05 +03:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
|
|
|
|
// Switch on request method
|
|
|
|
switch c.Request.Method {
|
|
|
|
case "GET":
|
2020-02-24 19:14:46 +03:00
|
|
|
files, err := t.ExportMetadata()
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
|
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "data_api_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
queryValues := c.Request.URL.Query()
|
|
|
|
export := queryValues.Get("export")
|
|
|
|
if export == "true" {
|
2020-02-24 19:14:46 +03:00
|
|
|
err := t.WriteMetadata(files)
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
c.JSON(http.StatusOK, &gin.H{"metadata": "Success"})
|
2018-06-24 16:40:48 +03:00
|
|
|
case "POST":
|
|
|
|
var request Request
|
|
|
|
|
|
|
|
// Bind Request body to Request struct
|
|
|
|
if c.BindJSON(&request) != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: "Something went wrong"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err := t.Query(request.Up)
|
|
|
|
if err != nil {
|
|
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "data_api_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == migrate.ErrMigrationMode {
|
|
|
|
c.JSON(http.StatusBadRequest, &Response{Code: "migration_mode_enabled", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
files, err := t.ExportMetadata()
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
|
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "data_api_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2020-02-24 19:14:46 +03:00
|
|
|
err = t.WriteMetadata(files)
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
2020-02-24 19:14:46 +03:00
|
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "data_api_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusOK, &gin.H{"message": "Success"})
|
|
|
|
default:
|
|
|
|
c.JSON(http.StatusMethodNotAllowed, &gin.H{"message": "Method not allowed"})
|
|
|
|
}
|
|
|
|
}
|