Split migrate and downgrade commands

Rename Pending to GetPending
This commit is contained in:
Michał 2024-05-07 12:47:44 +01:00
parent 2bde65c5a2
commit b8d9ded81e
4 changed files with 73 additions and 43 deletions

View file

@ -16,15 +16,18 @@ func Parse() {
run(os.Args[2:])
case "migrate":
migrate(os.Args[2:])
case "downgrade":
downgrade(os.Args[2:])
case "status":
status(os.Args[2:])
case "-h":
fallthrough
case "-help":
fmt.Println("Available commands are:")
fmt.Println(" run: starts the server")
fmt.Println(" migrate: migrates database")
fmt.Println(" status: checks if there are pending migrations")
fmt.Println(" run: starts the server")
fmt.Println(" migrate: migrates database")
fmt.Println(" downgrade: undoes database migrations")
fmt.Println(" status: checks if there are pending migrations")
fmt.Println("\nTo specific usages, run commandName -h")
default:
fmt.Println("Use -h or -help for usage")

View file

@ -11,7 +11,6 @@ import (
func migrate(flags []string) {
cmd := flag.NewFlagSet("migrate", flag.ExitOnError)
downgrade := cmd.Bool("downgrade", false, "Downgrade Database")
confirm := cmd.Bool("y", false, "Skip questioning")
err := cmd.Parse(flags)
@ -20,7 +19,25 @@ func migrate(flags []string) {
os.Exit(1)
}
err = db.Migrate(*downgrade, *confirm)
err = db.Migrate(*confirm)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
func downgrade(flags []string) {
cmd := flag.NewFlagSet("migrate", flag.ExitOnError)
confirm := cmd.Bool("y", false, "Skip questioning")
err := cmd.Parse(flags)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
err = db.Downgrade(*confirm)
if err != nil {
fmt.Println(err)
os.Exit(1)

View file

@ -23,7 +23,7 @@ func run(flags []string) {
}
if !*skip {
pending, err := db.Pending()
pending, err := db.GetPending()
if err != nil {
fmt.Println(err)
os.Exit(1)

View file

@ -17,73 +17,83 @@ var migrations = migrate.EmbedFileSystemMigrationSource{
Root: "migrations",
}
func Migrate(downgrade, confirm bool) error {
var direction = migrate.Down
if !downgrade {
direction = migrate.Up
}
func Migrate(confirm bool) error {
var direction = migrate.Up
var pending int
if !downgrade {
n, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
if err != nil {
return err
}
pending = len(n)
} else {
n, err := migrate.GetMigrationRecords(Conn, "sqlite3")
if err != nil {
return err
}
pending = len(n)
pending, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
if err != nil {
return err
}
if pending == 0 {
if len(pending) == 0 {
fmt.Println("Nothing to change")
os.Exit(0)
}
if !confirm {
if downgrade {
fmt.Printf("Downgrade %d migrations? [Y/n] ", pending)
} else {
fmt.Printf("Apply %d pending migrations? [Y/n] ", pending)
}
fmt.Printf("Apply %d pending migration(s)? [Y/n] ", len(pending))
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
// Format input
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
input = strings.ToLower(input)
if !strings.HasPrefix(input, "y") && input != "" {
fmt.Println("Canceling migration")
fmt.Printf("Canceling %d migration(s)\n", len(pending))
os.Exit(0)
}
}
n, err := migrate.Exec(Conn, "sqlite3", migrations, direction)
changes, err := migrate.Exec(Conn, "sqlite3", migrations, direction)
if err != nil {
return err
}
if downgrade {
fmt.Printf("Downgraded %d migrations!\n", n)
} else {
fmt.Printf("Applied %d migrations!\n", n)
fmt.Printf("Applied %d migration(s)!\n", changes)
return nil
}
func Downgrade(confirm bool) error {
var direction = migrate.Down
pending, err := migrate.GetMigrationRecords(Conn, "sqlite3")
if err != nil {
return err
}
if len(pending) == 0 {
fmt.Println("Nothing to change")
os.Exit(0)
}
if !confirm {
fmt.Printf("Undo %d migration(s)? [Y/n] ", len(pending))
reader := bufio.NewReader(os.Stdin)
input, _ := reader.ReadString('\n')
input = strings.TrimSpace(input)
input = strings.ToLower(input)
if !strings.HasPrefix(input, "y") && input != "" {
fmt.Printf("Canceling %d downgrade(s)\n", len(pending))
os.Exit(0)
}
}
changes, err := migrate.Exec(Conn, "sqlite3", migrations, direction)
if err != nil {
return err
}
fmt.Printf("Undone %d migration(s)!\n", changes)
return nil
}
func Status() error {
// Get the list of applied migrations
applied, err := migrate.GetMigrationRecords(Conn, "sqlite3")
if err != nil {
return err
}
// Get the list of migrations that are yet to be applied
pending, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
if err != nil {
return err
@ -104,7 +114,7 @@ func Status() error {
return nil
}
func Pending() (bool, error) {
func GetPending() (bool, error) {
planned, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
if err != nil {
return false, err