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: update output of schema/apply command #91

Merged
merged 3 commits into from
Sep 11, 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
41 changes: 26 additions & 15 deletions atlasexec/atlas_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"fmt"
"time"
)

type (
Expand All @@ -27,21 +28,30 @@ type (
Vars VarArgs
DevURL string

URL string
To string
TxMode string
Exclude []string
Schema []string
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.
URL string
To string
TxMode string
Exclude []string
Schema []string
DryRun bool // If true, --dry-run is set.
AutoApprove bool // If true, --auto-approve is set.
PlanURL string // URL of the plan in Atlas format (atlas://<repo>/plans/<id>). (optional)
}
// SchemaApply represents the result of a 'schema apply' command.
SchemaApply struct {
Env
Changes Changes `json:"Changes,omitempty"`
// General error that occurred during execution.
// e.g., when committing or rolling back a transaction.
Error string `json:"Error,omitempty"`
// Changes holds the changes applied to the database.
// Exists for backward compatibility with the old schema
// apply structure as old SDK versions rely on it.
Changes Changes `json:"Changes,omitempty"`
Error string `json:"Error,omitempty"` // Any error that occurred during execution.
Start time.Time `json:"Start,omitempty"` // When apply (including plan) started.
End time.Time `json:"End,omitempty"` // When apply ended.
Applied *AppliedFile `json:"Applied,omitempty"` // Applied migration file (pre-planned or computed).
// Plan information might be partially filled. For example, if lint is done
// during plan-stage, the linting report is available in the Plan field. If
// the migration is pre-planned migration, the File.URL is set, etc.
Plan *SchemaPlan `json:"Plan,omitempty"`
}
// SchemaApplyError is returned when an error occurred
// during a schema applying attempt.
Expand Down Expand Up @@ -259,9 +269,10 @@ func (c *Client) SchemaApplySlice(ctx context.Context, params *SchemaApplyParams
if params.PlanURL != "" {
args = append(args, "--plan", params.PlanURL)
}
if params.DryRun {
switch {
case params.DryRun:
args = append(args, "--dry-run")
} else {
case params.AutoApprove:
args = append(args, "--auto-approve")
}
return jsonDecodeErr(newSchemaApplyError)(c.runCommand(ctx, args))
Expand Down
25 changes: 17 additions & 8 deletions atlasexec/atlas_schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,10 @@ func TestAtlasSchema_Apply(t *testing.T) {
to := fmt.Sprintf("file://%s", path)
require.NoError(t, err)
_, err = c.SchemaApply(context.Background(), &atlasexec.SchemaApplyParams{
URL: u,
To: to,
DevURL: "sqlite://file?_fk=1&cache=shared&mode=memory",
URL: u,
To: to,
DevURL: "sqlite://file?_fk=1&cache=shared&mode=memory",
AutoApprove: true,
})
require.NoError(t, err)
_, err = ce.WriteFile("schema.sql", []byte(s1+`
Expand All @@ -159,9 +160,10 @@ func TestAtlasSchema_Apply(t *testing.T) {
);`))
require.NoError(t, err)
_, err = c.SchemaApply(context.Background(), &atlasexec.SchemaApplyParams{
URL: u,
To: to,
DevURL: "sqlite://file?_fk=1&cache=shared&mode=memory",
URL: u,
To: to,
DevURL: "sqlite://file?_fk=1&cache=shared&mode=memory",
AutoApprove: true,
})
require.NoError(t, err)

Expand Down Expand Up @@ -596,14 +598,21 @@ func TestSchema_Apply(t *testing.T) {
{
name: "no params",
params: &atlasexec.SchemaApplyParams{},
args: "schema apply --format {{ json . }} --auto-approve",
args: "schema apply --format {{ json . }}",
},
{
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",
args: "schema apply --format {{ json . }} --plan atlas://app1/plans/foo-plan",
},
{
name: "with auto-approve",
params: &atlasexec.SchemaApplyParams{
AutoApprove: true,
},
args: "schema apply --format {{ json . }} --auto-approve",
},
}
for _, tt := range testCases {
Expand Down
Loading