Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

atlasexec: support "migrate test" #76

Merged
merged 1 commit into from
Jun 21, 2024
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
39 changes: 39 additions & 0 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
Expand Down
67 changes: 66 additions & 1 deletion atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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())
})
}
}
Loading