Add a --complete flag to to the start command (#33)

If set, complete the migration immediately after starting it.

For example:

```bash
go run start examples/02_create_another_table.json --complete
```

Completes the migration immediately, without the user having to run
`complete`.
This commit is contained in:
Andrew Farries 2023-07-18 09:08:49 +01:00 committed by GitHub
parent 11cbe3a2bd
commit 37b75384a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 24 deletions

View File

@ -43,7 +43,7 @@ func NewRoll(ctx context.Context) (*roll.Roll, error) {
// Execute executes the root command.
func Execute() error {
// register subcommands
rootCmd.AddCommand(startCmd)
rootCmd.AddCommand(startCmd())
rootCmd.AddCommand(completeCmd)
rootCmd.AddCommand(rollbackCmd)
rootCmd.AddCommand(analyzeCmd)

View File

@ -10,32 +10,46 @@ import (
"github.com/spf13/cobra"
)
var startCmd = &cobra.Command{
Use: "start <file>",
Short: "Start a migration for the operations present in the given file",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
fileName := args[0]
func startCmd() *cobra.Command {
var complete bool
m, err := NewRoll(cmd.Context())
if err != nil {
return err
}
defer m.Close()
startCmd := &cobra.Command{
Use: "start <file>",
Short: "Start a migration for the operations present in the given file",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
fileName := args[0]
migration, err := migrations.ReadMigrationFile(args[0])
if err != nil {
return fmt.Errorf("reading migration file: %w", err)
}
m, err := NewRoll(cmd.Context())
if err != nil {
return err
}
defer m.Close()
version := strings.TrimSuffix(filepath.Base(fileName), filepath.Ext(fileName))
migration, err := migrations.ReadMigrationFile(args[0])
if err != nil {
return fmt.Errorf("reading migration file: %w", err)
}
err = m.Start(cmd.Context(), migration)
if err != nil {
return err
}
version := strings.TrimSuffix(filepath.Base(fileName), filepath.Ext(fileName))
fmt.Printf("Migration successful!, new version of the schema available under postgres '%s' schema\n", version)
return nil
},
err = m.Start(cmd.Context(), migration)
if err != nil {
return err
}
if complete {
if err = m.Complete(cmd.Context()); err != nil {
return err
}
}
fmt.Printf("Migration successful!, new version of the schema available under postgres '%s' schema\n", version)
return nil
},
}
startCmd.Flags().BoolVarP(&complete, "complete", "c", false, "Mark the migration as complete")
return startCmd
}