2021-05-24 05:33:45 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os/exec"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
|
2021-05-24 05:33:45 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
. "github.com/onsi/gomega/gbytes"
|
|
|
|
. "github.com/onsi/gomega/gexec"
|
|
|
|
)
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
var _ = Describe("hasura migrate delete", func() {
|
2021-05-24 05:33:45 +03:00
|
|
|
var teardown func()
|
|
|
|
var hgeEndpoint string
|
|
|
|
BeforeEach(func() {
|
2021-06-03 16:26:28 +03:00
|
|
|
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
|
2021-05-24 05:33:45 +03:00
|
|
|
hgeEndpoint = fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
|
|
|
|
|
|
|
|
teardown = func() {
|
|
|
|
teardownHGE()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
AfterEach(func() { teardown() })
|
2021-05-24 05:33:45 +03:00
|
|
|
|
|
|
|
Context("migrate delete --all", 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)
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
2021-05-24 05:33:45 +03:00
|
|
|
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{
|
2021-08-31 12:56:31 +03:00
|
|
|
"migrations applied",
|
2021-05-24 05:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say(keyword))
|
2021-05-24 05:33:45 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:58 +03:00
|
|
|
args := []string{"migrate", "delete", "--all", "--database-name", "default", "--force"}
|
|
|
|
cmd := exec.Command(testutil.CLIBinaryPath, args...)
|
2021-05-24 05:33:45 +03:00
|
|
|
cmd.Dir = projectDirectory
|
|
|
|
session, err := Start(
|
|
|
|
cmd,
|
|
|
|
NewPrefixedWriter(testutil.DebugOutPrefix, GinkgoWriter),
|
|
|
|
NewPrefixedWriter(testutil.DebugErrPrefix, GinkgoWriter),
|
|
|
|
)
|
|
|
|
Expect(err).To(BeNil())
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
|
2021-05-24 05:33:45 +03:00
|
|
|
|
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{"migrate", "status", "--database-name", "default"},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).ShouldNot(Say(version))
|
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-05-24 05:33:45 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-06-22 14:15:22 +03:00
|
|
|
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{
|
2021-08-31 12:56:31 +03:00
|
|
|
"migrations applied",
|
2021-06-22 14:15:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say(keyword))
|
2021-06-22 14:15:22 +03:00
|
|
|
}
|
|
|
|
|
2021-09-07 16:33:58 +03:00
|
|
|
args := []string{"migrate", "delete", "--all", "--server", "--database-name", "default", "--force"}
|
|
|
|
cmd := exec.Command(testutil.CLIBinaryPath, args...)
|
2021-06-22 14:15:22 +03:00
|
|
|
cmd.Dir = projectDirectory
|
|
|
|
session, err := Start(
|
|
|
|
cmd,
|
|
|
|
NewPrefixedWriter(testutil.DebugOutPrefix, GinkgoWriter),
|
|
|
|
NewPrefixedWriter(testutil.DebugErrPrefix, GinkgoWriter),
|
|
|
|
)
|
|
|
|
Expect(err).To(BeNil())
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
|
2021-06-22 14:15:22 +03:00
|
|
|
|
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{"migrate", "status", "--database-name", "default"},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Eventually(session.Out).Should(Say(version))
|
|
|
|
Eventually(session.Out).Should(Say("Present"))
|
|
|
|
Eventually(session.Out).Should(Say("Not Present"))
|
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-06-22 14:15:22 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2021-05-24 05:33:45 +03:00
|
|
|
Context("migrate delete --version <version>", func() {
|
|
|
|
It("should delete the migrations on server and on source ", func() {
|
|
|
|
dirName := testutil.RandDirName()
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: []string{"init", dirName},
|
|
|
|
})
|
|
|
|
|
|
|
|
editEndpointInConfig(filepath.Join(dirName, defaultConfigFilename), hgeEndpoint)
|
2021-06-03 16:26:28 +03:00
|
|
|
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
2021-05-24 05:33:45 +03:00
|
|
|
Args: []string{"migrate", "create", "schema_creation", "--up-sql", "create schema \"testing\";", "--down-sql", "drop schema \"testing\" cascade;", "--database-name", "default"},
|
|
|
|
WorkingDirectory: dirName,
|
|
|
|
})
|
|
|
|
|
|
|
|
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: dirName,
|
|
|
|
})
|
|
|
|
wantKeywordList := []string{
|
2021-08-31 12:56:31 +03:00
|
|
|
"migrations applied",
|
2021-05-24 05:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say(keyword))
|
2021-05-24 05:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{"migrate", "delete", "--version", version, "--database-name", "default"},
|
|
|
|
WorkingDirectory: dirName,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
|
2021-05-24 05:33:45 +03:00
|
|
|
|
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{"migrate", "status", "--database-name", "default"},
|
|
|
|
WorkingDirectory: dirName,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).ShouldNot(Say(version))
|
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-05-24 05:33:45 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("migrate delete --version <version> (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)
|
2021-06-03 16:26:28 +03:00
|
|
|
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
2021-05-24 05:33:45 +03:00
|
|
|
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{
|
2021-08-31 12:56:31 +03:00
|
|
|
"migrations applied",
|
2021-05-24 05:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say(keyword))
|
2021-05-24 05:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{"migrate", "delete", "--version", version},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
|
2021-05-24 05:33:45 +03:00
|
|
|
|
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{"migrate", "status"},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).ShouldNot(Say(version))
|
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-05-24 05:33:45 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
Context("migrate delete --all (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)
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
session := testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
2021-05-24 05:33:45 +03:00
|
|
|
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{
|
2021-08-31 12:56:31 +03:00
|
|
|
"migrations applied",
|
2021-05-24 05:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say(keyword))
|
2021-05-24 05:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
args := []string{"migrate", "delete", "--all", "--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())
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
|
2021-05-24 05:33:45 +03:00
|
|
|
|
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{"migrate", "status"},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).ShouldNot(Say(version))
|
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-05-24 05:33:45 +03:00
|
|
|
})
|
|
|
|
})
|
2021-06-22 14:15:22 +03:00
|
|
|
|
|
|
|
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{
|
2021-08-31 12:56:31 +03:00
|
|
|
"migrations applied",
|
2021-06-22 14:15:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say(keyword))
|
2021-06-22 14:15:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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())
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Err).Should(Say("Deleted migrations"))
|
2021-06-22 14:15:22 +03:00
|
|
|
|
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{"migrate", "status"},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Wait(timeout).Out).Should(Say(version))
|
|
|
|
Expect(session.Wait(timeout).Out).Should(Say("Present"))
|
|
|
|
Expect(session.Wait(timeout).Out).Should(Say("Not Present"))
|
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-06-22 14:15:22 +03:00
|
|
|
})
|
|
|
|
})
|
2021-05-24 05:33:45 +03:00
|
|
|
})
|