-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
195 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package config | ||
|
||
import ( | ||
"context" | ||
|
||
cliz "github.com/kunitsucom/util.go/exp/cli" | ||
|
||
"github.com/kunitsucom/ddlctl/internal/consts" | ||
) | ||
|
||
func loadAutoApprove(_ context.Context, cmd *cliz.Command) bool { | ||
v, _ := cmd.GetOptionBool(consts.OptionAutoApprove) | ||
return v | ||
} | ||
|
||
func AutoApprove() bool { | ||
globalConfigMu.RLock() | ||
defer globalConfigMu.RUnlock() | ||
return globalConfig.AutoApprove | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package ddlctl | ||
|
||
import ( | ||
"bufio" | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
sqlz "github.com/kunitsucom/util.go/database/sql" | ||
errorz "github.com/kunitsucom/util.go/errors" | ||
|
||
"github.com/kunitsucom/ddlctl/internal/config" | ||
"github.com/kunitsucom/ddlctl/internal/consts" | ||
apperr "github.com/kunitsucom/ddlctl/pkg/errors" | ||
) | ||
|
||
//nolint:cyclop,funlen | ||
func Apply(ctx context.Context, args []string) error { | ||
if _, err := config.Load(ctx); err != nil { | ||
return errorz.Errorf("config.Load: %w", err) | ||
} | ||
|
||
if len(args) != 2 { | ||
return errorz.Errorf("args=%v: %w", args, apperr.ErrTwoArgumentsRequired) | ||
} | ||
|
||
left, right, err := resolve(ctx, config.Dialect(), args[0], args[1]) | ||
if err != nil { | ||
return errorz.Errorf("resolve: %w", err) | ||
} | ||
|
||
buf := new(strings.Builder) | ||
if err := diff(buf, left, right); err != nil { | ||
return errorz.Errorf("diff: %w", err) | ||
} | ||
|
||
msg := ` | ||
ddlctl will exec the following DDL queries: | ||
-- 8< -- | ||
` + buf.String() + ` | ||
-- >8 -- | ||
Do you want to apply these DDL? | ||
ddlctl will exec the DDL queries described above. | ||
Only 'yes' will be accepted to approve. | ||
Enter a value: ` | ||
|
||
if _, err := os.Stdout.WriteString(msg); err != nil { | ||
return errorz.Errorf("os.Stdout.WriteString: %w", err) | ||
} | ||
|
||
if config.AutoApprove() { | ||
if _, err := os.Stdout.WriteString(fmt.Sprintf("yes (via --%s option)\n", consts.OptionAutoApprove)); err != nil { | ||
return errorz.Errorf("os.Stdout.WriteString: %w", err) | ||
} | ||
} else { | ||
if err := prompt(); err != nil { | ||
return errorz.Errorf("prompt: %w", err) | ||
} | ||
} | ||
|
||
os.Stdout.WriteString("\nexecuting...\n") | ||
|
||
db, err := sqlz.OpenContext(ctx, _postgres, args[0]) | ||
if err != nil { | ||
return errorz.Errorf("sqlz.OpenContext: %w", err) | ||
} | ||
defer db.Close() | ||
|
||
if _, err := db.ExecContext(ctx, buf.String()); err != nil { | ||
return errorz.Errorf("db.ExecContext: %w", err) | ||
} | ||
|
||
os.Stdout.WriteString("done\n") | ||
|
||
return nil | ||
} | ||
|
||
func prompt() error { | ||
scanner := bufio.NewScanner(os.Stdin) | ||
scanner.Scan() | ||
userInput := scanner.Text() | ||
|
||
switch userInput { | ||
case "yes", "YES": | ||
return nil | ||
default: | ||
return errorz.Errorf("userInput=%s: %w", userInput, apperr.ErrUserCanceled) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters