Skip to content

Commit

Permalink
atlasexec: support plan argument for schema/apply (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
giautm committed Aug 29, 2024
1 parent 4f3593c commit 8714b31
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 13 deletions.
32 changes: 19 additions & 13 deletions atlasexec/atlas_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ type (
TxMode string
Exclude []string
Schema []string
DryRun bool
DryRun bool // If false, --auto-approve is set.
PlanURL string // URL of the plan in Atlas format (atlas://<repo>/plans/<id>). (optional)
}
// SchemaApply contains a summary of a 'schema apply' execution on a database.
SchemaApply struct {
Expand Down Expand Up @@ -108,15 +109,15 @@ type (

From, To []string
Repo string
Pending bool // Push plan in pending state.
File string
Pending bool // Push plan in pending state.
File string // File to push. (optional)
}
// SchemaPlanPullParams are the parameters for the `schema plan pull` command.
SchemaPlanPullParams struct {
ConfigURL string
Env string
Vars VarArgs
URL string
URL string // URL to the plan in Atlas format. (required)
}
// SchemaPlanLintParams are the parameters for the `schema plan lint` command.
SchemaPlanLintParams struct {
Expand Down Expand Up @@ -226,23 +227,23 @@ func (c *Client) SchemaApply(ctx context.Context, params *SchemaApplyParams) (*S
// SchemaApplySlice runs the 'schema apply' command for multiple targets.
func (c *Client) SchemaApplySlice(ctx context.Context, params *SchemaApplyParams) ([]*SchemaApply, error) {
args := []string{"schema", "apply", "--format", "{{ json . }}"}
// Global flags
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
}
if params.Env != "" {
args = append(args, "--env", params.Env)
}
if params.ConfigURL != "" {
args = append(args, "--config", params.ConfigURL)
if params.Vars != nil {
args = append(args, params.Vars.AsArgs()...)
}
// Flags of the 'schema apply' sub-commands
if params.URL != "" {
args = append(args, "--url", params.URL)
}
if params.To != "" {
args = append(args, "--to", params.To)
}
if params.DryRun {
args = append(args, "--dry-run")
} else {
args = append(args, "--auto-approve")
}
if params.TxMode != "" {
args = append(args, "--tx-mode", params.TxMode)
}
Expand All @@ -255,8 +256,13 @@ func (c *Client) SchemaApplySlice(ctx context.Context, params *SchemaApplyParams
if len(params.Exclude) > 0 {
args = append(args, "--exclude", listString(params.Exclude))
}
if params.Vars != nil {
args = append(args, params.Vars.AsArgs()...)
if params.PlanURL != "" {
args = append(args, "--plan", params.PlanURL)
}
if params.DryRun {
args = append(args, "--dry-run")
} else {
args = append(args, "--auto-approve")
}
return jsonDecodeErr(newSchemaApplyError)(c.runCommand(ctx, args))
}
Expand Down
35 changes: 35 additions & 0 deletions atlasexec/atlas_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -581,3 +581,38 @@ func TestSchema_Push(t *testing.T) {
})
}
}

func TestSchema_Apply(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.SchemaApplyParams
args string
}{
{
name: "no params",
params: &atlasexec.SchemaApplyParams{},
args: "schema apply --format {{ json . }} --auto-approve",
},
{
name: "with plan",
params: &atlasexec.SchemaApplyParams{
PlanURL: "atlas://app1/plans/foo-plan",
},
args: "schema apply --format {{ json . }} --plan atlas://app1/plans/foo-plan --auto-approve",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("TEST_ARGS", tt.args)
t.Setenv("TEST_STDOUT", `{"Driver":"sqlite3"}`)
result, err := c.SchemaApply(context.Background(), tt.params)
require.NoError(t, err)
require.Equal(t, "sqlite3", result.Driver)
})
}
}

0 comments on commit 8714b31

Please sign in to comment.