Skip to content

Commit

Permalink
atlasexec: fix migrateapply error reporting (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
rotemtam committed Oct 29, 2023
1 parent 4211cc5 commit 42daa67
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
10 changes: 8 additions & 2 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ func (c *Client) MigratePush(ctx context.Context, params *MigratePushParams) (st
return strings.TrimSpace(resp), err
}

// MigrateApply runs the 'migrate apply' command.
// MigrateApply runs the 'migrate apply' command. If the underlying command returns an error, but prints to stdout
// it will be returned as a MigrateApply with the error message in the Error field.
func (c *Client) MigrateApply(ctx context.Context, params *MigrateApplyParams) (*MigrateApply, error) {
args := []string{"migrate", "apply", "--format", "{{ json . }}"}
if params.Env != "" {
Expand Down Expand Up @@ -222,7 +223,12 @@ func (c *Client) MigrateApply(ctx context.Context, params *MigrateApplyParams) (
args = append(args, strconv.FormatUint(params.Amount, 10))
}
args = append(args, params.Vars.AsArgs()...)
return jsonDecode[MigrateApply](c.runCommand(ctx, args))
r, err := c.runCommand(ctx, args)
if cliErr := (cliError{}); errors.As(err, &cliErr) && cliErr.stderr == "" {
r = strings.NewReader(cliErr.stdout)
err = nil
}
return jsonDecode[MigrateApply](r, err)
}

// SchemaApply runs the 'schema apply' command.
Expand Down
16 changes: 15 additions & 1 deletion atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,28 @@ func Test_MigrateApply(t *testing.T) {
require.EqualValues(t, "20230926085734", got.Target)
}

func TestBrokenApply(t *testing.T) {
c, err := atlasexec.NewClient(".", "atlas")
require.NoError(t, err)
got, err := c.MigrateApply(context.Background(), &atlasexec.MigrateApplyParams{
URL: "sqlite://?mode=memory",
DirURL: "file://testdata/broken",
})
require.NoError(t, err)
require.EqualValues(t,
`sql/migrate: execute: executing statement "broken;" from version "20231029112426": near "broken": syntax error`,
got.Error,
)
}

func TestMigrateLint(t *testing.T) {
t.Run("with broken config", func(t *testing.T) {
c, err := atlasexec.NewClient(".", "atlas")
require.NoError(t, err)
got, err := c.MigrateLint(context.Background(), &atlasexec.MigrateLintParams{
ConfigURL: "file://config-broken.hcl",
})
require.ErrorContains(t, err, `project file "config-broken.hcl" was not found`)
require.ErrorContains(t, err, `file "config-broken.hcl" was not found`)
require.Nil(t, got)
})
t.Run("with broken dev-url", func(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions atlasexec/testdata/broken/20231029112426.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
broken;
2 changes: 2 additions & 0 deletions atlasexec/testdata/broken/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:Enr95HgKxQs2iSsOANpqDUOaHc6eZeQ+ak0ZF2wjmZE=
20231029112426.sql h1:lHLnIyWaiYac90Ad0I1SOsPxvQng3tGlq++/8RkpJaI=

0 comments on commit 42daa67

Please sign in to comment.