mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-05-28 22:33:12 +00:00
Split migrate and downgrade commands
Rename Pending to GetPending
This commit is contained in:
parent
2bde65c5a2
commit
b8d9ded81e
4 changed files with 73 additions and 43 deletions
|
@ -16,15 +16,18 @@ func Parse() {
|
||||||
run(os.Args[2:])
|
run(os.Args[2:])
|
||||||
case "migrate":
|
case "migrate":
|
||||||
migrate(os.Args[2:])
|
migrate(os.Args[2:])
|
||||||
|
case "downgrade":
|
||||||
|
downgrade(os.Args[2:])
|
||||||
case "status":
|
case "status":
|
||||||
status(os.Args[2:])
|
status(os.Args[2:])
|
||||||
case "-h":
|
case "-h":
|
||||||
fallthrough
|
fallthrough
|
||||||
case "-help":
|
case "-help":
|
||||||
fmt.Println("Available commands are:")
|
fmt.Println("Available commands are:")
|
||||||
fmt.Println(" run: starts the server")
|
fmt.Println(" run: starts the server")
|
||||||
fmt.Println(" migrate: migrates database")
|
fmt.Println(" migrate: migrates database")
|
||||||
fmt.Println(" status: checks if there are pending migrations")
|
fmt.Println(" downgrade: undoes database migrations")
|
||||||
|
fmt.Println(" status: checks if there are pending migrations")
|
||||||
fmt.Println("\nTo specific usages, run commandName -h")
|
fmt.Println("\nTo specific usages, run commandName -h")
|
||||||
default:
|
default:
|
||||||
fmt.Println("Use -h or -help for usage")
|
fmt.Println("Use -h or -help for usage")
|
||||||
|
|
|
@ -11,7 +11,6 @@ import (
|
||||||
func migrate(flags []string) {
|
func migrate(flags []string) {
|
||||||
cmd := flag.NewFlagSet("migrate", flag.ExitOnError)
|
cmd := flag.NewFlagSet("migrate", flag.ExitOnError)
|
||||||
|
|
||||||
downgrade := cmd.Bool("downgrade", false, "Downgrade Database")
|
|
||||||
confirm := cmd.Bool("y", false, "Skip questioning")
|
confirm := cmd.Bool("y", false, "Skip questioning")
|
||||||
|
|
||||||
err := cmd.Parse(flags)
|
err := cmd.Parse(flags)
|
||||||
|
@ -20,7 +19,25 @@ func migrate(flags []string) {
|
||||||
os.Exit(1)
|
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 {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -23,7 +23,7 @@ func run(flags []string) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if !*skip {
|
if !*skip {
|
||||||
pending, err := db.Pending()
|
pending, err := db.GetPending()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -17,73 +17,83 @@ var migrations = migrate.EmbedFileSystemMigrationSource{
|
||||||
Root: "migrations",
|
Root: "migrations",
|
||||||
}
|
}
|
||||||
|
|
||||||
func Migrate(downgrade, confirm bool) error {
|
func Migrate(confirm bool) error {
|
||||||
var direction = migrate.Down
|
var direction = migrate.Up
|
||||||
if !downgrade {
|
|
||||||
direction = migrate.Up
|
|
||||||
}
|
|
||||||
|
|
||||||
var pending int
|
pending, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
||||||
if !downgrade {
|
if err != nil {
|
||||||
n, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
return err
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
pending = len(n)
|
|
||||||
} else {
|
|
||||||
n, err := migrate.GetMigrationRecords(Conn, "sqlite3")
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
pending = len(n)
|
|
||||||
}
|
}
|
||||||
|
if len(pending) == 0 {
|
||||||
if pending == 0 {
|
|
||||||
fmt.Println("Nothing to change")
|
fmt.Println("Nothing to change")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
if !confirm {
|
if !confirm {
|
||||||
if downgrade {
|
fmt.Printf("Apply %d pending migration(s)? [Y/n] ", len(pending))
|
||||||
fmt.Printf("Downgrade %d migrations? [Y/n] ", pending)
|
|
||||||
} else {
|
|
||||||
fmt.Printf("Apply %d pending migrations? [Y/n] ", pending)
|
|
||||||
}
|
|
||||||
|
|
||||||
reader := bufio.NewReader(os.Stdin)
|
reader := bufio.NewReader(os.Stdin)
|
||||||
input, _ := reader.ReadString('\n')
|
|
||||||
|
|
||||||
// Format input
|
input, _ := reader.ReadString('\n')
|
||||||
input = strings.TrimSpace(input)
|
input = strings.TrimSpace(input)
|
||||||
input = strings.ToLower(input)
|
input = strings.ToLower(input)
|
||||||
|
|
||||||
if !strings.HasPrefix(input, "y") && input != "" {
|
if !strings.HasPrefix(input, "y") && input != "" {
|
||||||
fmt.Println("Canceling migration")
|
fmt.Printf("Canceling %d migration(s)\n", len(pending))
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
n, err := migrate.Exec(Conn, "sqlite3", migrations, direction)
|
changes, err := migrate.Exec(Conn, "sqlite3", migrations, direction)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if downgrade {
|
fmt.Printf("Applied %d migration(s)!\n", changes)
|
||||||
fmt.Printf("Downgraded %d migrations!\n", n)
|
return nil
|
||||||
} else {
|
}
|
||||||
fmt.Printf("Applied %d migrations!\n", n)
|
|
||||||
|
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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Status() error {
|
func Status() error {
|
||||||
// Get the list of applied migrations
|
|
||||||
applied, err := migrate.GetMigrationRecords(Conn, "sqlite3")
|
applied, err := migrate.GetMigrationRecords(Conn, "sqlite3")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the list of migrations that are yet to be applied
|
|
||||||
pending, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
pending, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -104,7 +114,7 @@ func Status() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Pending() (bool, error) {
|
func GetPending() (bool, error) {
|
||||||
planned, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
planned, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue