From a24e95878819b70ef2274bdb1cf8837c90cc5b65 Mon Sep 17 00:00:00 2001 From: "Giau. Tran Minh" <12751435+giautm@users.noreply.github.com> Date: Fri, 20 Sep 2024 15:20:34 +0700 Subject: [PATCH] atlasexec: use JSON output for schema/clean command (#96) --- atlasexec/atlas_schema.go | 22 +++++++++++++++++----- atlasexec/atlas_schema_test.go | 15 ++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/atlasexec/atlas_schema.go b/atlasexec/atlas_schema.go index 6918c84..c7457d0 100644 --- a/atlasexec/atlas_schema.go +++ b/atlasexec/atlas_schema.go @@ -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. @@ -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) @@ -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. diff --git a/atlasexec/atlas_schema_test.go b/atlasexec/atlas_schema_test.go index 4fb8bc8..79b80af 100644 --- a/atlasexec/atlas_schema_test.go +++ b/atlasexec/atlas_schema_test.go @@ -640,12 +640,13 @@ 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", @@ -653,16 +654,16 @@ func TestSchema_Clean(t *testing.T) { 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) }) } }