Skip to content

Commit

Permalink
any flags doesn't work with commands up down etc (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
raoptimus committed Dec 2, 2024
1 parent efdef36 commit eaefd7d
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 107 deletions.
100 changes: 44 additions & 56 deletions cmd/db-migrator/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
package main

import (
"context"
"fmt"
"os"
"slices"

_ "github.com/lib/pq"
"github.com/raoptimus/db-migrator.go/internal/migrator"
"github.com/raoptimus/db-migrator.go/pkg/console"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

var (
Expand All @@ -28,152 +28,140 @@ var (
func main() {
options := migrator.Options{}

app := cli.NewApp()
app.Name = "DB Service"
app.Usage = "up/down/redo command for migrates the different db"
app.Version = fmt.Sprintf("%s.rev[%s]", Version, GitCommit)
app.Commands = commands(&options)
app.Before = func(context *cli.Context) error {
dbService = migrator.New(&options)
return nil
cmd := &cli.Command{
Name: "DB Service",
Usage: "up/down/redo command for migrates the different db",
Version: fmt.Sprintf("%s.rev[%s]", Version, GitCommit),
Commands: commands(),
DefaultCommand: "help",
Flags: flags(&options),
Before: func(ctx context.Context, cmd *cli.Command) (context.Context, error) {
dbService = migrator.New(&options)

return ctx, nil
},
}

if err := app.Run(os.Args); err != nil {
if err := cmd.Run(context.Background(), os.Args); err != nil {
console.Std.Fatal(err)
}
}

func commands(options *migrator.Options) []*cli.Command {
defaultFlags := flags(options)
allFlags := slices.Concat(defaultFlags, addsFlags(options))

func commands() []*cli.Command {
return []*cli.Command{
{
Name: "up",
Flags: allFlags,
Action: func(ctx *cli.Context) error {
Name: "up",
Action: func(ctx context.Context, cmd *cli.Command) error {
if a, err := dbService.Upgrade(); err != nil {
return err
} else {
return a.Run(ctx)
return a.Run(ctx, cmd.Args())
}
},
},
{
Name: "down",
Flags: allFlags,
Action: func(ctx *cli.Context) error {
Name: "down",
Action: func(ctx context.Context, cmd *cli.Command) error {
if a, err := dbService.Downgrade(); err != nil {
return err
} else {
return a.Run(ctx)
return a.Run(ctx, cmd.Args())
}
},
},
{
Name: "redo",
Flags: allFlags,
Action: func(ctx *cli.Context) error {
Name: "redo",
Action: func(ctx context.Context, cmd *cli.Command) error {
if a, err := dbService.Redo(); err != nil {
return err
} else {
return a.Run(ctx)
return a.Run(ctx, cmd.Args())
}
},
},
{
Name: "create",
Flags: defaultFlags,
Action: func(ctx *cli.Context) error {
return dbService.Create().Run(ctx)
Name: "create",
Action: func(ctx context.Context, cmd *cli.Command) error {
return dbService.Create().Run(ctx, cmd.Args())
},
},
{
Name: "history",
Flags: allFlags,
Action: func(ctx *cli.Context) error {
Name: "history",
Action: func(ctx context.Context, cmd *cli.Command) error {
if a, err := dbService.History(); err != nil {
return err
} else {
return a.Run(ctx)
return a.Run(ctx, cmd.Args())
}
},
},
{
Name: "new",
Flags: allFlags,
Action: func(ctx *cli.Context) error {
Name: "new",
Action: func(ctx context.Context, cmd *cli.Command) error {
if a, err := dbService.HistoryNew(); err != nil {
return err
} else {
return a.Run(ctx)
return a.Run(ctx, cmd.Args())
}
},
},
{
Name: "to",
Flags: allFlags,
Action: func(ctx *cli.Context) error {
Name: "to",
Action: func(ctx context.Context, cmd *cli.Command) error {
if a, err := dbService.To(); err != nil {
return err
} else {
return a.Run(ctx)
return a.Run(ctx, cmd.Args())
}
},
},
}
}

func addsFlags(options *migrator.Options) []cli.Flag {
func flags(options *migrator.Options) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "dsn",
EnvVars: []string{"DSN"},
Sources: cli.EnvVars("DSN"),
Aliases: []string{"d"},
Usage: "DB connection string",
Destination: &options.DSN,
Required: true,
},
}
}

func flags(options *migrator.Options) []cli.Flag {
return []cli.Flag{
&cli.StringFlag{
Name: "migrationPath",
EnvVars: []string{"MIGRATION_PATH"},
Sources: cli.EnvVars("MIGRATION_PATH"),
Aliases: []string{"p"},
Value: "./migrations",
Usage: "Directory for migrated files",
Destination: &options.Directory,
},
&cli.StringFlag{
Name: "migrationTable",
EnvVars: []string{"MIGRATION_TABLE"},
Sources: cli.EnvVars("MIGRATION_TABLE"),
Aliases: []string{"t"},
Value: "migration",
Usage: "Table name for history of migrates",
Destination: &options.TableName,
},
&cli.StringFlag{
Name: "migrationClusterName",
EnvVars: []string{"MIGRATION_CLUSTER_NAME"},
Sources: cli.EnvVars("MIGRATION_CLUSTER_NAME"),
Aliases: []string{"cn"},
Value: "",
Usage: "Cluster name for history of migrates",
Destination: &options.ClusterName,
},
&cli.BoolFlag{
Name: "compact",
EnvVars: []string{"COMPACT"},
Sources: cli.EnvVars("COMPACT"),
Aliases: []string{"c"},
Usage: "Indicates whether the console output should be compacted.",
Value: false,
Destination: &options.Compact,
},
&cli.BoolFlag{
Name: "interactive",
EnvVars: []string{"INTERACTIVE"},
Sources: cli.EnvVars("INTERACTIVE"),
Aliases: []string{"i"},
Usage: "Whether to run the command interactively",
Value: true,
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ require (
github.com/raoptimus/db-migrator.go/pkg/timex v0.0.0-20240503100150-5445e535daaf
github.com/stretchr/testify v1.10.0
github.com/urfave/cli/v2 v2.27.5
github.com/urfave/cli/v3 v3.0.0-beta1
)

require (
Expand Down
10 changes: 2 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ github.com/ClickHouse/clickhouse-go/v2 v2.30.0 h1:AG4D/hW39qa58+JHQIFOSnxyL46H6h
github.com/ClickHouse/clickhouse-go/v2 v2.30.0/go.mod h1:i9ZQAojcayW3RsdCb3YR+n+wC2h65eJsZCscZ1Z1wyo=
github.com/andybalholm/brotli v1.1.1 h1:PR2pgnyFznKEugtsUo0xLdDop5SKXd5Qf5ysW+7XdTA=
github.com/andybalholm/brotli v1.1.1/go.mod h1:05ib4cKhjx3OQYUY22hTVd34Bc8upXjOLL2rKwwZBoA=
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/cpuguy83/go-md2man/v2 v2.0.5 h1:ZtcqGrnekaHpVLArFSe4HK5DoKx1T0rq2DwVB0alcyc=
github.com/cpuguy83/go-md2man/v2 v2.0.5/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down Expand Up @@ -73,20 +71,16 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/urfave/cli/v2 v2.27.2 h1:6e0H+AkS+zDckwPCUrZkKX38mRaau4nL2uipkJpbkcI=
github.com/urfave/cli/v2 v2.27.2/go.mod h1:g0+79LmHHATl7DAcHO99smiR/T7uGLw84w8Y42x+4eM=
github.com/urfave/cli/v2 v2.27.5 h1:WoHEJLdsXr6dDWoJgMq/CboDmyY/8HMMH1fTECbih+w=
github.com/urfave/cli/v2 v2.27.5/go.mod h1:3Sevf16NykTbInEnD0yKkjDAeZDS0A6bzhBH5hrMvTQ=
github.com/urfave/cli/v3 v3.0.0-beta1 h1:6DTaaUarcM0wX7qj5Hcvs+5Dm3dyUTBbEwIWAjcw9Zg=
github.com/urfave/cli/v3 v3.0.0-beta1/go.mod h1:FnIeEMYu+ko8zP1F9Ypr3xkZMIDqW3DR92yUtY39q1Y=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.1/go.mod h1:RaEWvsqvNKKvBPvcKeFjrG2cJqOkHTiyTpzz23ni57g=
github.com/xdg-go/stringprep v1.0.3/go.mod h1:W3f5j4i+9rC0kuIEJL0ky1VpHXQU3ocBgklLGvcBnW8=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw=
github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1 h1:gEOO8jv9F4OT7lGCjxCBTO/36wtF6j2nSip77qHd4x4=
github.com/xrash/smetrics v0.0.0-20240521201337-686a1a2994c1/go.mod h1:Ohn+xnUBiLI6FVj/9LpzZWtj1/D6lUovWYBkxHVV3aM=
github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZqKjWU=
Expand Down
2 changes: 1 addition & 1 deletion internal/action/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"time"

"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

var time230527213123 = time.Date(
Expand Down
7 changes: 4 additions & 3 deletions internal/action/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
package action

import (
"context"
"fmt"
"os"
"regexp"

"github.com/pkg/errors"
"github.com/raoptimus/db-migrator.go/pkg/timex"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

const fileModeExecutable = 0o755
Expand Down Expand Up @@ -46,8 +47,8 @@ func NewCreate(
}
}

func (c *Create) Run(ctx *cli.Context) error {
migrationName := ctx.Args().Get(0)
func (c *Create) Run(_ context.Context, cmdArgs cli.Args) error {
migrationName := cmdArgs.Get(0)
if !regexpFileName.MatchString(migrationName) {
return ErrInvalidFileName
}
Expand Down
11 changes: 6 additions & 5 deletions internal/action/downgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
package action

import (
"context"
"fmt"

"github.com/raoptimus/db-migrator.go/internal/args"
"github.com/raoptimus/db-migrator.go/internal/console"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

type Downgrade struct {
Expand All @@ -34,13 +35,13 @@ func NewDowngrade(
}
}

func (d *Downgrade) Run(ctx *cli.Context) error {
limit, err := args.ParseStepStringOrDefault(ctx.Args().Get(0), minLimit)
func (d *Downgrade) Run(ctx context.Context, cmdArgs cli.Args) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs.Get(0), minLimit)
if err != nil {
return err
}

migrations, err := d.service.Migrations(ctx.Context, limit)
migrations, err := d.service.Migrations(ctx, limit)
if err != nil {
return err
}
Expand Down Expand Up @@ -72,7 +73,7 @@ func (d *Downgrade) Run(ctx *cli.Context) error {
migration := &migrations[i]
fileName, safely := d.fileNameBuilder.Down(migration.Version, false)

if err := d.service.RevertFile(ctx.Context, migration, fileName, safely); err != nil {
if err := d.service.RevertFile(ctx, migration, fileName, safely); err != nil {
console.Errorf(
"%d from %d %s reverted.\n"+
"Migration failed. The rest of the migrations are canceled.\n",
Expand Down
10 changes: 6 additions & 4 deletions internal/action/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
package action

import (
"context"

"github.com/raoptimus/db-migrator.go/internal/args"
"github.com/raoptimus/db-migrator.go/internal/console"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

const defaultGetHistoryLimit = 10
Expand All @@ -28,13 +30,13 @@ func NewHistory(
}
}

func (h *History) Run(ctx *cli.Context) error {
limit, err := args.ParseStepStringOrDefault(ctx.Args().Get(0), defaultGetHistoryLimit)
func (h *History) Run(ctx context.Context, cmdArgs cli.Args) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs.Get(0), defaultGetHistoryLimit)
if err != nil {
return err
}

migrations, err := h.service.Migrations(ctx.Context, limit)
migrations, err := h.service.Migrations(ctx, limit)
if err != nil {
return err
}
Expand Down
10 changes: 6 additions & 4 deletions internal/action/history_new.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
package action

import (
"context"

"github.com/raoptimus/db-migrator.go/internal/args"
"github.com/raoptimus/db-migrator.go/internal/console"
"github.com/urfave/cli/v2"
"github.com/urfave/cli/v3"
)

type HistoryNew struct {
Expand All @@ -26,13 +28,13 @@ func NewHistoryNew(
}
}

func (h *HistoryNew) Run(ctx *cli.Context) error {
limit, err := args.ParseStepStringOrDefault(ctx.Args().Get(0), defaultGetHistoryLimit)
func (h *HistoryNew) Run(ctx context.Context, cmdArgs cli.Args) error {
limit, err := args.ParseStepStringOrDefault(cmdArgs.Get(0), defaultGetHistoryLimit)
if err != nil {
return err
}

migrations, err := h.service.NewMigrations(ctx.Context)
migrations, err := h.service.NewMigrations(ctx)
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit eaefd7d

Please sign in to comment.