Skip to content

Commit

Permalink
Add ignore-unknown option
Browse files Browse the repository at this point in the history
  • Loading branch information
montrayqc authored and snovichkov committed Sep 7, 2023
1 parent 29e2587 commit 88c900d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
4 changes: 3 additions & 1 deletion internal/command/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import (

// NewMigrateConstructor is command constructor getter.
func NewMigrateConstructor(
path, table, schema, dialect, connection string,
path, table, schema, dialect, connection string, ignoreUnknown bool,
) func(subcommands []*cobra.Command) *cobra.Command {
migrate.SetTable(table)
migrate.SetSchema(schema)
migrate.SetIgnoreUnknown(ignoreUnknown)

return func(subcommands []*cobra.Command) *cobra.Command {
var cmd = &cobra.Command{
Expand All @@ -27,6 +28,7 @@ func NewMigrateConstructor(
cmd.PersistentFlags().String("path", path, "Path to migrations")
cmd.PersistentFlags().String("dialect", dialect, "Dialect name")
cmd.PersistentFlags().String("connection", connection, "Connection name")
cmd.PersistentFlags().Bool("ignore-unknown", ignoreUnknown, "Ignore unknown migrations")

return cmd
}
Expand Down
7 changes: 7 additions & 0 deletions internal/command/migrate_down.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ func NewMigrateDown(ctn di.Container) *cobra.Command {
dialect = driver
}

var ignoreUnknown bool
if ignoreUnknown, err = cmd.Flags().GetBool("ignore-unknown"); err != nil {
return err
}

migrate.SetIgnoreUnknown(ignoreUnknown)

var n int
if n, err = migrate.ExecMax(
db.Master(),
Expand Down
7 changes: 7 additions & 0 deletions internal/command/migrate_up.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ func NewMigrateUp(ctn di.Container) *cobra.Command {
dialect = driver
}

var ignoreUnknown bool
if ignoreUnknown, err = cmd.Flags().GetBool("ignore-unknown"); err != nil {
return err
}

migrate.SetIgnoreUnknown(ignoreUnknown)

var n int
if n, err = migrate.Exec(
db.Master(),
Expand Down
20 changes: 14 additions & 6 deletions migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ import (
type (
// Bundle implements the glue.Bundle interface.
Bundle struct {
path string
table string
schema string
dialect string
connection string
path string
table string
schema string
dialect string
connection string
ignoreUnknown bool
}

// Option interface.
Expand Down Expand Up @@ -73,6 +74,13 @@ func Schema(value string) Option {
})
}

// IgnoreUnknown option
func IgnoreUnknown(value bool) Option {
return optionFunc(func(b *Bundle) {
b.ignoreUnknown = value
})
}

// NewBundle create bundle instance.
func NewBundle(options ...Option) (b *Bundle) {
b = &Bundle{
Expand All @@ -99,7 +107,7 @@ func (b *Bundle) Build(builder di.Builder) error {

return builder.Apply(
di.Provide(
command.NewMigrateConstructor(b.path, b.table, b.schema, b.dialect, b.connection),
command.NewMigrateConstructor(b.path, b.table, b.schema, b.dialect, b.connection, b.ignoreUnknown),
di.Constraint(0, di.WithTags(tag)),
glue.AsCliCommand(),
),
Expand Down

0 comments on commit 88c900d

Please sign in to comment.