2fas-server/cmd/migrate/main.go

89 lines
2.0 KiB
Go
Raw Normal View History

2022-12-31 10:22:38 +01:00
package main
import (
"flag"
"fmt"
2023-01-31 20:27:04 +01:00
"github.com/pressly/goose/v3"
2023-01-30 19:59:42 +01:00
"github.com/twofas/2fas-server/config"
"github.com/twofas/2fas-server/internal/common/db"
2022-12-31 10:22:38 +01:00
"log"
"os"
)
const dialect = "mysql"
var (
flags = flag.NewFlagSet("migrate", flag.ExitOnError)
dir = flags.String("dir", "/migrations", "directory with migration files")
)
func main() {
flags.Usage = usage
flags.Parse(os.Args[1:])
args := flags.Args()
if len(args) == 0 || args[0] == "-h" || args[0] == "--help" {
flags.Usage()
return
}
command := args[0]
switch command {
case "create":
if err := goose.Run("create", nil, *dir, args[1:]...); err != nil {
log.Fatalf("migrate run: %v", err)
}
return
case "fix":
if err := goose.Run("fix", nil, *dir); err != nil {
log.Fatalf("migrate run: %v", err)
}
return
}
config.LoadConfiguration()
appDb := db.NewDbConnection(config.Config)
defer appDb.Close()
if err := goose.SetDialect(dialect); err != nil {
log.Fatal(err)
}
if err := goose.Run(command, appDb, *dir, args[1:]...); err != nil {
log.Fatalf("migrate run: %v", err)
}
}
func usage() {
fmt.Println(usagePrefix)
flags.PrintDefaults()
fmt.Println(usageCommands)
}
var (
usagePrefix = `Usage: migrate [OPTIONS] COMMAND
Examples:
migrate status
Options:
`
usageCommands = `
Commands:
up Migrate the DB to the most recent version available
up-by-one Migrate the DB up by 1
up-to VERSION Migrate the DB to a specific VERSION
down Roll back the version by 1
down-to VERSION Roll back to a specific VERSION
redo Re-run the latest migration
reset Roll back all migrations
status Dump the migration status for the current DB
version Print the current version of the database
create NAME [sql|go] Creates new migration file with the current timestamp
fix Apply sequential ordering to migrations
`
)