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

Add "auto approve" flag to "schema apply" command #526

Merged
merged 4 commits into from
Jan 31, 2022
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
17 changes: 11 additions & 6 deletions cmd/action/apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ var (
Addr string
DryRun bool
Schema []string
AutoApprove bool
}
// ApplyCmd represents the apply command.
ApplyCmd = &cobra.Command{
Expand Down Expand Up @@ -57,6 +58,7 @@ func init() {
ApplyCmd.Flags().BoolVarP(&ApplyFlags.DryRun, "dry-run", "", false, "Dry-run. Print SQL plan without prompting for execution")
ApplyCmd.Flags().StringVarP(&ApplyFlags.Addr, "addr", "", "127.0.0.1:5800", "used with -w, local address to bind the server to")
ApplyCmd.Flags().StringSliceVarP(&ApplyFlags.Schema, "schema", "s", nil, "Set schema name")
ApplyCmd.Flags().BoolVarP(&ApplyFlags.AutoApprove, "auto-approve", "", false, "Auto approve. Apply the schema changes without prompting for approval")
cobra.CheckErr(ApplyCmd.MarkFlagRequired("dsn"))
cobra.CheckErr(ApplyCmd.MarkFlagRequired("file"))
}
Expand All @@ -69,10 +71,10 @@ func CmdApplyRun(cmd *cobra.Command, args []string) {
}
d, err := defaultMux.OpenAtlas(ApplyFlags.DSN)
cobra.CheckErr(err)
applyRun(d, ApplyFlags.DSN, ApplyFlags.File, ApplyFlags.DryRun)
applyRun(d, ApplyFlags.DSN, ApplyFlags.File, ApplyFlags.DryRun, ApplyFlags.AutoApprove)
}

func applyRun(d *Driver, dsn string, file string, dryRun bool) {
func applyRun(d *Driver, dsn string, file string, dryRun bool, autoApprove bool) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rotemtam should we introduce a small config struct here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but can be done in additional PR.

ctx := context.Background()
schemas := ApplyFlags.Schema
if n, err := SchemaNameFromDSN(dsn); n != "" {
Expand Down Expand Up @@ -119,14 +121,17 @@ func applyRun(d *Driver, dsn string, file string, dryRun bool) {
if dryRun {
return
}
if autoApprove || promptUser() {
cobra.CheckErr(d.ApplyChanges(ctx, changes))
}
}

func promptUser() bool {
prompt := promptui.Select{
Label: "Are you sure?",
Items: []string{answerApply, answerAbort},
}
_, result, err := prompt.Run()
cobra.CheckErr(err)
if result == answerApply {
err = d.ApplyChanges(ctx, changes)
cobra.CheckErr(err)
}
return result == answerApply
}
1 change: 1 addition & 0 deletions doc/md/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ atlas schema apply -d "sqlite://file:ex1.db?_fk=1" -f atlas.hcl
#### Flags
```
--addr string used with -w, local address to bind the server to (default "127.0.0.1:5800")
--auto-approve Auto approve. Apply the schema changes without prompting for approval
--dry-run Dry-run. Print SQL plan without prompting for execution
-d, --dsn string [driver://username:password@protocol(address)/dbname?param=value] Select data source using the dsn format
-f, --file string [/path/to/file] file containing schema
Expand Down
29 changes: 29 additions & 0 deletions internal/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,35 @@ func testCLISchemaApplyDry(t T, h string, dsn string) {
require.False(t, ok, "expected users table not to be created")
}

func testCLISchemaApplyAutoApprove(t T, h string, dsn string) {
// Required to have a clean "stderr" while running first time.
err := exec.Command("go", "run", "-mod=mod", "ariga.io/atlas/cmd/atlas").Run()
require.NoError(t, err)
t.dropTables("users")
f := "atlas.hcl"
err = ioutil.WriteFile(f, []byte(h), 0644)
require.NoError(t, err)
defer os.Remove(f)
cmd := exec.Command("go", "run", "ariga.io/atlas/cmd/atlas",
"schema",
"apply",
"-d",
dsn,
"-f",
f,
"--auto-approve",
)
stdout, stderr := bytes.NewBuffer(nil), bytes.NewBuffer(nil)
cmd.Stderr = stderr
cmd.Stdout = stdout
require.NoError(t, err)
require.NoError(t, cmd.Run(), stderr.String(), stdout.String())
require.Empty(t, stderr.String(), stderr.String())
require.Contains(t, stdout.String(), "-- Planned")
u := t.loadUsers()
require.NotNil(t, u)
}

func testCLISchemaDiff(t T, dsn string) {
// Required to have a clean "stderr" while running first time.
err := exec.Command("go", "run", "-mod=mod", "ariga.io/atlas/cmd/atlas").Run()
Expand Down