这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (ec *ExecutionContext) Validate() error {
// set names of files and directories
ec.MigrationDir = filepath.Join(ec.ExecutionDirectory, "migrations")
ec.ConfigFile = filepath.Join(ec.ExecutionDirectory, "config.yaml")
ec.MetadataFile = filepath.Join(ec.ExecutionDirectory, "metadata.yaml")
ec.MetadataFile = filepath.Join(ec.MigrationDir, "metadata.yaml")

// read config and parse the values into Config
err = ec.readConfig()
Expand Down
12 changes: 10 additions & 2 deletions cli/commands/console.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func (o *consoleOptions) run() error {
r,
}

router.setRoutes(o.EC.Config.ParsedEndpoint, o.EC.Config.AccessKey, o.EC.MigrationDir, o.EC.Logger)
router.setRoutes(o.EC.Config.ParsedEndpoint, o.EC.Config.AccessKey, o.EC.MigrationDir, o.EC.MetadataFile, o.EC.Logger)

if o.EC.Version == nil {
return errors.New("cannot validate version, object is nil")
Expand Down Expand Up @@ -152,7 +152,7 @@ type consoleRouter struct {
*gin.Engine
}

func (router *consoleRouter) setRoutes(nurl *url.URL, accessKey, migrationDir string, logger *logrus.Logger) {
func (router *consoleRouter) setRoutes(nurl *url.URL, accessKey, migrationDir, metadataFile string, logger *logrus.Logger) {
apis := router.Group("/apis")
{
apis.Use(setLogger(logger))
Expand All @@ -170,6 +170,7 @@ func (router *consoleRouter) setRoutes(nurl *url.URL, accessKey, migrationDir st
// Migrate api endpoints and middleware
metadataAPIs := apis.Group("/metadata")
{
metadataAPIs.Use(setMetadataFile(metadataFile))
metadataAPIs.Any("", api.MetadataAPI)
}
}
Expand All @@ -192,6 +193,13 @@ func setFilePath(dir string) gin.HandlerFunc {
}
}

func setMetadataFile(file string) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("metadataFile", file)
c.Next()
}
}

func setLogger(logger *logrus.Logger) gin.HandlerFunc {
return func(c *gin.Context) {
c.Set("logger", logger)
Expand Down
7 changes: 3 additions & 4 deletions cli/commands/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package commands
import (
"encoding/json"
"io/ioutil"
"path/filepath"

"github.com/ghodss/yaml"
"github.com/hasura/graphql-engine/cli"
Expand Down Expand Up @@ -39,7 +38,7 @@ func NewMetadataCmd(ec *cli.ExecutionContext) *cobra.Command {
return metadataCmd
}

func executeMetadata(cmd string, t *migrate.Migrate, metadata string) error {
func executeMetadata(cmd string, t *migrate.Migrate, metadataPath string) error {
switch cmd {
case "export":
metaData, err := t.ExportMetadata()
Expand All @@ -57,7 +56,7 @@ func executeMetadata(cmd string, t *migrate.Migrate, metadata string) error {
return err
}

err = ioutil.WriteFile(filepath.Join(metadata, "metadata.yaml"), data, 0644)
err = ioutil.WriteFile(metadataPath, data, 0644)
if err != nil {
return errors.Wrap(err, "cannot save metadata")
}
Expand All @@ -67,7 +66,7 @@ func executeMetadata(cmd string, t *migrate.Migrate, metadata string) error {
return errors.Wrap(err, "Cannot reset Metadata")
}
case "apply":
data, err := ioutil.ReadFile(filepath.Join(metadata, "metadata.yaml"))
data, err := ioutil.ReadFile(metadataPath)
if err != nil {
return errors.Wrap(err, "cannot read metadata file")
}
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/metadata_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func (o *metadataApplyOptions) run() error {
if err != nil {
return err
}
return executeMetadata(o.actionType, migrateDrv, o.EC.ExecutionDirectory)
return executeMetadata(o.actionType, migrateDrv, o.EC.MetadataFile)
}
8 changes: 4 additions & 4 deletions cli/commands/metadata_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/sirupsen/logrus/hooks/test"
)

func testMetadataApply(t *testing.T, executionDir string, endpoint *url.URL) {
func testMetadataApply(t *testing.T, metadataFile string, endpoint *url.URL) {
logger, _ := test.NewNullLogger()
opts := &metadataApplyOptions{
EC: &cli.ExecutionContext{
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
ExecutionDirectory: executionDir,
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
MetadataFile: metadataFile,
Config: &cli.HasuraGraphQLConfig{
Endpoint: endpoint.String(),
AccessKey: "",
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/metadata_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func (o *metadataExportOptions) run() error {
if err != nil {
return err
}
return executeMetadata(o.actionType, migrateDrv, o.EC.ExecutionDirectory)
return executeMetadata(o.actionType, migrateDrv, o.EC.MetadataFile)
}
8 changes: 4 additions & 4 deletions cli/commands/metadata_export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/sirupsen/logrus/hooks/test"
)

func testMetadataExport(t *testing.T, executionDir string, endpoint *url.URL) {
func testMetadataExport(t *testing.T, metadataFile string, endpoint *url.URL) {
logger, _ := test.NewNullLogger()
opts := &metadataExportOptions{
EC: &cli.ExecutionContext{
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
ExecutionDirectory: executionDir,
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
MetadataFile: metadataFile,
Config: &cli.HasuraGraphQLConfig{
Endpoint: endpoint.String(),
AccessKey: "",
Expand Down
2 changes: 1 addition & 1 deletion cli/commands/metadata_reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (o *metadataResetOptions) run() error {
if err != nil {
return err
}
err = executeMetadata(o.actionType, migrateDrv, o.EC.ExecutionDirectory)
err = executeMetadata(o.actionType, migrateDrv, o.EC.MetadataFile)
if err != nil {
return errors.Wrap(err, "Cannot reset metadata")
}
Expand Down
8 changes: 4 additions & 4 deletions cli/commands/metadata_reset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ import (
"github.com/sirupsen/logrus/hooks/test"
)

func testMetadataReset(t *testing.T, executionDir string, endpoint *url.URL) {
func testMetadataReset(t *testing.T, metadataFile string, endpoint *url.URL) {
logger, _ := test.NewNullLogger()
opts := &metadataResetOptions{
EC: &cli.ExecutionContext{
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
ExecutionDirectory: executionDir,
Logger: logger,
Spinner: spinner.New(spinner.CharSets[7], 100*time.Millisecond),
MetadataFile: metadataFile,
Config: &cli.HasuraGraphQLConfig{
Endpoint: endpoint.String(),
AccessKey: "",
Expand Down
40 changes: 14 additions & 26 deletions cli/commands/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,7 @@ func testMigrateWithDocker(t *testing.T, migrationsDir, executionDir string) {
}
defer os.RemoveAll(migrationsDir)

executionDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(executionDir)

testMigrate(t, endpointURL, migrationsDir, executionDir)
testMigrate(t, endpointURL, migrationsDir)
})
})
}
Expand All @@ -116,17 +110,11 @@ func TestMigrateCmd(t *testing.T) {
}
defer os.RemoveAll(migrationsDir)

// Create Execution Dir
executionDir, err := ioutil.TempDir("", "")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(executionDir)

testMigrate(t, endpointURL, migrationsDir, executionDir)
testMigrate(t, endpointURL, migrationsDir)
}

func testMigrate(t *testing.T, endpoint *url.URL, migrationsDir, executionDir string) {
func testMigrate(t *testing.T, endpoint *url.URL, migrationsDir string) {
metadataFile := filepath.Join(migrationsDir, "metadata.yaml")
// Create 1_create_table_test.up.sql which creates table test
mustWriteFile(t, migrationsDir, "1_create_table_test.up.sql", `CREATE TABLE "test"("id" serial NOT NULL, PRIMARY KEY ("id") )`)
// Create 1_create_table_test.down.sql which creates table test
Expand Down Expand Up @@ -212,16 +200,16 @@ func testMigrate(t *testing.T, endpoint *url.URL, migrationsDir, executionDir st
// Apply both 1 and 2
testMigrateApply(t, endpoint, migrationsDir, "", "", "", "")

testMetadataExport(t, executionDir, endpoint)
compareMetadata(t, executionDir, testMetadata["metadata"])
testMetadataExport(t, metadataFile, endpoint)
compareMetadata(t, metadataFile, testMetadata["metadata"])

testMetadataApply(t, executionDir, endpoint)
testMetadataExport(t, executionDir, endpoint)
compareMetadata(t, executionDir, testMetadata["metadata"])
testMetadataApply(t, metadataFile, endpoint)
testMetadataExport(t, metadataFile, endpoint)
compareMetadata(t, metadataFile, testMetadata["metadata"])

testMetadataReset(t, executionDir, endpoint)
testMetadataExport(t, executionDir, endpoint)
compareMetadata(t, executionDir, testMetadata["empty-metadata"])
testMetadataReset(t, metadataFile, endpoint)
testMetadataExport(t, metadataFile, endpoint)
compareMetadata(t, metadataFile, testMetadata["empty-metadata"])
}

func mustWriteFile(t testing.TB, dir, file string, body string) {
Expand All @@ -230,8 +218,8 @@ func mustWriteFile(t testing.TB, dir, file string, body string) {
}
}

func compareMetadata(t testing.TB, executionDir string, actualData []byte) {
data, err := ioutil.ReadFile(filepath.Join(executionDir, "metadata.yaml"))
func compareMetadata(t testing.TB, metadataFile string, actualData []byte) {
data, err := ioutil.ReadFile(metadataFile)
if err != nil {
t.Fatalf("error reading metadata %s", err)
}
Expand Down
11 changes: 8 additions & 3 deletions cli/migrate/api/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"io/ioutil"
"net/http"
"net/url"
"path/filepath"
"strings"

"github.com/ghodss/yaml"
Expand Down Expand Up @@ -39,6 +38,12 @@ func MetadataAPI(c *gin.Context) {
}
logger := loggerPtr.(*logrus.Logger)

metadataFilePtr, ok := c.Get("metadataFile")
if !ok {
return
}
metadataFile := metadataFilePtr.(string)

// Create new migrate
t, err := migrate.New(sourceURL.String(), databaseURL.String(), false, logger)
if err != nil {
Expand Down Expand Up @@ -84,7 +89,7 @@ func MetadataAPI(c *gin.Context) {
return
}

err = ioutil.WriteFile(filepath.Join(sourceURL.Path, "../metadata.yaml"), data, 0644)
err = ioutil.WriteFile(metadataFile, data, 0644)
if err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
return
Expand Down Expand Up @@ -138,7 +143,7 @@ func MetadataAPI(c *gin.Context) {
return
}

err = ioutil.WriteFile(filepath.Join(sourceURL.Path, "../metadata.yaml"), data, 0644)
err = ioutil.WriteFile(metadataFile, data, 0644)
if err != nil {
c.JSON(http.StatusInternalServerError, &Response{Code: "internal_error", Message: err.Error()})
return
Expand Down