2018-06-24 16:40:48 +03:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2019-06-26 16:12:44 +03:00
|
|
|
"fmt"
|
2018-06-24 16:40:48 +03:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
|
|
"github.com/hasura/graphql-engine/cli/migrate/cmd"
|
2018-07-09 16:47:38 +03:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
DataAPIError = "Data Error: "
|
|
|
|
MigrationMode = "migration_mode"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Response struct {
|
|
|
|
Code string `json:"code,omitempty"`
|
|
|
|
Message string `json:"message,omitempty"`
|
|
|
|
Name string `json:"name,omitempty"`
|
|
|
|
StatusCode int `json:"-"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Request struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Up []interface{} `json:"up"`
|
2018-07-20 13:31:33 +03:00
|
|
|
Down []interface{} `json:"down"`
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func MigrateAPI(c *gin.Context) {
|
|
|
|
// Get File url
|
|
|
|
sourcePtr, ok := c.Get("filedir")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get hasuradb url
|
|
|
|
databasePtr, ok := c.Get("dbpath")
|
|
|
|
if !ok {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:47:38 +03:00
|
|
|
// Get Logger
|
|
|
|
loggerPtr, ok := c.Get("logger")
|
|
|
|
if !ok {
|
2018-06-24 16:40:48 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-09 16:47:38 +03:00
|
|
|
// Convert to url.URL
|
|
|
|
databaseURL := databasePtr.(*url.URL)
|
|
|
|
sourceURL := sourcePtr.(*url.URL)
|
|
|
|
logger := loggerPtr.(*logrus.Logger)
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
// Create new migrate
|
2018-07-09 16:47:38 +03:00
|
|
|
t, err := migrate.New(sourceURL.String(), databaseURL.String(), false, logger)
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// Switch on request method
|
|
|
|
switch c.Request.Method {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
2018-07-09 16:47:38 +03:00
|
|
|
// Convert to Millisecond
|
2018-06-24 16:40:48 +03:00
|
|
|
timestamp := startTime.UnixNano() / int64(time.Millisecond)
|
|
|
|
|
2018-07-20 13:31:33 +03:00
|
|
|
createOptions := cmd.New(timestamp, request.Name, sourceURL.Path)
|
|
|
|
err = createOptions.SetMetaUp(request.Up)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = createOptions.SetMetaDown(request.Down)
|
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = createOptions.Create()
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "create_file_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rescan file system
|
2018-06-28 11:36:57 +03:00
|
|
|
err = t.ReScan()
|
2018-06-24 16:40:48 +03:00
|
|
|
if err != nil {
|
2018-07-20 13:31:33 +03:00
|
|
|
deleteErr := createOptions.Delete()
|
2018-06-24 16:40:48 +03:00
|
|
|
if deleteErr != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "delete_file_error", Message: deleteErr.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = t.Migrate(uint64(timestamp), "up"); err != nil {
|
2018-07-20 13:31:33 +03:00
|
|
|
deleteErr := createOptions.Delete()
|
2018-06-24 16:40:48 +03:00
|
|
|
if deleteErr != nil {
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "delete_file_error", Message: deleteErr.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(err.Error(), DataAPIError) {
|
|
|
|
c.JSON(http.StatusBadRequest, &Response{Code: "data_api_error", Message: strings.TrimPrefix(err.Error(), DataAPIError)})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == migrate.ErrNoMigrationMode {
|
|
|
|
c.JSON(http.StatusBadRequest, &Response{Code: "migration_mode_disabled", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
|
|
|
|
return
|
|
|
|
}
|
2019-06-26 16:12:44 +03:00
|
|
|
c.JSON(http.StatusOK, &Response{Name: fmt.Sprintf("%d_%s", timestamp, request.Name)})
|
2018-06-24 16:40:48 +03:00
|
|
|
default:
|
|
|
|
c.JSON(http.StatusMethodNotAllowed, &gin.H{"message": "Method not allowed"})
|
|
|
|
}
|
|
|
|
}
|