这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions cli/commands/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func newMigrate(dir string, db *url.URL, accessKey string, logger *logrus.Logger
return t, nil
}

func executeMigration(cmd string, t *migrate.Migrate, stepOrVersion int64) error {
func ExecuteMigration(cmd string, t *migrate.Migrate, stepOrVersion int64) error {
var err error

switch cmd {
Expand Down Expand Up @@ -88,7 +88,6 @@ func executeStatus(t *migrate.Migrate) (*migrate.Status, error) {
func getDataPath(nurl *url.URL, accessKey string) *url.URL {
host := &url.URL{
Scheme: "hasuradb",
User: url.UserPassword("admin", accessKey),
Host: nurl.Host,
Path: nurl.Path,
}
Expand All @@ -100,6 +99,9 @@ func getDataPath(nurl *url.URL, accessKey string) *url.URL {
default:
q.Set("sslmode", "disable")
}
if accessKey != "" {
q.Add("headers", "X-Hasura-Access-Key:"+accessKey)
}
host.RawQuery = q.Encode()
return host
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/migrate_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (o *migrateApplyOptions) run() error {
return err
}

err = executeMigration(migrationType, migrateDrv, step)
err = ExecuteMigration(migrationType, migrateDrv, step)
if err != nil {
if err == migrate.ErrNoChange {
o.EC.Logger.Info("nothing to apply")
Expand Down
7 changes: 4 additions & 3 deletions cli/commands/migrate_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
if err != nil {
return err
}
printStatus(status)
buf := PrintStatus(status)
fmt.Println(buf.String())
return nil
},
}
Expand All @@ -49,7 +50,7 @@ func (o *migrateStatusOptions) run() (*migrate.Status, error) {
return status, nil
}

func printStatus(status *migrate.Status) {
func PrintStatus(status *migrate.Status) *bytes.Buffer {
out := new(tabwriter.Writer)
buf := &bytes.Buffer{}
out.Init(buf, 0, 8, 2, ' ', 0)
Expand All @@ -63,7 +64,7 @@ func printStatus(status *migrate.Status) {
)
}
out.Flush()
fmt.Println(buf.String())
return buf
}

func convertBool(ok bool) string {
Expand Down
76 changes: 25 additions & 51 deletions cli/migrate/database/hasuradb/hasuradb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,7 @@ func init() {

const (
DefaultMigrationsTable = "schema_migrations"
DefaultRole = "admin"
DefaultUserID = "0"
DefaultSchema = "hdb_catalog"
ACCESS_KEY_HEADER = "X-Hasura-Access-Key"
)

var (
Expand All @@ -42,8 +39,7 @@ type Config struct {
MigrationsTable string
SettingsTable string
URL *nurl.URL
Role string
UserID string
Headers map[string]string
isCMD bool
}

Expand Down Expand Up @@ -96,46 +92,36 @@ func (h *HasuraDB) Open(url string, isCMD bool, logger *log.Logger) (database.Dr
logger.Debug(err)
return nil, err
}
// Use sslMode to set Scheme
values := hurl.Query()
sslMode := values.Get("sslmode")
// Use sslMode query param to set Scheme
var scheme string
params := hurl.Query()
sslMode := params.Get("sslmode")
if sslMode == "enable" {
hurl.Scheme = "https"
scheme = "https"
} else {
hurl.Scheme = "http"
}
values.Del("sslmode")
hurl.RawQuery = values.Encode()
var user, pass string
switch hurl.User {
case nil:
user = DefaultRole
pass = DefaultUserID
default:
user = hurl.User.Username()
if user == "" {
user = DefaultRole
}
tmpPass, ok := hurl.User.Password()
if !ok {
// If Pass not set
pass = DefaultUserID
} else {
pass = tmpPass
scheme = "http"
}

headers := make(map[string]string)
if queryHeaders, ok := params["headers"]; ok {
for _, header := range queryHeaders {
headerValue := strings.SplitN(header, ":", 2)
if len(headerValue) == 2 && headerValue[1] != "" {
headers[headerValue[0]] = headerValue[1]
}
}
}
// Remove UserInfo
hurl.User = nil
// Add v1/query to path
hurl.Path = path.Join(hurl.Path, "v1/query")

hx, err := WithInstance(&Config{
MigrationsTable: DefaultMigrationsTable,
SettingsTable: DefaultSettingsTable,
URL: hurl,
Role: user,
UserID: pass,
isCMD: isCMD,
URL: &nurl.URL{
Scheme: scheme,
Host: hurl.Host,
Path: path.Join(hurl.Path, "v1/query"),
},
isCMD: isCMD,
Headers: headers,
}, logger)

if err != nil {
Expand Down Expand Up @@ -464,8 +450,8 @@ func (h *HasuraDB) sendQuery(m interface{}) (resp *http.Response, body []byte, e

request = request.Post(h.config.URL.String()).Send(m)

if h.config.UserID != "" {
request = request.Set(ACCESS_KEY_HEADER, h.config.UserID)
for headerName, headerValue := range h.config.Headers {
request.Set(headerName, headerValue)
}

resp, body, errs := request.EndBytes()
Expand All @@ -479,18 +465,6 @@ func (h *HasuraDB) sendQuery(m interface{}) (resp *http.Response, body []byte, e
return resp, body, err
}

func SingleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}

func (h *HasuraDB) First() (version uint64, ok bool) {
return h.migrations.First()
}
Expand Down