2021-04-13 11:22:48 +03:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2021-06-21 17:34:10 +03:00
|
|
|
"io/ioutil"
|
2021-04-13 11:22:48 +03:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2021-06-21 17:34:10 +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-13 11:22:48 +03:00
|
|
|
. "github.com/onsi/ginkgo"
|
|
|
|
. "github.com/onsi/gomega"
|
|
|
|
. "github.com/onsi/gomega/gexec"
|
|
|
|
)
|
|
|
|
|
2021-07-28 09:44:13 +03:00
|
|
|
var test = func(projectDirectory string, globalFlags []string, databaseKind string) {
|
|
|
|
var upSql, downSql string
|
|
|
|
if databaseKind == "mssql" {
|
|
|
|
upSql = `CREATE SCHEMA test` + "\n" + `CREATE TABLE "test"."table1" ("id" int NOT NULL, PRIMARY KEY ("id") );`
|
|
|
|
downSql = `DROP TABLE "test"."table1";` + "\n" + `DROP SCHEMA test`
|
|
|
|
} else {
|
|
|
|
upSql = `CREATE TABLE "public"."table1" ("id" serial NOT NULL, PRIMARY KEY ("id") );`
|
|
|
|
downSql = `DROP TABLE "public"."table1";`
|
|
|
|
}
|
|
|
|
|
2021-06-03 16:26:28 +03:00
|
|
|
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,
|
|
|
|
})
|
2021-07-28 09:44:13 +03:00
|
|
|
Eventually(session, timeout).Should(Exit(0))
|
|
|
|
Expect(session.Err.Contents()).Should(ContainSubstring("Seeds planted"))
|
2021-06-03 16:26:28 +03:00
|
|
|
}
|
2021-04-13 11:22:48 +03:00
|
|
|
|
2021-06-21 17:34:10 +03:00
|
|
|
var _ = Describe("seed apply (config v2)", func() {
|
|
|
|
var 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 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)
|
2021-10-13 17:38:07 +03:00
|
|
|
Expect(err).To(BeNil())
|
2021-06-03 16:26:28 +03:00
|
|
|
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())
|
2021-07-28 09:44:13 +03:00
|
|
|
test(projectDirectoryConfigV2, nil, "postgres")
|
2021-04-13 11:22:48 +03:00
|
|
|
})
|
|
|
|
})
|
2021-06-21 17:34:10 +03:00
|
|
|
|
|
|
|
var _ = Describe("seed apply (config v3)", func() {
|
|
|
|
// automatic state migration should not affect new config v3 projects
|
|
|
|
var projectDirectory string
|
|
|
|
var teardown func()
|
|
|
|
var pgSource = randomdata.SillyName()
|
|
|
|
var citusSource = randomdata.SillyName()
|
2021-07-28 09:44:13 +03:00
|
|
|
var mssqlSource = randomdata.SillyName()
|
2021-06-21 17:34:10 +03:00
|
|
|
BeforeEach(func() {
|
|
|
|
projectDirectory = testutil.RandDirName()
|
|
|
|
hgeEndPort, teardownHGE := testutil.StartHasuraWithMetadataDatabase(GinkgoT(), testutil.HasuraDockerImage)
|
|
|
|
hgeEndpoint := fmt.Sprintf("http://0.0.0.0:%s", hgeEndPort)
|
2021-07-28 09:44:13 +03:00
|
|
|
_, teardownPG := AddDatabaseToHasura(hgeEndpoint, pgSource, "postgres")
|
|
|
|
_, teardownCitus := AddDatabaseToHasura(hgeEndpoint, citusSource, "citus")
|
|
|
|
_, teardownMSSQL := AddDatabaseToHasura(hgeEndpoint, mssqlSource, "mssql")
|
2021-06-21 17:34:10 +03:00
|
|
|
testutil.RunCommandAndSucceed(testutil.CmdOpts{
|
|
|
|
Args: []string{"init", projectDirectory},
|
|
|
|
})
|
|
|
|
editEndpointInConfig(filepath.Join(projectDirectory, defaultConfigFilename), hgeEndpoint)
|
|
|
|
teardown = func() {
|
|
|
|
os.RemoveAll(projectDirectory)
|
|
|
|
teardownHGE()
|
|
|
|
teardownPG()
|
|
|
|
teardownCitus()
|
2021-07-28 09:44:13 +03:00
|
|
|
teardownMSSQL()
|
2021-06-21 17:34:10 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
AfterEach(func() { teardown() })
|
|
|
|
|
|
|
|
It("can apply seeds in config v3", func() {
|
|
|
|
err := os.MkdirAll(filepath.Join(filepath.Join(projectDirectory, "seeds", pgSource)), 0755)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
data := []byte(`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-07-28 09:44:13 +03:00
|
|
|
mssqlData := []byte(`INSERT INTO "test"."table1" (id) VALUES (1);
|
|
|
|
INSERT INTO "test"."table1" (id) VALUES (2);
|
|
|
|
INSERT INTO "test"."table1" (id) VALUES (3);
|
|
|
|
INSERT INTO "test"."table1" (id) VALUES (4);`)
|
|
|
|
|
2021-06-21 17:34:10 +03:00
|
|
|
err = ioutil.WriteFile(filepath.Join(projectDirectory, "seeds", pgSource, "table_seed.sql"), data, 0655)
|
|
|
|
Expect(err).To(BeNil())
|
2021-07-28 09:44:13 +03:00
|
|
|
test(projectDirectory, []string{"--database-name", pgSource}, "postgres")
|
2021-06-21 17:34:10 +03:00
|
|
|
|
|
|
|
err = os.MkdirAll(filepath.Join(filepath.Join(projectDirectory, "seeds", citusSource)), 0755)
|
2021-07-28 09:44:13 +03:00
|
|
|
Expect(err).To(BeNil())
|
2021-06-21 17:34:10 +03:00
|
|
|
err = ioutil.WriteFile(filepath.Join(projectDirectory, "seeds", citusSource, "table_seed.sql"), data, 0655)
|
|
|
|
Expect(err).To(BeNil())
|
2021-07-28 09:44:13 +03:00
|
|
|
test(projectDirectory, []string{"--database-name", citusSource}, "citus")
|
2021-06-21 17:34:10 +03:00
|
|
|
|
2021-07-28 09:44:13 +03:00
|
|
|
err = os.MkdirAll(filepath.Join(filepath.Join(projectDirectory, "seeds", mssqlSource)), 0755)
|
|
|
|
Expect(err).To(BeNil())
|
|
|
|
err = ioutil.WriteFile(filepath.Join(projectDirectory, "seeds", mssqlSource, "table_seed.sql"), mssqlData, 0655)
|
2021-06-21 17:34:10 +03:00
|
|
|
Expect(err).To(BeNil())
|
2021-07-28 09:44:13 +03:00
|
|
|
test(projectDirectory, []string{"--database-name", mssqlSource}, "mssql")
|
2021-06-21 17:34:10 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
})
|