Skip to content

Commit

Permalink
cmd/action: add "auto approve" flag to "schema apply" command (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericyd authored Jan 31, 2022
1 parent 10ec926 commit a037e20
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 6 deletions.
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) {
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

0 comments on commit a037e20

Please sign in to comment.