Skip to content
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
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,27 @@ After setting up the CLI, you can run the following commands directly from the t

These commands are built into the CLI, making it easy to perform common migration tasks without having to write custom code each time.

### 3. Add Commands to Existing cobra.Command

```go
var rootCmd = &cobra.Command{
Use: "migration",
Short: "This is your existing cobra.Command",
Run: func(cmd *cobra.Command, args []string) {
cmd.Help()
},
}

rootCmd.AddCommand(
cli.ListCommand(ctx),
cli.MigrateCommand(ctx),
cli.RollbackCommand(ctx),
cli.ResetCommand(ctx),
cli.CleanCommand(ctx),
cli.CreateCommand(ctx),
)
```

### Full Example

```go
Expand Down
39 changes: 32 additions & 7 deletions cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewCli(config CliConfig) (*Cli, error) {
}, nil
}

func (c *Cli) Execute(ctx context.Context) error {
func (c *Cli) ListCommand(ctx context.Context) *cobra.Command {
var listCmd = &cobra.Command{
Use: "list",
Short: "List all migrations",
Expand All @@ -46,6 +46,10 @@ func (c *Cli) Execute(ctx context.Context) error {
},
}

return listCmd
}

func (c *Cli) MigrateCommand(ctx context.Context) *cobra.Command {
var migrateCmd = &cobra.Command{
Use: "migrate",
Short: "Run all pending migrations",
Expand Down Expand Up @@ -78,6 +82,10 @@ func (c *Cli) Execute(ctx context.Context) error {

migrateCmd.Flags().BoolP("fresh", "f", false, "Run fresh migrations")

return migrateCmd
}

func (c *Cli) RollbackCommand(ctx context.Context) *cobra.Command {
var rollbackCmd = &cobra.Command{
Use: "rollback",
Short: "Rollback the last migration",
Expand Down Expand Up @@ -108,6 +116,10 @@ func (c *Cli) Execute(ctx context.Context) error {

rollbackCmd.Flags().IntP("step", "s", 1, "Number of migrations to rollback")

return rollbackCmd
}

func (c *Cli) ResetCommand(ctx context.Context) *cobra.Command {
var resetCmd = &cobra.Command{
Use: "reset",
Short: "Rollback all migrations and re-run all migrations",
Expand All @@ -119,6 +131,11 @@ func (c *Cli) Execute(ctx context.Context) error {
}
},
}

return resetCmd
}

func (c *Cli) CleanCommand(ctx context.Context) *cobra.Command {
var cleanCmd = &cobra.Command{
Use: "clean",
Short: "Clean database (delete all tables)",
Expand All @@ -131,6 +148,10 @@ func (c *Cli) Execute(ctx context.Context) error {
},
}

return cleanCmd
}

func (c *Cli) CreateCommand(ctx context.Context) *cobra.Command {
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a new migration",
Expand All @@ -145,6 +166,10 @@ func (c *Cli) Execute(ctx context.Context) error {
},
}

return createCmd
}

func (c *Cli) Execute(ctx context.Context) error {
var rootCmd = &cobra.Command{
Use: c.cliName,
CompletionOptions: cobra.CompletionOptions{
Expand All @@ -157,12 +182,12 @@ func (c *Cli) Execute(ctx context.Context) error {
}

rootCmd.AddCommand(
listCmd,
migrateCmd,
rollbackCmd,
resetCmd,
cleanCmd,
createCmd,
c.ListCommand(ctx),
c.MigrateCommand(ctx),
c.RollbackCommand(ctx),
c.ResetCommand(ctx),
c.CleanCommand(ctx),
c.CreateCommand(ctx),
)

return rootCmd.Execute()
Expand Down