Skip to content

Commit

Permalink
atlasexec: add schema/clean command (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Sep 18, 2024
1 parent 41bde7d commit 5d10c68
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 0 deletions.
34 changes: 34 additions & 0 deletions atlasexec/atlas_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,15 @@ type (
Link string `json:"Link,omitempty"` // Link to the plan in the registry.
Status string `json:"Status,omitempty"` // Status of the plan in the registry.
}
// SchemaCleanParams are the parameters for the `schema clean` command.
SchemaCleanParams struct {
ConfigURL string
Env string
Vars VarArgs

URL string // URL of the schema to clean. (required)
AutoApprove bool // If true, --auto-approve is set.
}
)

// SchemaPush runs the 'schema push' command.
Expand Down Expand Up @@ -625,6 +634,31 @@ func (c *Client) SchemaPlanApprove(ctx context.Context, params *SchemaPlanApprov
return firstResult(jsonDecode[SchemaPlanApprove](c.runCommand(ctx, args)))
}

// SchemaClean runs the `schema clean` command.
func (c *Client) SchemaClean(ctx context.Context, params *SchemaCleanParams) (string, error) {
args := []string{"schema", "clean"}
// Global flags
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
}
if params.Env != "" {
args = append(args, "--env", params.Env)
}
if params.Vars != nil {
args = append(args, params.Vars.AsArgs()...)
}
// Flags of the 'schema clean' sub-commands
if params.URL != "" {
args = append(args, "--url", params.URL)
} else {
return "", &InvalidParamsError{"schema clean", "missing required flag --url"}
}
if params.AutoApprove {
args = append(args, "--auto-approve")
}
return stringVal(c.runCommand(ctx, args))
}

// InvalidParamsError is an error type for invalid parameters.
type InvalidParamsError struct {
cmd string
Expand Down
39 changes: 39 additions & 0 deletions atlasexec/atlas_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,6 +626,45 @@ func TestSchema_Apply(t *testing.T) {
}
}

func TestSchema_Clean(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
c, err := atlasexec.NewClient(t.TempDir(), filepath.Join(wd, "./mock-atlas.sh"))
require.NoError(t, err)

testCases := []struct {
name string
params *atlasexec.SchemaCleanParams
args string
}{
{
name: "with url",
params: &atlasexec.SchemaCleanParams{
Env: "test",
URL: "sqlite://app1.db",
},
args: "schema clean --env test --url sqlite://app1.db",
},
{
name: "with auto-approve",
params: &atlasexec.SchemaCleanParams{
URL: "sqlite://app1.db",
AutoApprove: true,
},
args: "schema clean --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")
result, err := c.SchemaClean(context.Background(), tt.params)
require.NoError(t, err)
require.Equal(t, "Nothing to drop", result)
})
}
}

func TestSchema_ApplyEnvs(t *testing.T) {
wd, err := os.Getwd()
require.NoError(t, err)
Expand Down
25 changes: 25 additions & 0 deletions atlasexec/mock-atlas.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,30 @@
#!/bin/bash

# TEST_BATCH provide the directory containts all
# outputs for multiple runs. The path should be absolulate
# or related to current working directory.
if [[ "$TEST_BATCH" != "" ]]; then
COUNTER_FILE=$TEST_BATCH/counter
COUNTER=$(cat $COUNTER_FILE 2>/dev/null)
COUNTER=$((COUNTER+1))
DIR_CUR="$TEST_BATCH/$COUNTER"
if [ ! -d "$DIR_CUR" ]; then
>&2 echo -n "$DIR_CUR does not exist, quitting..."
exit 1
fi
# Save counter for the next runs
echo -n $COUNTER > $COUNTER_FILE
if [ -f "$DIR_CUR/args" ]; then
TEST_ARGS=$(cat $DIR_CUR/args)
fi
if [ -f "$DIR_CUR/stderr" ]; then
TEST_STDERR=$(cat $DIR_CUR/stderr)
fi
if [ -f "$DIR_CUR/stdout" ]; then
TEST_STDOUT=$(cat $DIR_CUR/stdout)
fi
fi

if [[ "$TEST_ARGS" != "$@" ]]; then
>&2 echo "Receive unexpected args: $@"
exit 1
Expand Down

0 comments on commit 5d10c68

Please sign in to comment.