Repository navigation
Expand file tree
/
Copy pathsquash.go
More file actions
113 lines (96 loc) · 2.66 KB
/
Copy pathsquash.go
File metadata and controls
113 lines (96 loc) · 2.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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"`
To *int64 `json:"to"`
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
}
if request.To == nil {
var v int64 = -1
request.To = &v
}
versions, err := cmd.SquashCmd(t, request.From, *request.To, 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"})
}