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: added SchemaPlanList command support #88

Merged
merged 1 commit into from
Aug 28, 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
4 changes: 2 additions & 2 deletions atlasexec/atlas_models.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type (
Checks []*FileChecks // Assertion checks
Error *struct {
Stmt string // SQL statement that failed.
Text string // Error returned by the database.
Text string // Error returned by the database.
}
}
// RevertedFile is part of a MigrateDown containing information about a reverted file in a downgrade attempt.
Expand All @@ -37,7 +37,7 @@ type (
Scope string // Scope of the revert. e.g., statement, versions, etc.
Error *struct {
Stmt string // SQL statement that failed.
Text string // Error returned by the database.
Text string // Error returned by the database.
}
}
// A SummaryReport contains a summary of the analysis of all files.
Expand Down
66 changes: 62 additions & 4 deletions atlasexec/atlas_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,26 @@ type (
DevURL string

From, To []string
Name string
Repo string
Name string
// The below are mutually exclusive and can be replaced
// with the 'schema plan' sub-commands instead.
DryRun bool // If false, --auto-approve is set.
Pending bool
Push, Save bool
}
// SchemaPlanListParams are the parameters for the `schema plan list` command.
SchemaPlanListParams struct {
ConfigURL string
Env string
Vars VarArgs
Context *RunContext
DevURL string

From, To []string
Repo string
Pending bool // If true, only pending plans are listed.
}
// SchemaPlanPushParams are the parameters for the `schema plan push` command.
SchemaPlanPushParams struct {
ConfigURL string
Expand All @@ -82,9 +94,9 @@ type (
DevURL string

From, To []string
File string
Repo string
Pending bool
Pending bool // Push plan in pending state.
File string
}
// SchemaPlanPullParams are the parameters for the `schema plan pull` command.
SchemaPlanPullParams struct {
Expand Down Expand Up @@ -114,8 +126,8 @@ type (
DevURL string

From, To []string
Name string
Repo string
Name string
File string
}
// SchemaPlanApproveParams are the parameters for the `schema plan approve` command.
Expand Down Expand Up @@ -308,6 +320,52 @@ func (c *Client) SchemaPlan(ctx context.Context, params *SchemaPlanParams) (*Sch
return firstResult(jsonDecode[SchemaPlan](c.runCommand(ctx, args)))
}

// SchemaPlanList runs the `schema plan list` command.
func (c *Client) SchemaPlanList(ctx context.Context, params *SchemaPlanListParams) ([]SchemaPlanFile, error) {
args := []string{"schema", "plan", "list", "--format", "{{ json . }}"}
// 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()...)
}
// Hidden flags
if params.Context != nil {
buf, err := json.Marshal(params.Context)
if err != nil {
return nil, err
}
args = append(args, "--context", string(buf))
}
// Flags of the 'schema plan lint' sub-commands
if params.DevURL != "" {
args = append(args, "--dev-url", params.DevURL)
}
if len(params.From) > 0 {
args = append(args, "--from", listString(params.From))
}
if len(params.To) > 0 {
args = append(args, "--to", listString(params.To))
}
if params.Repo != "" {
args = append(args, "--repo", params.Repo)
}
if params.Pending {
args = append(args, "--pending")
}
args = append(args, "--auto-approve")
// NOTE: This command only support one result.
v, err := firstResult(jsonDecode[[]SchemaPlanFile](c.runCommand(ctx, args)))
if err != nil {
return nil, err
}
return *v, nil
}

// SchemaPlanPush runs the `schema plan push` command.
func (c *Client) SchemaPlanPush(ctx context.Context, params *SchemaPlanPushParams) (string, error) {
args := []string{"schema", "plan", "push", "--format", "{{ json . }}"}
Expand Down
44 changes: 44 additions & 0 deletions atlasexec/atlas_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,47 @@ func TestSchema_PlanPull(t *testing.T) {
})
}
}

func TestSchema_PlanList(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.SchemaPlanListParams
args string
}{
{
name: "no params",
params: &atlasexec.SchemaPlanListParams{},
args: "schema plan list --format {{ json . }} --auto-approve",
},
{
name: "with repo",
params: &atlasexec.SchemaPlanListParams{
Repo: "atlas://testing-repo",
From: []string{"env://url"},
},
args: "schema plan list --format {{ json . }} --from env://url --repo atlas://testing-repo --auto-approve",
},
{
name: "with repo and pending",
params: &atlasexec.SchemaPlanListParams{
Repo: "atlas://testing-repo",
Pending: true,
},
args: "schema plan list --format {{ json . }} --repo atlas://testing-repo --pending --auto-approve",
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
t.Setenv("TEST_ARGS", tt.args)
t.Setenv("TEST_STDOUT", `[{"Name":"pr-2-ufnTS7Nr"}]`)
result, err := c.SchemaPlanList(context.Background(), tt.params)
require.NoError(t, err)
require.Equal(t, "pr-2-ufnTS7Nr", result[0].Name)
})
}
}
Loading