mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2024-12-26 17:36:15 +00:00
Improve migrate command
This commit is contained in:
parent
2f40cdcd64
commit
2bde65c5a2
|
@ -12,6 +12,7 @@ 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)
|
||||
if err != nil {
|
||||
|
@ -19,7 +20,7 @@ func migrate(flags []string) {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
err = db.Migrate(!*downgrade)
|
||||
err = db.Migrate(*downgrade, *confirm)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
|
|
|
@ -1,10 +1,16 @@
|
|||
|
||||
-- +migrate Up
|
||||
CREATE TABLE IF NOT EXISTS Item (
|
||||
uuid TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
price INTEGER NOT NULL,
|
||||
description TEXT
|
||||
item_uuid TEXT NOT NULL PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
price INTEGER NOT NULL,
|
||||
description TEXT
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS Images (
|
||||
image_uuid TEXT NOT NULL PRIMARY KEY,
|
||||
show BOOLEAN NOT NULL,
|
||||
item_uuid TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- +migrate Down
|
||||
|
|
|
@ -1,8 +1,11 @@
|
|||
package database
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"embed"
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
migrate "github.com/rubenv/sql-migrate"
|
||||
)
|
||||
|
@ -14,12 +17,50 @@ var migrations = migrate.EmbedFileSystemMigrationSource{
|
|||
Root: "migrations",
|
||||
}
|
||||
|
||||
func Migrate(Up bool) error {
|
||||
var direction migrate.MigrationDirection
|
||||
if Up {
|
||||
func Migrate(downgrade, confirm bool) error {
|
||||
var direction = migrate.Down
|
||||
if !downgrade {
|
||||
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 {
|
||||
direction = migrate.Down
|
||||
n, err := migrate.GetMigrationRecords(Conn, "sqlite3")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
pending = len(n)
|
||||
}
|
||||
|
||||
if 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)
|
||||
}
|
||||
|
||||
reader := bufio.NewReader(os.Stdin)
|
||||
input, _ := reader.ReadString('\n')
|
||||
|
||||
// Format input
|
||||
input = strings.TrimSpace(input)
|
||||
input = strings.ToLower(input)
|
||||
|
||||
if !strings.HasPrefix(input, "y") && input != "" {
|
||||
fmt.Println("Canceling migration")
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
n, err := migrate.Exec(Conn, "sqlite3", migrations, direction)
|
||||
|
@ -27,7 +68,11 @@ func Migrate(Up bool) error {
|
|||
return err
|
||||
}
|
||||
|
||||
fmt.Printf("Applied %d migrations!\n", n)
|
||||
if downgrade {
|
||||
fmt.Printf("Downgraded %d migrations!\n", n)
|
||||
} else {
|
||||
fmt.Printf("Applied %d migrations!\n", n)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -39,17 +84,21 @@ func Status() error {
|
|||
}
|
||||
|
||||
// Get the list of migrations that are yet to be applied
|
||||
planned, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
||||
pending, _, err := migrate.PlanMigration(Conn, "sqlite3", migrations, migrate.Up, 0)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
fmt.Println("Available migrations:")
|
||||
for _, migration := range applied {
|
||||
fmt.Printf(" Complete: %s\n", migration.Id)
|
||||
fmt.Printf(" [x] %s\n", migration.Id)
|
||||
}
|
||||
for _, migration := range planned {
|
||||
fmt.Printf(" Pending: %s\n", migration.Id)
|
||||
for _, migration := range pending {
|
||||
fmt.Printf(" [ ] %s\n", migration.Id)
|
||||
}
|
||||
|
||||
if len(pending) > 0 {
|
||||
fmt.Printf("\nTo apply %d pending migrations, run `TastyBites migrate`\n", len(pending))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
|
Loading…
Reference in a new issue