forked from hasura/graphql-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
189 lines (168 loc) · 4.64 KB
/
util.go
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package migrate
import (
"bufio"
"fmt"
"io"
nurl "net/url"
"runtime"
"strings"
"time"
"github.com/hasura/graphql-engine/cli/metadata"
"github.com/hasura/graphql-engine/cli/metadata/actions"
"github.com/hasura/graphql-engine/cli/metadata/allowlist"
"github.com/hasura/graphql-engine/cli/metadata/functions"
"github.com/hasura/graphql-engine/cli/metadata/querycollections"
"github.com/hasura/graphql-engine/cli/metadata/remoteschemas"
"github.com/hasura/graphql-engine/cli/metadata/tables"
"github.com/hasura/graphql-engine/cli/metadata/types"
"github.com/hasura/graphql-engine/cli/metadata/version"
"github.com/hasura/graphql-engine/cli"
"github.com/pkg/errors"
)
// MultiError holds multiple errors.
type MultiError struct {
Errs []error
}
// NewMultiError returns an error type holding multiple errors.
func NewMultiError(errs ...error) MultiError {
compactErrs := make([]error, 0)
for _, e := range errs {
if e != nil {
compactErrs = append(compactErrs, e)
}
}
return MultiError{compactErrs}
}
// Error implements error. Mulitple errors are concatenated with 'and's.
func (m MultiError) Error() string {
var strs = make([]string, 0)
for _, e := range m.Errs {
if len(e.Error()) > 0 {
strs = append(strs, e.Error())
}
}
return strings.Join(strs, " and ")
}
// suint64 safely converts int to uint64
// see https://goo.gl/wEcqof
// see https://goo.gl/pai7Dr
func suint64(n int64) uint64 {
if n < 0 {
panic(fmt.Sprintf("suint(%v) expects input >= 0", n))
}
return uint64(n)
}
// newSlowReader turns an io.ReadCloser into a slow io.ReadCloser.
// Use this to simulate a slow internet connection.
func newSlowReader(r io.ReadCloser) io.ReadCloser {
return &slowReader{
rx: r,
reader: bufio.NewReader(r),
}
}
type slowReader struct {
rx io.ReadCloser
reader *bufio.Reader
}
func (b *slowReader) Read(p []byte) (n int, err error) {
time.Sleep(10 * time.Millisecond)
c, err := b.reader.ReadByte()
if err != nil {
return 0, err
} else {
copy(p, []byte{c})
return 1, nil
}
}
func (b *slowReader) Close() error {
return b.rx.Close()
}
var errNoScheme = fmt.Errorf("no scheme")
// schemeFromUrl returns the scheme from a URL string
func schemeFromUrl(url string) (string, error) {
u, err := nurl.Parse(url)
if err != nil {
return "", err
}
if len(u.Scheme) == 0 {
return "", errNoScheme
}
return u.Scheme, nil
}
// FilterCustomQuery filters all query values starting with `x-`
func FilterCustomQuery(u *nurl.URL) *nurl.URL {
ux := *u
vx := make(nurl.Values)
for k, v := range ux.Query() {
if len(k) <= 1 || (len(k) > 1 && k[0:2] != "x-") {
vx[k] = v
}
}
ux.RawQuery = vx.Encode()
return &ux
}
func NewMigrate(ec *cli.ExecutionContext, isCmd bool) (*Migrate, error) {
dbURL := GetDataPath(ec)
fileURL := GetFilePath(ec.MigrationDir)
t, err := New(fileURL.String(), dbURL.String(), isCmd, int(ec.Config.Version), ec.Logger)
if err != nil {
return nil, errors.Wrap(err, "cannot create migrate instance")
}
// Set Plugins
SetMetadataPluginsWithDir(ec, t)
return t, nil
}
func GetDataPath(ec *cli.ExecutionContext) *nurl.URL {
url := ec.Config.ServerConfig.ParsedEndpoint
host := &nurl.URL{
Scheme: "hasuradb",
Host: url.Host,
Path: url.Path,
RawQuery: ec.Config.ServerConfig.APIPaths.GetQueryParams().Encode(),
}
q := host.Query()
// Set sslmode in query
switch scheme := url.Scheme; scheme {
case "https":
q.Set("sslmode", "enable")
default:
q.Set("sslmode", "disable")
}
for k, v := range ec.HGEHeaders {
q.Add("headers", fmt.Sprintf("%s:%s", k, v))
}
host.RawQuery = q.Encode()
return host
}
func SetMetadataPluginsWithDir(ec *cli.ExecutionContext, drv *Migrate, dir ...string) {
var metadataDir string
if len(dir) == 0 {
metadataDir = ec.MetadataDir
} else {
metadataDir = dir[0]
}
plugins := make(types.MetadataPlugins, 0)
if ec.Config.Version == cli.V2 && metadataDir != "" {
plugins = append(plugins, version.New(ec, metadataDir))
plugins = append(plugins, tables.New(ec, metadataDir))
plugins = append(plugins, functions.New(ec, metadataDir))
plugins = append(plugins, querycollections.New(ec, metadataDir))
plugins = append(plugins, allowlist.New(ec, metadataDir))
plugins = append(plugins, remoteschemas.New(ec, metadataDir))
plugins = append(plugins, actions.New(ec, metadataDir))
} else {
plugins = append(plugins, metadata.New(ec, ec.MigrationDir))
}
drv.SetMetadataPlugins(plugins)
}
func GetFilePath(dir string) *nurl.URL {
host := &nurl.URL{
Scheme: "file",
Path: dir,
}
// Add Prefix / to path if runtime.GOOS equals to windows
if runtime.GOOS == "windows" && !strings.HasPrefix(host.Path, "/") {
host.Path = "/" + host.Path
}
return host
}