Repository: hasura/graphql-engine
File: cli/commands/migrate_create.go (Go)

Code before:
		}
		defer os.RemoveAll(tmpDirName)
		plugins := make(metadataTypes.MetadataPlugins, 0)
		plugins = append(plugins, metadata.New(o.EC, tmpDirName))
		migrateDrv.SetMetadataPlugins(plugins)
		// fetch metadata from server
		files, err := migrateDrv.ExportMetadata()
		if err != nil {
			return 0, errors.Wrap(err, "cannot fetch metadata from server")
		}
		err = migrateDrv.WriteMetadata(files)
		if err != nil {
			return 0, errors.Wrap(err, "cannot write to tmp file")
		}

		for name := range files {
			err = createOptions.SetMetaUpFromFile(name)
			if err != nil {
				return 0, errors.Wrap(err, "cannot parse metadata from the server")
			}
		}
	}

	// create pure sql based migrations here
	if o.flags.Changed("up-sql") && o.flags.Changed("down-sql") {
		errUpSQL := createOptions.SetSQLUp(o.upSQL)
		errDownSQL := createOptions.SetSQLDown(o.downSQL)

		if errUpSQL != nil {
			return 0, errors.Wrap(errUpSQL, "up migration with SQL string could not be created")
		}

		if errDownSQL != nil {
			return 0, errors.Wrap(errDownSQL, "down migration with SQL string could not be created")
		}
	}

	if !o.flags.Changed("sql-from-file") && !o.flags.Changed("metadata-from-file") && !o.metaDataServer && !o.sqlServer && o.EC.Config.Version == cli.V1 && !(o.flags.Changed("up-sql") && o.flags.Changed("down-sql")) {
		// Set empty data for [up|down].yaml
		createOptions.MetaUp = []byte(`[]`)
		createOptions.MetaDown = []byte(`[]`)
	}

	if !o.flags.Changed("sql-from-file") && !o.flags.Changed("metadata-from-file") && !o.metaDataServer && !o.sqlServer && o.EC.Config.Version != cli.V1 && !(o.flags.Changed("up-sql") && o.flags.Changed("down-sql")) {
		// Set empty data for [up|down].sql
		createOptions.SQLUp = []byte(``)
		createOptions.SQLDown = []byte(``)
	}

	defer func() {
		if err != nil {

Review comment (suggestion):
Handling error as soon as it happens will probably be better, something like
```
err = createOptions.SetSQLUp(o.upSQL)
if err != nil {
    return 0, errors.Wrap(errUpSQL, "up migration with SQL string could not be created")
}
```

Code after:
		migrateDrv.SetMetadataPlugins(plugins)
		// fetch metadata from server
		files, err := migrateDrv.ExportMetadata()
		if err != nil {
			return 0, errors.Wrap(err, "cannot fetch metadata from server")
		}
		err = migrateDrv.WriteMetadata(files)
		if err != nil {
			return 0, errors.Wrap(err, "cannot write to tmp file")
		}

		for name := range files {
			err = createOptions.SetMetaUpFromFile(name)
			if err != nil {
				return 0, errors.Wrap(err, "cannot parse metadata from the server")
			}
		}
	}

	// create pure sql based migrations here
	if o.flags.Changed("up-sql") {
		err = createOptions.SetSQLUp(o.upSQL)
		if err != nil {
			return 0, errors.Wrap(err, "up migration with SQL string could not be created")
		}
	}

	if o.flags.Changed("down-sql") {
		err = createOptions.SetSQLDown(o.downSQL)
		if err != nil {
			return 0, errors.Wrap(err, "down migration with SQL string could not be created")
		}
	}

	if !o.flags.Changed("sql-from-file") && !o.flags.Changed("metadata-from-file") && !o.metaDataServer && !o.sqlServer && o.EC.Config.Version == cli.V1 && !o.flags.Changed("up-sql") && !o.flags.Changed("down-sql") {
		// Set empty data for [up|down].yaml
		createOptions.MetaUp = []byte(`[]`)
		createOptions.MetaDown = []byte(`[]`)
	}

	if !o.flags.Changed("sql-from-file") && !o.flags.Changed("metadata-from-file") && !o.metaDataServer && !o.sqlServer && o.EC.Config.Version != cli.V1 && !o.flags.Changed("up-sql") && !o.flags.Changed("down-sql") {
		// Set empty data for [up|down].sql
		createOptions.SQLUp = []byte(``)
		createOptions.SQLDown = []byte(``)
	}

	defer func() {
		if err != nil {
			createOptions.Delete()
		}
	}()