Skip to content

Commit

Permalink
atlasexec: use JSON output for schema/clean command (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Sep 20, 2024
1 parent d312a53 commit a24e958
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
22 changes: 17 additions & 5 deletions atlasexec/atlas_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,17 @@ type (
Vars VarArgs

URL string // URL of the schema to clean. (required)
DryRun bool // If true, --dry-run is set.
AutoApprove bool // If true, --auto-approve is set.
}
// SchemaClean represents the result of a 'schema clean' command.
SchemaClean struct {
Env
Start time.Time `json:"Start,omitempty"` // When clean started.
End time.Time `json:"End,omitempty"` // When clean ended.
Applied *AppliedFile `json:"Applied,omitempty"` // Applied migration file.
Error string `json:"Error,omitempty"` // Any error that occurred during execution.
}
)

// SchemaPush runs the 'schema push' command.
Expand Down Expand Up @@ -641,8 +650,8 @@ func (c *Client) SchemaPlanApprove(ctx context.Context, params *SchemaPlanApprov
}

// SchemaClean runs the `schema clean` command.
func (c *Client) SchemaClean(ctx context.Context, params *SchemaCleanParams) (string, error) {
args := []string{"schema", "clean"}
func (c *Client) SchemaClean(ctx context.Context, params *SchemaCleanParams) (*SchemaClean, error) {
args := []string{"schema", "clean", "--format", "{{ json . }}"}
// Global flags
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
Expand All @@ -657,12 +666,15 @@ func (c *Client) SchemaClean(ctx context.Context, params *SchemaCleanParams) (st
if params.URL != "" {
args = append(args, "--url", params.URL)
} else {
return "", &InvalidParamsError{"schema clean", "missing required flag --url"}
return nil, &InvalidParamsError{"schema clean", "missing required flag --url"}
}
if params.AutoApprove {
switch {
case params.DryRun:
args = append(args, "--dry-run")
case params.AutoApprove:
args = append(args, "--auto-approve")
}
return stringVal(c.runCommand(ctx, args))
return firstResult(jsonDecode[SchemaClean](c.runCommand(ctx, args)))
}

// InvalidParamsError is an error type for invalid parameters.
Expand Down
15 changes: 8 additions & 7 deletions atlasexec/atlas_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -640,29 +640,30 @@ func TestSchema_Clean(t *testing.T) {
args string
}{
{
name: "with url",
name: "with env and dry-run",
params: &atlasexec.SchemaCleanParams{
Env: "test",
URL: "sqlite://app1.db",
Env: "test",
URL: "sqlite://app1.db",
DryRun: true,
},
args: "schema clean --env test --url sqlite://app1.db",
args: "schema clean --format {{ json . }} --env test --url sqlite://app1.db --dry-run",
},
{
name: "with auto-approve",
params: &atlasexec.SchemaCleanParams{
URL: "sqlite://app1.db",
AutoApprove: true,
},
args: "schema clean --url sqlite://app1.db --auto-approve",
args: "schema clean --format {{ json . }} --url sqlite://app1.db --auto-approve",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("TEST_ARGS", tt.args)
t.Setenv("TEST_STDOUT", "Nothing to drop")
t.Setenv("TEST_STDOUT", "{\"Start\":\"2024-09-20T14:51:40.439499+07:00\",\"End\":\"2024-09-20T14:51:40.439533+07:00\",\"Applied\":{\"Name\":\"20240920075140.sql\",\"Version\":\"20240920075140\",\"Start\":\"2024-09-20T14:51:40.43952+07:00\",\"End\":\"2024-09-20T14:51:40.439533+07:00\",\"Applied\":[\"PRAGMA foreign_keys = off;\",\"DROP TABLE `t1`;\", \"PRAGMA foreign_keys = on;\"]}}")
result, err := c.SchemaClean(context.Background(), tt.params)
require.NoError(t, err)
require.Equal(t, "Nothing to drop", result)
require.Equal(t, "20240920075140.sql", result.Applied.Name)
})
}
}
Expand Down

0 comments on commit a24e958

Please sign in to comment.