mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-05-24 04:14:55 +00:00
Improve migrate command
This commit is contained in:
parent
2f40cdcd64
commit
2bde65c5a2
3 changed files with 70 additions and 14 deletions
|
@ -12,6 +12,7 @@ func migrate(flags []string) {
|
||||||
cmd := flag.NewFlagSet("migrate", flag.ExitOnError)
|
cmd := flag.NewFlagSet("migrate", flag.ExitOnError)
|
||||||
|
|
||||||
downgrade := cmd.Bool("downgrade", false, "Downgrade Database")
|
downgrade := cmd.Bool("downgrade", false, "Downgrade Database")
|
||||||
|
confirm := cmd.Bool("y", false, "Skip questioning")
|
||||||
|
|
||||||
err := cmd.Parse(flags)
|
err := cmd.Parse(flags)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -19,7 +20,7 @@ func migrate(flags []string) {
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
err = db.Migrate(!*downgrade)
|
err = db.Migrate(*downgrade, *confirm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Println(err)
|
fmt.Println(err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
|
|
|
@ -1,11 +1,17 @@
|
||||||
|
|
||||||
-- +migrate Up
|
-- +migrate Up
|
||||||
CREATE TABLE IF NOT EXISTS Item (
|
CREATE TABLE IF NOT EXISTS Item (
|
||||||
uuid TEXT NOT NULL PRIMARY KEY,
|
item_uuid TEXT NOT NULL PRIMARY KEY,
|
||||||
name TEXT NOT NULL,
|
name TEXT NOT NULL,
|
||||||
price INTEGER NOT NULL,
|
price INTEGER NOT NULL,
|
||||||
description TEXT
|
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
|
-- +migrate Down
|
||||||
DROP TABLE Item;
|
DROP TABLE Item;
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
migrate "github.com/rubenv/sql-migrate"
|
migrate "github.com/rubenv/sql-migrate"
|
||||||
)
|
)
|
||||||
|
@ -14,12 +17,50 @@ var migrations = migrate.EmbedFileSystemMigrationSource{
|
||||||
Root: "migrations",
|
Root: "migrations",
|
||||||
}
|
}
|
||||||
|
|
||||||
func Migrate(Up bool) error {
|
func Migrate(downgrade, confirm bool) error {
|
||||||
var direction migrate.MigrationDirection
|
var direction = migrate.Down
|
||||||
if Up {
|
if !downgrade {
|
||||||
direction = migrate.Up
|
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 {
|
} 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)
|
n, err := migrate.Exec(Conn, "sqlite3", migrations, direction)
|
||||||
|
@ -27,7 +68,11 @@ func Migrate(Up bool) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if downgrade {
|
||||||
|
fmt.Printf("Downgraded %d migrations!\n", n)
|
||||||
|
} else {
|
||||||
fmt.Printf("Applied %d migrations!\n", n)
|
fmt.Printf("Applied %d migrations!\n", n)
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -39,17 +84,21 @@ func Status() error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the list of migrations that are yet to be applied
|
// 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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println("Available migrations:")
|
fmt.Println("Available migrations:")
|
||||||
for _, migration := range applied {
|
for _, migration := range applied {
|
||||||
fmt.Printf(" Complete: %s\n", migration.Id)
|
fmt.Printf(" [x] %s\n", migration.Id)
|
||||||
}
|
}
|
||||||
for _, migration := range planned {
|
for _, migration := range pending {
|
||||||
fmt.Printf(" Pending: %s\n", migration.Id)
|
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
|
return nil
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue