Skip to content

Commit

Permalink
atlasexec: support "migrate test"
Browse files Browse the repository at this point in the history
  • Loading branch information
datdao committed Jun 21, 2024
1 parent a9fdd42 commit 1ac7d38
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
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
65 changes: 65 additions & 0 deletions atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
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())
})
}
}

0 comments on commit 1ac7d38

Please sign in to comment.