mirror of
https://github.com/hasura/graphql-engine.git
synced 2024-11-10 10:29:12 +03:00
cli: add server
flag to migrate delete
and modify the docs for resetting migrations
https://github.com/hasura/graphql-engine-mono/pull/1540 Co-authored-by: Rikin Kachhia <54616969+rikinsk@users.noreply.github.com> Co-authored-by: Aravind K P <8335904+scriptonist@users.noreply.github.com> GitOrigin-RevId: 8aceb35c583d20b7dc7b16f257e20c83171008a2
This commit is contained in:
parent
e640000a09
commit
59679babfa
@ -13,6 +13,7 @@
|
||||
- cli: `metadata diff` will now only show the differences in metadata. old behaviour is avialble behind a flag (`--type unified-common`) (#5487)
|
||||
- cli: add citus support
|
||||
- cli: allow `--skip-execution` to be used with `up` and `down` flags in `migrate apply`
|
||||
- cli: allow deleting migration state from server using `--server` flag in `migrate delete` command
|
||||
|
||||
## v2.0.0-beta.2
|
||||
|
||||
|
@ -9,7 +9,6 @@ import (
|
||||
"github.com/hasura/graphql-engine/cli/v2/migrate"
|
||||
mig "github.com/hasura/graphql-engine/cli/v2/migrate/cmd"
|
||||
"github.com/hasura/graphql-engine/cli/v2/util"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
@ -22,10 +21,10 @@ func newMigrateDeleteCmd(ec *cli.ExecutionContext) *cobra.Command {
|
||||
Short: "(PREVIEW) clear migrations from local project and server",
|
||||
Example: `
|
||||
# Usage to delete a version:
|
||||
hasura migrate delete --version <version_delete> --database-name default
|
||||
hasura migrate delete --version <version_delete> --database-name <database-name>
|
||||
|
||||
# Usage to delete all versions
|
||||
hasura migrate delete --all`,
|
||||
hasura migrate delete --all --database-name <database-name>`,
|
||||
SilenceUsage: true,
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
ec.Logger.Warn("[PREVIEW] this command is in preview. usage may change in future\n")
|
||||
@ -38,6 +37,11 @@ func newMigrateDeleteCmd(ec *cli.ExecutionContext) *cobra.Command {
|
||||
if cmd.Flags().Changed("all") && cmd.Flags().Changed("version") {
|
||||
return fmt.Errorf("only one of [--all , --version] should be set")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
// exit if user inputs n for clearing migrations
|
||||
if cmd.Flags().Changed("all") && !opts.force {
|
||||
confirmation, err := util.GetYesNoPrompt("clear all migrations of database and it's history on the server?")
|
||||
if err != nil {
|
||||
@ -47,9 +51,7 @@ func newMigrateDeleteCmd(ec *cli.ExecutionContext) *cobra.Command {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
},
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
opts.Source = ec.Source
|
||||
if ec.Config.Version >= cli.V3 {
|
||||
var err error
|
||||
@ -75,15 +77,17 @@ func newMigrateDeleteCmd(ec *cli.ExecutionContext) *cobra.Command {
|
||||
f.Uint64Var(&opts.version, "version", 0, "deletes the specified version in migrations")
|
||||
f.BoolVar(&opts.all, "all", false, "clears all migrations for selected database")
|
||||
f.BoolVar(&opts.force, "force", false, "when set executes operation without any confirmation")
|
||||
f.BoolVar(&opts.onlyServer, "server", false, "to reset migrations only on server")
|
||||
|
||||
return migrateDeleteCmd
|
||||
}
|
||||
|
||||
type MigrateDeleteOptions struct {
|
||||
EC *cli.ExecutionContext
|
||||
version uint64
|
||||
all bool
|
||||
force bool
|
||||
EC *cli.ExecutionContext
|
||||
version uint64
|
||||
all bool
|
||||
force bool
|
||||
onlyServer bool
|
||||
|
||||
Source cli.Source
|
||||
}
|
||||
@ -102,18 +106,17 @@ func (o *MigrateDeleteOptions) Run() error {
|
||||
return fmt.Errorf("error while retrieving migration status %w", err)
|
||||
}
|
||||
|
||||
// sourceVersions migration versions in source to be deleted similarly with serverVersions
|
||||
var sourceVersions, serverVersions []uint64
|
||||
|
||||
if !o.all {
|
||||
// if o.version isn't present on source and on server return error version isn't present.
|
||||
if _, ok := status.Migrations[o.version]; !ok {
|
||||
return fmt.Errorf("version %v not found", o.version)
|
||||
}
|
||||
err := DeleteVersions(o.EC, []uint64{o.version}, o.Source)
|
||||
if err != nil {
|
||||
o.EC.Logger.Warn(errors.Wrap(err, "error in deletion of migration in source"))
|
||||
}
|
||||
versions := []uint64{o.version}
|
||||
err = migrateDrv.RemoveVersions(versions)
|
||||
sourceVersions = []uint64{o.version}
|
||||
serverVersions = []uint64{o.version}
|
||||
} else if o.all {
|
||||
var sourceVersions, serverVersions []uint64
|
||||
for k, v := range status.Migrations {
|
||||
if v.IsApplied {
|
||||
serverVersions = append(serverVersions, k)
|
||||
@ -122,17 +125,22 @@ func (o *MigrateDeleteOptions) Run() error {
|
||||
sourceVersions = append(sourceVersions, k)
|
||||
}
|
||||
}
|
||||
// delete version history on server
|
||||
err = migrateDrv.RemoveVersions(serverVersions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error removing migration from server: %w", err)
|
||||
}
|
||||
// delete migrations history in project
|
||||
}
|
||||
|
||||
// resets the migrations on server
|
||||
err = migrateDrv.RemoveVersions(serverVersions)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error removing migration from server: %w", err)
|
||||
}
|
||||
|
||||
// removes the migrations on source
|
||||
if !o.onlyServer {
|
||||
err = DeleteVersions(o.EC, sourceVersions, o.Source)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error removing migration from project: %w", err)
|
||||
return fmt.Errorf("error removing migrations from project: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
o.EC.Logger.Infof("Deleted migrations")
|
||||
return nil
|
||||
}
|
||||
|
@ -84,6 +84,65 @@ var _ = Describe("hasura migrate delete", func() {
|
||||
})
|
||||
})
|
||||
|
||||
Context("migrate delete --all --server", func() {
|
||||
It("should delete the migrations on server and on source ", func() {
|
||||
projectDirectory := testutil.RandDirName()
|
||||
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
||||
Args: []string{"init", projectDirectory},
|
||||
})
|
||||
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
|
||||
|
||||
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
||||
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;", "--database-name", "default"},
|
||||
WorkingDirectory: projectDirectory,
|
||||
})
|
||||
|
||||
str := string(session.Err.Contents())
|
||||
i := strings.Index(str, "\"version\"")
|
||||
version := str[i+10 : i+23]
|
||||
session = testutil.Hasura(testutil.CmdOpts{
|
||||
Args: []string{"migrate", "apply", "--database-name", "default"},
|
||||
WorkingDirectory: projectDirectory,
|
||||
})
|
||||
wantKeywordList := []string{
|
||||
".*Applying migrations...*.",
|
||||
".*migrations*.",
|
||||
".*applied*.",
|
||||
}
|
||||
|
||||
for _, keyword := range wantKeywordList {
|
||||
Eventually(session.Err, 60*40).Should(Say(keyword))
|
||||
}
|
||||
|
||||
session = testutil.Hasura(testutil.CmdOpts{
|
||||
Args: []string{"migrate", "delete", "--all", "--server"},
|
||||
WorkingDirectory: projectDirectory,
|
||||
})
|
||||
|
||||
Eventually(session.Err, 60*40).Should(Say("--database-name flag is required"))
|
||||
|
||||
args := strings.Join([]string{"yes", "|", testutil.CLIBinaryPath, "migrate", "delete", "--all", "--server", "--database-name", "default"}, " ")
|
||||
cmd := exec.Command("bash", "-c", args)
|
||||
cmd.Dir = projectDirectory
|
||||
session, err := Start(
|
||||
cmd,
|
||||
NewPrefixedWriter(testutil.DebugOutPrefix, GinkgoWriter),
|
||||
NewPrefixedWriter(testutil.DebugErrPrefix, GinkgoWriter),
|
||||
)
|
||||
Expect(err).To(BeNil())
|
||||
Eventually(session.Err, 60*40).Should(Say("Deleted migrations"))
|
||||
|
||||
session = testutil.Hasura(testutil.CmdOpts{
|
||||
Args: []string{"migrate", "status", "--database-name", "default"},
|
||||
WorkingDirectory: projectDirectory,
|
||||
})
|
||||
Eventually(session.Out, 60*40).Should(Say(version))
|
||||
Eventually(session.Out, 60*40).Should(Say("Present"))
|
||||
Eventually(session.Out, 60*40).Should(Say("Not Present"))
|
||||
Eventually(session, 60*50).Should(Exit(0))
|
||||
})
|
||||
})
|
||||
|
||||
Context("migrate delete --version <version>", func() {
|
||||
It("should delete the migrations on server and on source ", func() {
|
||||
dirName := testutil.RandDirName()
|
||||
@ -223,4 +282,56 @@ var _ = Describe("hasura migrate delete", func() {
|
||||
Eventually(session, 60*50).Should(Exit(0))
|
||||
})
|
||||
})
|
||||
|
||||
Context("migrate delete --all --server(config v2)", func() {
|
||||
It("should delete the migrations on server and on source ", func() {
|
||||
projectDirectory := testutil.RandDirName()
|
||||
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
||||
Args: []string{"init", projectDirectory, "--version", "2"},
|
||||
})
|
||||
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
|
||||
|
||||
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
||||
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;"},
|
||||
WorkingDirectory: projectDirectory,
|
||||
})
|
||||
|
||||
str := string(session.Err.Contents())
|
||||
i := strings.Index(str, "\"version\"")
|
||||
version := str[i+10 : i+23]
|
||||
session = testutil.Hasura(testutil.CmdOpts{
|
||||
Args: []string{"migrate", "apply"},
|
||||
WorkingDirectory: projectDirectory,
|
||||
})
|
||||
wantKeywordList := []string{
|
||||
".*Applying migrations...*.",
|
||||
".*migrations*.",
|
||||
".*applied*.",
|
||||
}
|
||||
|
||||
for _, keyword := range wantKeywordList {
|
||||
Eventually(session.Err, 60*40).Should(Say(keyword))
|
||||
}
|
||||
|
||||
args := []string{"migrate", "delete", "--all", "--server", "--force"}
|
||||
cmd := exec.Command(testutil.CLIBinaryPath, args...)
|
||||
cmd.Dir = projectDirectory
|
||||
session, err := Start(
|
||||
cmd,
|
||||
NewPrefixedWriter(testutil.DebugOutPrefix, GinkgoWriter),
|
||||
NewPrefixedWriter(testutil.DebugErrPrefix, GinkgoWriter),
|
||||
)
|
||||
Expect(err).To(BeNil())
|
||||
Eventually(session.Err, 60*40).Should(Say("Deleted migrations"))
|
||||
|
||||
session = testutil.Hasura(testutil.CmdOpts{
|
||||
Args: []string{"migrate", "status"},
|
||||
WorkingDirectory: projectDirectory,
|
||||
})
|
||||
Eventually(session.Out, 60*40).Should(Say(version))
|
||||
Eventually(session.Out, 60*40).Should(Say("Present"))
|
||||
Eventually(session.Out, 60*40).Should(Say("Not Present"))
|
||||
Eventually(session, 60*50).Should(Exit(0))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
@ -34,7 +34,21 @@ You can use the following command:
|
||||
Step 2: Reset the migration history on the server
|
||||
-------------------------------------------------
|
||||
|
||||
`coming soon`
|
||||
To reset the migrations status on the server only, use:
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# reset migrations on server only
|
||||
|
||||
hasura migrate delete --all --server --database-name <database-name>
|
||||
|
||||
To reset the migrations status on server and clean up the local project migration files
|
||||
|
||||
.. code-block:: bash
|
||||
|
||||
# reset migrations on server and on local project, use:
|
||||
|
||||
hasura migrate delete --all --database-name <database-name>
|
||||
|
||||
Step 3: Pull the schema and metadata from the server
|
||||
----------------------------------------------------
|
||||
|
Loading…
Reference in New Issue
Block a user