2021-04-05 17:00:21 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-06-21 16:54:08 +03:00
|
|
|
"github.com/Pallinder/go-randomdata"
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
|
2021-04-05 17:00:21 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
. "github.com/onsi/gomega/gexec"
|
|
|
|
)
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
var _ = Describe("hasura migrate create (config v3)", func() {
|
|
|
|
var projectDirectory string
|
2021-04-05 17:00:21 +03:00
|
|
|
var teardown func()
|
|
|
|
BeforeEach(func() {
|
2021-06-03 16:26:28 +03:00
|
|
|
projectDirectory = testutil.RandDirName()
|
|
|
|
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
|
2021-04-05 17:00:21 +03:00
|
|
|
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
2021-06-03 16:26:28 +03:00
|
|
|
Args: []string{"init", projectDirectory},
|
2021-04-05 17:00:21 +03:00
|
|
|
})
|
2021-06-03 16:26:28 +03:00
|
|
|
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
|
2021-04-05 17:00:21 +03:00
|
|
|
|
|
|
|
teardown = func() {
|
2021-06-03 16:26:28 +03:00
|
|
|
os.RemoveAll(projectDirectory)
|
2021-04-05 17:00:21 +03:00
|
|
|
teardownHGE()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
AfterEach(func() { teardown() })
|
|
|
|
|
|
|
|
It("can create migrations for default database", func() {
|
|
|
|
migrationName := "create_schema_testing"
|
|
|
|
session := testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{
|
|
|
|
"migrate",
|
|
|
|
"create",
|
|
|
|
migrationName,
|
|
|
|
"--up-sql",
|
|
|
|
"create schema \"testing\";",
|
|
|
|
"--down-sql",
|
|
|
|
"drop schema \"testing\" cascade;",
|
|
|
|
"--database-name", "default",
|
|
|
|
},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
|
|
|
wantKeywordList := []string{
|
|
|
|
"Migrations files created",
|
|
|
|
migrationName,
|
|
|
|
"version",
|
|
|
|
}
|
|
|
|
|
2021-07-28 09:44:13 +03:00
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-06-03 16:26:28 +03:00
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Err.Contents()).Should(ContainSubstring(keyword))
|
2021-06-03 16:26:28 +03:00
|
|
|
}
|
|
|
|
dirs, err := os.ReadDir(filepath.Join(projectDirectory, "migrations", "default"))
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
for _, d := range dirs {
|
|
|
|
Expect(d.Name()).Should(ContainSubstring(migrationName))
|
|
|
|
}
|
|
|
|
})
|
2021-06-21 16:54:08 +03:00
|
|
|
|
2022-01-12 19:15:52 +03:00
|
|
|
It("can create empty migrations when no data is provided", func() {
|
|
|
|
migrationName := "no_data_migration"
|
|
|
|
sourceName := randomdata.SillyName()
|
|
|
|
session := testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{
|
|
|
|
"migrate",
|
|
|
|
"create",
|
|
|
|
migrationName,
|
|
|
|
"--database-name", sourceName,
|
|
|
|
},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
|
|
|
wantKeywordList := []string{
|
|
|
|
"Migrations files created",
|
|
|
|
migrationName,
|
|
|
|
"version",
|
|
|
|
}
|
|
|
|
|
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
|
|
|
for _, keyword := range wantKeywordList {
|
|
|
|
Expect(session.Err.Contents()).Should(ContainSubstring(keyword))
|
|
|
|
}
|
|
|
|
dirs, err := os.ReadDir(filepath.Join(projectDirectory, "migrations", sourceName))
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
for _, d := range dirs {
|
|
|
|
Expect(d.Name()).Should(ContainSubstring(migrationName))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-21 16:54:08 +03:00
|
|
|
It("can create migrations for database that is not connected to server", func() {
|
|
|
|
migrationName := "create_schema_testing"
|
|
|
|
sourceName := randomdata.SillyName()
|
|
|
|
session := testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: []string{
|
|
|
|
"migrate",
|
|
|
|
"create",
|
|
|
|
migrationName,
|
|
|
|
"--up-sql",
|
|
|
|
"create schema \"testing\";",
|
|
|
|
"--down-sql",
|
|
|
|
"drop schema \"testing\" cascade;",
|
|
|
|
"--database-name", sourceName,
|
|
|
|
},
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
|
|
|
wantKeywordList := []string{
|
2023-01-09 11:01:49 +03:00
|
|
|
fmt.Sprintf("database '%s' is not connected to hasura", sourceName),
|
2021-06-21 16:54:08 +03:00
|
|
|
"Migrations files created",
|
|
|
|
migrationName,
|
|
|
|
"version",
|
|
|
|
}
|
|
|
|
|
2021-07-28 09:44:13 +03:00
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-06-21 16:54:08 +03:00
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Err.Contents()).Should(ContainSubstring(keyword))
|
2021-06-21 16:54:08 +03:00
|
|
|
}
|
|
|
|
dirs, err := os.ReadDir(filepath.Join(projectDirectory, "migrations", sourceName))
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
for _, d := range dirs {
|
|
|
|
Expect(d.Name()).Should(ContainSubstring(migrationName))
|
|
|
|
}
|
|
|
|
})
|
2021-06-03 16:26:28 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
var _ = Describe("hasura migrate create (config v2)", func() {
|
|
|
|
var projectDirectoryLatest, projectDirectoryV13 string
|
|
|
|
var session *Session
|
|
|
|
var teardown func()
|
|
|
|
BeforeEach(func() {
|
|
|
|
projectDirectoryLatest = testutil.RandDirName()
|
|
|
|
hgeEndPortLatest, teardownHGELatest := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
|
|
|
|
hgeEndpointLatest := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPortLatest)
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: []string{"init", projectDirectoryLatest, "--version", "2"},
|
|
|
|
})
|
|
|
|
editEndpointInConfig(filepath.Join(projectDirectoryLatest, defaultConfigFilename), hgeEndpointLatest)
|
|
|
|
|
|
|
|
projectDirectoryV13 = testutil.RandDirName()
|
|
|
|
hgeEndPortV13, teardownHGEV13 := testutil.StartHasura(GinkgoT(), "hasura/graphql-engine:v1.3.3")
|
|
|
|
hgeEndpointV13 := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPortV13)
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: []string{"init", projectDirectoryV13, "--version", "2"},
|
|
|
|
})
|
|
|
|
editEndpointInConfig(filepath.Join(projectDirectoryV13, defaultConfigFilename), hgeEndpointV13)
|
|
|
|
|
|
|
|
teardown = func() {
|
|
|
|
session.Kill()
|
|
|
|
os.RemoveAll(projectDirectoryLatest)
|
|
|
|
os.RemoveAll(projectDirectoryV13)
|
|
|
|
teardownHGEV13()
|
|
|
|
teardownHGELatest()
|
|
|
|
}
|
2021-04-05 17:00:21 +03:00
|
|
|
})
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
AfterEach(func() { teardown() })
|
|
|
|
|
|
|
|
It("can create migration files", func() {
|
|
|
|
test := func(projectDirectory string) {
|
|
|
|
migrationName := "create_schema_testing"
|
|
|
|
wantKeywordList := []string{
|
|
|
|
"Migrations files created",
|
|
|
|
migrationName,
|
|
|
|
"version",
|
|
|
|
}
|
2021-04-05 17:00:21 +03:00
|
|
|
session = testutil.Hasura(testutil.CmdOpts{
|
2021-06-03 16:26:28 +03:00
|
|
|
Args: []string{
|
|
|
|
"migrate",
|
|
|
|
"create",
|
|
|
|
migrationName,
|
|
|
|
"--up-sql", "create schema \"testing\";",
|
|
|
|
"--down-sql",
|
|
|
|
"drop schema \"testing\" cascade;",
|
|
|
|
},
|
|
|
|
WorkingDirectory: projectDirectory,
|
2021-04-05 17:00:21 +03:00
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
2021-06-03 16:26:28 +03:00
|
|
|
for _, keyword := range wantKeywordList {
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(session.Err.Contents()).Should(ContainSubstring(keyword))
|
2021-04-05 17:00:21 +03:00
|
|
|
}
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
dirs, err := os.ReadDir(filepath.Join(projectDirectory, "migrations"))
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
for _, d := range dirs {
|
|
|
|
Expect(d.Name()).Should(ContainSubstring(migrationName))
|
2021-04-05 17:00:21 +03:00
|
|
|
}
|
2021-06-03 16:26:28 +03:00
|
|
|
}
|
|
|
|
test(projectDirectoryLatest)
|
|
|
|
test(projectDirectoryV13)
|
2021-04-05 17:00:21 +03:00
|
|
|
})
|
|
|
|
})
|