graphql-engine/cli/migrate/api/squash.go
Aravind K P 3136ffad1b cli: update go.mod
>

### Description
Update `go.mod` to allow other packages to import [v2.0.0 versions](https://blog.golang.org/v2-go-modules).

### Changelog

- [x] `CHANGELOG.md` is updated with user-facing content relevant to this PR. If no changelog is required, then add the `no-changelog-required` label.

### Affected components

- [x] CLI

https://github.com/hasura/graphql-engine-mono/pull/1584

GitOrigin-RevId: a5d17ad20289d1cd7217763f56ef3ba6552d69c4
2021-06-16 11:45:07 +00:00

109 lines
2.6 KiB
Go

package api
import (
"net/http"
"net/url"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
"github.com/hasura/graphql-engine/cli/v2/migrate"
"github.com/hasura/graphql-engine/cli/v2/migrate/cmd"
mig "github.com/hasura/graphql-engine/cli/v2/migrate/cmd"
)
type squashCreateRequest struct {
Name string `json:"name"`
From uint64 `json:"from"`
version int64
}
type squashDeleteRequest struct {
Versions []int64 `json:"migrations"`
}
func (s *squashCreateRequest) setDefaults() {
if s.Name == "" {
s.Name = "default_squash"
}
startTime := time.Now()
s.version = startTime.UnixNano() / int64(time.Millisecond)
}
func SquashCreateAPI(c *gin.Context) {
migratePtr, ok := c.Get("migrate")
if !ok {
return
}
sourcePtr, ok := c.Get("filedir")
if !ok {
return
}
t := migratePtr.(*migrate.Migrate)
sourceURL := sourcePtr.(*url.URL)
var request squashCreateRequest
// Bind Request body to Request struct
if c.BindJSON(&request) != nil {
c.JSON(500, &Response{Code: "internal_error", Message: "Something went wrong"})
return
}
request.setDefaults()
// Rescan file system
err := t.ReScan()
if err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
return
}
versions, err := cmd.SquashCmd(t, request.From, request.version, request.Name, sourceURL.Path)
if err != nil {
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
}
c.JSON(http.StatusOK, gin.H{"version": request.version, "squashed_migrations": versions})
}
func SquashDeleteAPI(c *gin.Context) {
sourcePtr, ok := c.Get("filedir")
if !ok {
return
}
sourceURL := sourcePtr.(*url.URL)
var request squashDeleteRequest
// Bind Request body to Request struct
if c.BindJSON(&request) != nil {
c.JSON(500, &Response{Code: "internal_error", Message: "Something went wrong"})
return
}
for _, v := range request.Versions {
delOptions := mig.CreateOptions{
Version: strconv.FormatInt(v, 10),
Directory: sourceURL.Path,
}
err := delOptions.Delete()
if err != nil {
c.JSON(500, &Response{Code: "internal_error", Message: "Something went wrong"})
return
}
}
c.JSON(http.StatusOK, gin.H{"message": "Migrations deleted"})
}