graphql-engine/cli/commands/migrate_squash_test.go
Aravind K P 566b441b12 cli: remove config v1 specific logic from migrate squash
https://github.com/hasura/graphql-engine-mono/pull/2381

Co-authored-by: Kali Vara Purushotham Santhati <72007599+purush7@users.noreply.github.com>
GitOrigin-RevId: 42db5ea7ecd46838b5f2b266beca48e5af040bb9
2021-09-24 12:06:11 +00:00

152 lines
5.7 KiB
Go

package commands
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/hasura/graphql-engine/cli/v2/internal/testutil"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/onsi/gomega/gexec"
)
var _ = Describe("hasura migrate squash", func() {
test := func(projectDirectory string, globalFlags []string, sourceName string) {
toSquash := []string{
"1588172669699_create_table_public_table30",
"1588172669752_create_table_public_table31",
"1588172669806_create_table_public_table32",
"1588172669861_create_table_public_table33",
"1588172669920_create_table_public_table34",
"1588172669974_create_table_public_table35",
"1588172670028_create_table_public_table36",
"1588172670088_create_table_public_table37",
"1588172670146_create_table_public_table38",
"1588172670201_create_table_public_table39",
"1588172670256_create_table_public_table40",
"1588172670313_create_table_public_table41",
"1588172670367_create_table_public_table42",
"1588172670422_create_table_public_table43",
"1588172670478_create_table_public_table44",
"1588172670532_create_table_public_table45",
"1588172670588_create_table_public_table46",
"1588172670644_create_table_public_table47",
"1588172670697_create_table_public_table48",
"1588172670757_create_table_public_table49",
"1588172670820_create_table_public_table50",
}
session := testutil.Hasura(testutil.CmdOpts{
Args: append([]string{
"migrate",
"squash",
"--from",
// testdata/migrate-squash-test/migrations/1588172669699_create_table_public_table30
"1588172669699",
"--delete-source",
}, globalFlags...),
WorkingDirectory: projectDirectory,
})
wantKeywordList := []string{
"Squashing migrations from",
"Created",
"after squashing",
"till",
}
Eventually(session, timeout).Should(Exit(0))
for _, keyword := range wantKeywordList {
Expect(session.Err.Contents()).Should(ContainSubstring(keyword))
}
// verify squashed migrations are deleted
migrationsDirectory := filepath.Join(projectDirectory, "migrations", sourceName)
for _, dir := range toSquash {
Expect(filepath.Join(migrationsDirectory, dir)).ShouldNot(BeADirectory())
}
// verify squashed migrations are deleted in statestore
session = testutil.Hasura(testutil.CmdOpts{
Args: append([]string{"migrate", "status"}, globalFlags...),
WorkingDirectory: projectDirectory,
})
Eventually(session, timeout).Should(Exit(0))
for _, dir := range toSquash {
// get version from dir name "1588172670820_create_table_public_table50"
parts := strings.Split(dir, "_")
Expect(len(parts)).ToNot(BeZero())
version := parts[0]
Expect(session.Out.Contents()).ToNot(ContainSubstring(version))
}
// assert contents of squashed migration
files, err := ioutil.ReadDir(migrationsDirectory)
Expect(err).To(BeNil())
for _, f := range files {
if strings.Contains(f.Name(), "squashed") {
gotUpSQL, err := ioutil.ReadFile(filepath.Join(migrationsDirectory, f.Name(), "up.sql"))
Expect(err).To(BeNil())
gotDownSQL, err := ioutil.ReadFile(filepath.Join(migrationsDirectory, f.Name(), "down.sql"))
Expect(err).To(BeNil())
wantUpSQL, err := ioutil.ReadFile("testdata/migrate-squash-test/want.up.sql")
Expect(err).To(BeNil())
wantDownSQL, err := ioutil.ReadFile("testdata/migrate-squash-test/want.down.sql")
Expect(err).To(BeNil())
Expect(string(gotUpSQL)).To(Equal(string(wantUpSQL)))
Expect(string(gotDownSQL)).To(Equal(string(wantDownSQL)))
break
}
}
}
var projectDirectoryLatestConfigV3, projectDirectoryLatestConfigV2, projectDirectoryV13 string
var teardown func()
BeforeEach(func() {
projectDirectoryLatestConfigV3 = 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", projectDirectoryLatestConfigV3},
})
editEndpointInConfig(filepath.Join(projectDirectoryLatestConfigV3, defaultConfigFilename), hgeEndpointLatest)
copyMigrationsToProjectDirectory(projectDirectoryLatestConfigV3, "testdata/migrate-squash-test/migrations", "default")
projectDirectoryLatestConfigV2 = testutil.RandDirName()
testutil.RunCommandAndSucceed(testutil.CmdOpts{
Args: []string{"init", projectDirectoryLatestConfigV2, "--version", "2"},
})
editEndpointInConfig(filepath.Join(projectDirectoryLatestConfigV2, defaultConfigFilename), hgeEndpointLatest)
copyMigrationsToProjectDirectory(projectDirectoryLatestConfigV2, "testdata/migrate-squash-test/migrations")
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)
copyMigrationsToProjectDirectory(projectDirectoryV13, "testdata/migrate-squash-test/migrations")
teardown = func() {
os.RemoveAll(projectDirectoryLatestConfigV3)
os.RemoveAll(projectDirectoryLatestConfigV2)
os.RemoveAll(projectDirectoryV13)
teardownHGELatest()
teardownHGEV13()
}
})
AfterEach(func() { teardown() })
It("can squash the migrations in local project dir", func() {
Context("config v3", func() { test(projectDirectoryLatestConfigV3, []string{"--database-name", "default"}, "default") })
Context("config v2 (latest)", func() { test(projectDirectoryLatestConfigV2, nil, "") })
Context("config v2 v1.3.3", func() { test(projectDirectoryV13, nil, "") })
})
})