1
0
mirror of https://github.com/hasura/graphql-engine.git synced 2024-12-14 17:02:49 +03:00

cli: add --dry-run flag to migrate apply command (close ) ()

This commit is contained in:
Ashish Rao 2020-06-03 16:49:36 +05:30 committed by GitHub
parent a69f47ccf4
commit d9cc676790
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 97 additions and 13 deletions

View File

@ -84,6 +84,8 @@ Read more about the session argument for computed fields in the [docs](https://h
- cli: list all available commands in root command help (fix #4623) (#4628)
- cli: fix bug with squashing event triggers (close #4883)
- cli: add support for skipping execution while generating migrations through the migrate REST API
- cli: add dry run flag in hasura migrate apply command (fix #3128) (#3499)
- cli: load assets from server when HASURA_GRAPHQL_CONSOLE_ASSETS_DIR is set (close #3382)
- docs: add section on actions vs. remote schemas to actions documentation (#4284)
- docs: fix wrong info about excluding scheme in CORS config (#4685)
- docs: add single object mutations docs (close #4622) (#4625)
@ -291,7 +293,6 @@ For example, see [here](https://hasura.io/docs/1.0/graphql/manual/api-reference/
- server: fix recreating action's permissions (close #4377)
- server: make the graceful shutdown logic customizable (graceful shutdown on the SIGTERM signal continues to be the default)
- docs: add reference docs for CLI (clsoe #4327) (#4408)
- cli: load assets from server when HASURA_GRAPHQL_CONSOLE_ASSETS_DIR is set (close #3382)
## `v1.2.0-beta.4`

View File

@ -61,7 +61,12 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
return ec.Validate()
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.EC.Spin("Applying migrations...")
if opts.dryRun && opts.SkipExecution {
return errors.New("both --skip-execution and --dry-run flags cannot be used together")
}
if !opts.dryRun {
opts.EC.Spin("Applying migrations...")
}
err := opts.Run()
opts.EC.Spinner.Stop()
if err != nil {
@ -78,7 +83,9 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
}
return errors.Wrap(err, "apply failed")
}
opts.EC.Logger.Info("migrations applied")
if !opts.dryRun {
opts.EC.Logger.Info("migrations applied")
}
return nil
},
}
@ -93,6 +100,7 @@ func newMigrateApplyCmd(ec *cli.ExecutionContext) *cobra.Command {
f.BoolVar(&opts.SkipExecution, "skip-execution", false, "skip executing the migration action, but mark them as applied")
f.StringVar(&opts.MigrationType, "type", "up", "type of migration (up, down) to be used with version flag")
f.BoolVar(&opts.dryRun, "dry-run", false, "print the names of migrations which are going to be applied")
return migrateApplyCmd
}
@ -106,6 +114,7 @@ type MigrateApplyOptions struct {
// version up to which migration chain has to be applied
GotoVersion string
SkipExecution bool
dryRun bool
}
func (o *MigrateApplyOptions) Run() error {
@ -119,6 +128,7 @@ func (o *MigrateApplyOptions) Run() error {
return err
}
migrateDrv.SkipExecution = o.SkipExecution
migrateDrv.DryRun = o.dryRun
return ExecuteMigration(migrationType, migrateDrv, step)
}

View File

@ -3,6 +3,7 @@ package commands
import (
"bytes"
"fmt"
"github.com/hasura/graphql-engine/cli/util"
"strconv"
"strings"
"text/tabwriter"
@ -10,7 +11,6 @@ import (
"github.com/hasura/graphql-engine/cli/migrate"
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/util"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"

View File

@ -3,11 +3,13 @@ package commands
import (
"bytes"
"fmt"
"os"
"text/tabwriter"
"github.com/hasura/graphql-engine/cli/util"
"github.com/hasura/graphql-engine/cli"
"github.com/hasura/graphql-engine/cli/migrate"
"github.com/hasura/graphql-engine/cli/util"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
@ -33,7 +35,7 @@ func newMigrateStatusCmd(ec *cli.ExecutionContext) *cobra.Command {
return err
}
buf := printStatus(status)
fmt.Println(buf.String())
fmt.Fprintf(os.Stdout, "%s", buf)
return nil
},
}

View File

@ -13,8 +13,11 @@ import (
"io"
"os"
"sync"
"text/tabwriter"
"time"
"github.com/hasura/graphql-engine/cli/util"
"github.com/hasura/graphql-engine/cli/metadata/types"
"github.com/hasura/graphql-engine/cli/migrate/database"
"github.com/hasura/graphql-engine/cli/migrate/source"
@ -99,6 +102,7 @@ type Migrate struct {
status *Status
SkipExecution bool
DryRun bool
}
// New returns a new Migrate instance from a source URL and a database URL.
@ -538,8 +542,11 @@ func (m *Migrate) Migrate(version uint64, direction string) error {
ret := make(chan interface{}, m.PrefetchMigrations)
go m.read(version, direction, ret)
return m.unlockErr(m.runMigrations(ret))
if m.DryRun {
return m.unlockErr(m.runDryRun(ret))
} else {
return m.unlockErr(m.runMigrations(ret))
}
}
func (m *Migrate) QueryWithVersion(version uint64, data io.ReadCloser, skipExecution bool) error {
@ -600,7 +607,11 @@ func (m *Migrate) Steps(n int64) error {
go m.readDown(-n, ret)
}
return m.unlockErr(m.runMigrations(ret))
if m.DryRun {
return m.unlockErr(m.runDryRun(ret))
} else {
return m.unlockErr(m.runMigrations(ret))
}
}
// Up looks at the currently active migration version
@ -632,7 +643,11 @@ func (m *Migrate) Up() error {
go m.readUp(-1, ret)
return m.unlockErr(m.runMigrations(ret))
if m.DryRun {
return m.unlockErr(m.runDryRun(ret))
} else {
return m.unlockErr(m.runMigrations(ret))
}
}
// Down looks at the currently active migration version
@ -663,7 +678,11 @@ func (m *Migrate) Down() error {
ret := make(chan interface{}, m.PrefetchMigrations)
go m.readDown(-1, ret)
return m.unlockErr(m.runMigrations(ret))
if m.DryRun {
return m.unlockErr(m.runDryRun(ret))
} else {
return m.unlockErr(m.runMigrations(ret))
}
}
func (m *Migrate) squashUp(version uint64, ret chan<- interface{}) {
@ -1188,6 +1207,32 @@ func (m *Migrate) runMigrations(ret <-chan interface{}) error {
return nil
}
func (m *Migrate) runDryRun(ret <-chan interface{}) error {
migrations := make([]*Migration, 0)
var lastInsertVersion int64
for r := range ret {
if m.stop() {
return nil
}
switch r.(type) {
case error:
return r.(error)
case *Migration:
migr := r.(*Migration)
if migr.Body != nil {
version := int64(migr.Version)
if version != lastInsertVersion {
migrations = append(migrations, migr)
lastInsertVersion = version
}
}
}
}
fmt.Fprintf(os.Stdout, "%s", printDryRunStatus(migrations))
return nil
}
func (m *Migrate) squashMigrations(retUp <-chan interface{}, retDown <-chan interface{}, dataUp chan<- interface{}, dataDown chan<- interface{}, versions chan<- int64) error {
var latestVersion int64
go func() {
@ -1582,8 +1627,11 @@ func (m *Migrate) GotoVersion(gotoVersion int64) error {
go m.readDownFromVersion(currVersion, gotoVersion, ret)
}
return m.unlockErr(m.runMigrations(ret))
if m.DryRun {
return m.unlockErr(m.runDryRun(ret))
} else {
return m.unlockErr(m.runMigrations(ret))
}
}
// readUpFromVersion reads up migrations from `from` limitted by `limit`. (is a modified version of readUp)
@ -1771,3 +1819,26 @@ func (m *Migrate) readDownFromVersion(from int64, to int64, ret chan<- interface
noOfAppliedMigrations++
}
}
func printDryRunStatus(migrations []*Migration) *bytes.Buffer {
out := new(tabwriter.Writer)
buf := &bytes.Buffer{}
out.Init(buf, 0, 8, 2, ' ', 0)
w := util.NewPrefixWriter(out)
w.Write(util.LEVEL_0, "VERSION\tTYPE\tNAME\n")
for _, migration := range migrations {
var direction string
if int64(migration.Version) == migration.TargetVersion {
direction = "up"
} else {
direction = "down"
}
w.Write(util.LEVEL_0, "%d\t%s\t%s\n",
migration.Version,
direction,
migration.Identifier,
)
}
out.Flush()
return buf
}