diff --git a/README.md b/README.md index ddabba7..a6fff18 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,7 @@ The SDK provides the following APIs: - `MigrateApplySlice`: runs the 'atlas migrate apply' command for multiple targets. - `MigrateDown`: runs the "atlas migrate down" command. - `MigrateStatus`: runs the "atlas migrate status" command. +- `MigrateTest`: runs the "atlas migrate test" command. - `SchemaApply`: runs the "atlas schema apply" command. - `SchemaInspect`: runs the "atlas schema inspect" command. - `SchemaDiff`: runs the "atlas schema diff" command. diff --git a/atlasexec/atlas.go b/atlasexec/atlas.go index 92ff5d9..cf8c455 100644 --- a/atlasexec/atlas.go +++ b/atlasexec/atlas.go @@ -115,6 +115,17 @@ type ( Base string Format string } + // MigrateTestParams are the parameters for the `migrate test` command. + MigrateTestParams struct { + Env string + ConfigURL string + DirURL string + DevURL string + DirFormat string + Run string + RevisionsSchema string + Vars Vars + } // SchemaApplyParams are the parameters for the `schema apply` command. SchemaApplyParams struct { Env string @@ -350,6 +361,34 @@ func (c *Client) MigrateDown(ctx context.Context, params *MigrateDownParams) (*M return firstResult(jsonDecode[MigrateDown](r, err)) } +// MigrateTest runs the 'migrate test' command. +func (c *Client) MigrateTest(ctx context.Context, params *MigrateTestParams) (string, error) { + args := []string{"migrate", "test"} + if params.Env != "" { + args = append(args, "--env", params.Env) + } + if params.ConfigURL != "" { + args = append(args, "--config", params.ConfigURL) + } + if params.DirURL != "" { + args = append(args, "--dir", params.DirURL) + } + if params.DirFormat != "" { + args = append(args, "--dir-format", params.DirFormat) + } + if params.DevURL != "" { + args = append(args, "--dev-url", params.DevURL) + } + if params.RevisionsSchema != "" { + args = append(args, "--revisions-schema", params.RevisionsSchema) + } + if params.Run != "" { + args = append(args, "--run", params.Run) + } + args = append(args, params.Vars.AsArgs()...) + return stringVal(c.runCommand(ctx, args)) +} + // SchemaApply runs the 'schema apply' command. func (c *Client) SchemaApply(ctx context.Context, params *SchemaApplyParams) (*SchemaApply, error) { return firstResult(c.SchemaApplySlice(ctx, params)) diff --git a/atlasexec/atlas_test.go b/atlasexec/atlas_test.go index 0f3c508..ae78c9a 100644 --- a/atlasexec/atlas_test.go +++ b/atlasexec/atlas_test.go @@ -87,7 +87,7 @@ func Test_MigrateApply(t *testing.T) { }) require.NoError(t, err) require.Equal(t, "sqlite3", got.Env.Driver) - require.Equal(t, "migrations", got.Env.Dir) + require.Equal(t, "file://migrations", got.Env.Dir) require.Equal(t, "sqlite://file?_fk=1&cache=shared&mode=memory", got.Env.URL.String()) require.Equal(t, "20230926085734", got.Target) // Add dirty changes and try again @@ -935,3 +935,68 @@ func TestMigrateDown(t *testing.T) { }) } } + +func TestMigrateTest(t *testing.T) { + wd, err := os.Getwd() + require.NoError(t, err) + // Mock the client with a script that just prints the arguments to stderr and + // exit with an error code. + c, err := atlasexec.NewClient(t.TempDir(), filepath.Join(wd, "./mock-args.sh")) + require.NoError(t, err) + + for _, tt := range []struct { + name string + params *atlasexec.MigrateTestParams + expect string + }{ + { + name: "no params", + params: &atlasexec.MigrateTestParams{}, + expect: "migrate test", + }, + { + name: "with env", + params: &atlasexec.MigrateTestParams{ + Env: "test", + }, + expect: "migrate test --env test", + }, + { + name: "with config", + params: &atlasexec.MigrateTestParams{ + ConfigURL: "file://config.hcl", + }, + expect: "migrate test --config file://config.hcl", + }, + { + name: "with dev-url", + params: &atlasexec.MigrateTestParams{ + DevURL: "sqlite://file?_fk=1&cache=shared&mode=memory", + }, + expect: "migrate test --dev-url sqlite://file?_fk=1&cache=shared&mode=memory", + }, + { + name: "with run", + params: &atlasexec.MigrateTestParams{ + Run: "example", + }, + expect: "migrate test --run example", + }, + { + name: "with revisions-schema", + params: &atlasexec.MigrateTestParams{ + RevisionsSchema: "schema", + }, + expect: "migrate test --revisions-schema schema", + }, + } { + t.Run(tt.name, func(t *testing.T) { + _, err := c.MigrateTest(context.Background(), tt.params) + require.Error(t, err) + // The script mock-args.sh exit with an error code. + // So, our atlasexec.MigrateTest should return a Error. + // Which contains all output from the script (both stdout and stderr). + require.Equal(t, tt.expect, err.Error()) + }) + } +}