2021-04-13 11:22:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-06-16 14:44:15 +03:00
|
|
|
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
|
2021-04-13 11:22:48 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
. "github.com/onsi/gomega/gexec"
|
|
|
|
)
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
var test = func(projectDirectory string, globalFlags []string) {
|
|
|
|
upSql := `CREATE TABLE "public"."table1" ("id" serial NOT NULL, PRIMARY KEY ("id") );`
|
|
|
|
downSql := `DROP TABLE "public"."table1";`
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: append([]string{"migrate", "create", "table1", "--up-sql", upSql, "--down-sql", downSql}, globalFlags...),
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: append([]string{"migrate", "apply"}, globalFlags...),
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
|
|
|
session := testutil.Hasura(testutil.CmdOpts{
|
|
|
|
Args: append([]string{"seed", "apply", "--file", "table_seed.sql"}, globalFlags...),
|
|
|
|
WorkingDirectory: projectDirectory,
|
|
|
|
})
|
|
|
|
Eventually(session, 60*60).Should(Exit(0))
|
|
|
|
Eventually(session.Wait().Err.Contents()).Should(ContainSubstring("Seeds planted"))
|
|
|
|
}
|
2021-04-13 11:22:48 +03:00
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
var _ = Describe("hasura seed apply", func() {
|
|
|
|
|
|
|
|
var projectDirectoryLatest, projectDirectoryConfigV2 string
|
2021-04-13 11:22:48 +03:00
|
|
|
var teardown func()
|
2021-06-03 16:26:28 +03:00
|
|
|
var hgeEndpoint string
|
2021-04-13 11:22:48 +03:00
|
|
|
BeforeEach(func() {
|
2021-06-03 16:26:28 +03:00
|
|
|
hgeEndPort, teardownHGE := testutil.StartHasura(GinkgoT(), testutil.HasuraDockerImage)
|
|
|
|
hgeEndpoint = fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
|
2021-04-13 11:22:48 +03:00
|
|
|
teardown = func() {
|
|
|
|
teardownHGE()
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
AfterEach(func() { teardown() })
|
2021-04-13 11:22:48 +03:00
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
It("can apply seeds in config v3", func() {
|
|
|
|
projectDirectoryLatest = testutil.RandDirName()
|
|
|
|
defer os.RemoveAll(projectDirectoryLatest)
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: []string{"init", projectDirectoryLatest},
|
|
|
|
})
|
|
|
|
editEndpointInConfig(filepath.Join(projectDirectoryLatest, defaultConfigFilename), hgeEndpoint)
|
2021-04-13 11:22:48 +03:00
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
err := os.MkdirAll(filepath.Join(filepath.Join(projectDirectoryLatest, "seeds"), "default"), 0755)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err, "error creating default directory in seeds")
|
|
|
|
}
|
|
|
|
file, err := os.Create(filepath.Join(projectDirectoryLatest, "seeds", "default", "table_seed.sql"))
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err, "not able to create table_seed.sql file")
|
|
|
|
}
|
2021-04-13 11:22:48 +03:00
|
|
|
data := `INSERT INTO public.table1 (id) VALUES (1);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (2);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (3);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (4);`
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
_, err = file.WriteString(data)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
test(projectDirectoryLatest, []string{"--database-name", "default"})
|
|
|
|
})
|
2021-05-14 22:09:01 +03:00
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
It("can apply seeds in config v2", func() {
|
|
|
|
projectDirectoryConfigV2 = testutil.RandDirName()
|
|
|
|
defer os.RemoveAll(projectDirectoryConfigV2)
|
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: []string{"init", projectDirectoryConfigV2, "--version", "2"},
|
2021-04-13 11:22:48 +03:00
|
|
|
})
|
2021-06-03 16:26:28 +03:00
|
|
|
editEndpointInConfig(filepath.Join(projectDirectoryConfigV2, defaultConfigFilename), hgeEndpoint)
|
|
|
|
|
|
|
|
err := os.MkdirAll(filepath.Join(filepath.Join(projectDirectoryConfigV2, "seeds")), 0755)
|
|
|
|
file, err := os.Create(filepath.Join(projectDirectoryConfigV2, "seeds", "table_seed.sql"))
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
|
|
|
|
data := `INSERT INTO public.table1 (id) VALUES (1);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (2);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (3);
|
|
|
|
INSERT INTO public.table1 (id) VALUES (4);`
|
|
|
|
|
|
|
|
_, err = file.WriteString(data)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
test(projectDirectoryConfigV2, nil)
|
2021-04-13 11:22:48 +03:00
|
|
|
})
|
|
|
|
})
|