From 42daa671be131937afc850dd81110b4fbc47c32b Mon Sep 17 00:00:00 2001 From: Rotem Tamir Date: Sun, 29 Oct 2023 15:21:14 +0200 Subject: [PATCH] atlasexec: fix migrateapply error reporting (#27) --- atlasexec/atlas.go | 10 ++++++++-- atlasexec/atlas_test.go | 16 +++++++++++++++- atlasexec/testdata/broken/20231029112426.sql | 1 + atlasexec/testdata/broken/atlas.sum | 2 ++ 4 files changed, 26 insertions(+), 3 deletions(-) create mode 100644 atlasexec/testdata/broken/20231029112426.sql create mode 100644 atlasexec/testdata/broken/atlas.sum diff --git a/atlasexec/atlas.go b/atlasexec/atlas.go index 4323654..e94616b 100644 --- a/atlasexec/atlas.go +++ b/atlasexec/atlas.go @@ -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 != "" { @@ -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. diff --git a/atlasexec/atlas_test.go b/atlasexec/atlas_test.go index 1c3d43a..de75c2e 100644 --- a/atlasexec/atlas_test.go +++ b/atlasexec/atlas_test.go @@ -63,6 +63,20 @@ 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") @@ -70,7 +84,7 @@ func TestMigrateLint(t *testing.T) { 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) { diff --git a/atlasexec/testdata/broken/20231029112426.sql b/atlasexec/testdata/broken/20231029112426.sql new file mode 100644 index 0000000..e67bfe2 --- /dev/null +++ b/atlasexec/testdata/broken/20231029112426.sql @@ -0,0 +1 @@ +broken; \ No newline at end of file diff --git a/atlasexec/testdata/broken/atlas.sum b/atlasexec/testdata/broken/atlas.sum new file mode 100644 index 0000000..a5543c5 --- /dev/null +++ b/atlasexec/testdata/broken/atlas.sum @@ -0,0 +1,2 @@ +h1:Enr95HgKxQs2iSsOANpqDUOaHc6eZeQ+ak0ZF2wjmZE= +20231029112426.sql h1:lHLnIyWaiYac90Ad0I1SOsPxvQng3tGlq++/8RkpJaI=