bouncer/internal/command/database/reset.go

39 lines
875 B
Go

package database
import (
"forge.cadoles.com/cadoles/bouncer/internal/command/common"
"forge.cadoles.com/cadoles/bouncer/internal/migrate"
"github.com/pkg/errors"
"github.com/urfave/cli/v2"
"gitlab.com/wpetit/goweb/logger"
)
func ResetCommand() *cli.Command {
return &cli.Command{
Name: "reset",
Usage: "Reset database",
Action: func(ctx *cli.Context) error {
conf, err := common.LoadConfig(ctx)
if err != nil {
return errors.Wrap(err, "Could not load configuration")
}
driver := string(conf.Database.Driver)
dsn := string(conf.Database.DSN)
migr, err := migrate.New("migrations", driver, dsn)
if err != nil {
return errors.WithStack(err)
}
if err := migr.Drop(); err != nil {
return errors.Wrap(err, "could not drop tables")
}
logger.Info(ctx.Context, "database schema reinitialized")
return nil
},
}
}