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 "schema test" #77

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 @@ -46,6 +46,7 @@ The SDK provides the following APIs:
- `SchemaApply`: runs the "atlas schema apply" command.
- `SchemaInspect`: runs the "atlas schema inspect" command.
- `SchemaDiff`: runs the "atlas schema diff" command.
- `SchemaTest`: runs the "atlas schema test" command.

Example with `MigrateApply` API:

Expand Down
31 changes: 31 additions & 0 deletions atlasexec/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,15 @@ type (
URL string
Vars Vars
}
// SchemaTestParams are the parameters for the `schema test` command.
SchemaTestParams struct {
Env string
ConfigURL string
URL string
DevURL string
Run string
Vars Vars
}
Vars map[string]string
)

Expand Down Expand Up @@ -461,6 +470,28 @@ func (c *Client) SchemaInspect(ctx context.Context, params *SchemaInspectParams)
return stringVal(c.runCommand(ctx, args))
}

// SchemaTest runs the 'schema test' command.
func (c *Client) SchemaTest(ctx context.Context, params *SchemaTestParams) (string, error) {
args := []string{"schema", "test"}
if params.Env != "" {
args = append(args, "--env", params.Env)
}
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
}
if params.URL != "" {
args = append(args, "--url", params.URL)
}
if params.DevURL != "" {
args = append(args, "--dev-url", params.DevURL)
}
if params.Run != "" {
args = append(args, "--run", params.Run)
}
args = append(args, params.Vars.AsArgs()...)
return stringVal(c.runCommand(ctx, args))
}

func lintArgs(params *MigrateLintParams) ([]string, error) {
args := []string{"migrate", "lint"}
if params.Web {
Expand Down
58 changes: 58 additions & 0 deletions atlasexec/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1000,3 +1000,61 @@ func TestMigrateTest(t *testing.T) {
})
}
}

func TestSchemaTest(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.SchemaTestParams
expect string
}{
{
name: "no params",
params: &atlasexec.SchemaTestParams{},
expect: "schema test",
},
{
name: "with env",
params: &atlasexec.SchemaTestParams{
Env: "test",
},
expect: "schema test --env test",
},
{
name: "with config",
params: &atlasexec.SchemaTestParams{
ConfigURL: "file://config.hcl",
},
expect: "schema test --config file://config.hcl",
},
{
name: "with dev-url",
params: &atlasexec.SchemaTestParams{
DevURL: "sqlite://file?_fk=1&cache=shared&mode=memory",
},
expect: "schema test --dev-url sqlite://file?_fk=1&cache=shared&mode=memory",
},
{
name: "with run",
params: &atlasexec.SchemaTestParams{
Run: "example",
},
expect: "schema test --run example",
},
} {
t.Run(tt.name, func(t *testing.T) {
_, err := c.SchemaTest(context.Background(), tt.params)
require.Error(t, err)
// The script mock-args.sh exit with an error code.
// So, our atlasexec.SchemaTest should return a Error.
// Which contains all output from the script (both stdout and stderr).
require.Equal(t, tt.expect, err.Error())
})
}
}
Loading