2018-06-24 16:40:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
2018-07-09 16:47:38 +03:00
|
|
|
"fmt"
|
|
|
|
"net/url"
|
|
|
|
"runtime"
|
|
|
|
"strings"
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli"
|
2018-07-09 16:47:38 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/migrate"
|
|
|
|
mig "github.com/hasura/graphql-engine/cli/migrate/cmd"
|
2019-02-14 12:37:47 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/version"
|
2018-07-09 16:47:38 +03:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/sirupsen/logrus"
|
2018-06-24 16:40:48 +03:00
|
|
|
"github.com/spf13/cobra"
|
2019-02-14 12:37:47 +03:00
|
|
|
|
|
|
|
// Initialize migration drivers
|
|
|
|
_ "github.com/hasura/graphql-engine/cli/migrate/database/hasuradb"
|
|
|
|
_ "github.com/hasura/graphql-engine/cli/migrate/source/file"
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
// NewMigrateCmd returns the migrate command
|
2018-06-24 16:40:48 +03:00
|
|
|
func NewMigrateCmd(ec *cli.ExecutionContext) *cobra.Command {
|
|
|
|
migrateCmd := &cobra.Command{
|
|
|
|
Use: "migrate",
|
|
|
|
Short: "Manage migrations on the database",
|
|
|
|
SilenceUsage: true,
|
|
|
|
}
|
|
|
|
migrateCmd.AddCommand(
|
2018-06-28 11:36:25 +03:00
|
|
|
newMigrateApplyCmd(ec),
|
|
|
|
newMigrateStatusCmd(ec),
|
|
|
|
newMigrateCreateCmd(ec),
|
2018-06-24 16:40:48 +03:00
|
|
|
)
|
|
|
|
return migrateCmd
|
|
|
|
}
|
2018-07-09 16:47:38 +03:00
|
|
|
|
2019-09-18 08:36:16 +03:00
|
|
|
func newMigrate(dir string, db *url.URL, adminSecretValue string, logger *logrus.Logger, v *version.Version, isCmd bool) (*migrate.Migrate, error) {
|
2019-02-14 12:37:47 +03:00
|
|
|
dbURL := getDataPath(db, getAdminSecretHeaderName(v), adminSecretValue)
|
2018-07-09 16:47:38 +03:00
|
|
|
fileURL := getFilePath(dir)
|
2019-09-18 08:36:16 +03:00
|
|
|
t, err := migrate.New(fileURL.String(), dbURL.String(), isCmd, logger)
|
2018-07-09 16:47:38 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "cannot create migrate instance")
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
// ExecuteMigration runs the actual migration
|
2018-08-21 21:37:47 +03:00
|
|
|
func ExecuteMigration(cmd string, t *migrate.Migrate, stepOrVersion int64) error {
|
2018-07-09 16:47:38 +03:00
|
|
|
var err error
|
|
|
|
|
|
|
|
switch cmd {
|
|
|
|
case "up":
|
|
|
|
err = mig.UpCmd(t, stepOrVersion)
|
|
|
|
case "down":
|
|
|
|
err = mig.DownCmd(t, stepOrVersion)
|
|
|
|
case "version":
|
|
|
|
var direction string
|
|
|
|
if stepOrVersion >= 0 {
|
|
|
|
direction = "up"
|
|
|
|
} else {
|
|
|
|
direction = "down"
|
|
|
|
stepOrVersion = -(stepOrVersion)
|
|
|
|
}
|
|
|
|
err = mig.GotoCmd(t, uint64(stepOrVersion), direction)
|
|
|
|
default:
|
|
|
|
err = fmt.Errorf("Invalid command")
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func executeStatus(t *migrate.Migrate) (*migrate.Status, error) {
|
|
|
|
status, err := t.GetStatus()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return status, nil
|
|
|
|
}
|
|
|
|
|
2019-02-14 12:37:47 +03:00
|
|
|
func getDataPath(nurl *url.URL, adminSecretHeader, adminSecretValue string) *url.URL {
|
2018-07-09 16:47:38 +03:00
|
|
|
host := &url.URL{
|
|
|
|
Scheme: "hasuradb",
|
|
|
|
Host: nurl.Host,
|
|
|
|
Path: nurl.Path,
|
|
|
|
}
|
|
|
|
q := nurl.Query()
|
|
|
|
// Set sslmode in query
|
|
|
|
switch scheme := nurl.Scheme; scheme {
|
|
|
|
case "https":
|
|
|
|
q.Set("sslmode", "enable")
|
|
|
|
default:
|
|
|
|
q.Set("sslmode", "disable")
|
|
|
|
}
|
2019-02-14 12:37:47 +03:00
|
|
|
if adminSecretValue != "" {
|
|
|
|
q.Add("headers", fmt.Sprintf("%s:%s", adminSecretHeader, adminSecretValue))
|
2018-08-21 21:37:47 +03:00
|
|
|
}
|
2018-07-09 16:47:38 +03:00
|
|
|
host.RawQuery = q.Encode()
|
|
|
|
return host
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFilePath(dir string) *url.URL {
|
|
|
|
host := &url.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
|
|
|
|
}
|
2019-02-14 12:37:47 +03:00
|
|
|
|
|
|
|
const (
|
|
|
|
XHasuraAdminSecret = "X-Hasura-Admin-Secret"
|
|
|
|
XHasuraAccessKey = "X-Hasura-Access-Key"
|
|
|
|
)
|
|
|
|
|
|
|
|
func getAdminSecretHeaderName(v *version.Version) string {
|
|
|
|
if v.ServerSemver == nil {
|
|
|
|
return XHasuraAdminSecret
|
|
|
|
}
|
|
|
|
flags, err := v.GetServerFeatureFlags()
|
|
|
|
if err != nil {
|
|
|
|
return XHasuraAdminSecret
|
|
|
|
}
|
|
|
|
if flags.HasAccessKey {
|
|
|
|
return XHasuraAccessKey
|
|
|
|
}
|
|
|
|
return XHasuraAdminSecret
|
|
|
|
}
|