mirror of
https://github.com/xataio/pgroll.git
synced 2024-10-26 05:46:27 +03:00
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:
parent
11cbe3a2bd
commit
37b75384a9
@ -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)
|
||||
|
60
cmd/start.go
60
cmd/start.go
@ -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
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user