mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-05-21 02:44:55 +00:00
Add migration, status and pending checks to database
This commit is contained in:
parent
b2933a41c1
commit
66fb03fa0d
9 changed files with 146 additions and 42 deletions
|
@ -3,19 +3,15 @@ package cmd
|
|||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
|
||||
db "github.com/Fluffy-Bean/TastyBites/database"
|
||||
sb "github.com/huandu/go-sqlbuilder"
|
||||
)
|
||||
|
||||
func migrate(flags []string) {
|
||||
|
||||
cmd := flag.NewFlagSet("migrate", flag.ExitOnError)
|
||||
|
||||
tables := cmd.Bool("tables", false, "Create tables")
|
||||
verbose := cmd.Bool("v", false, "Verbose")
|
||||
downgrade := cmd.Bool("downgrade", false, "Downgrade Database")
|
||||
|
||||
err := cmd.Parse(flags)
|
||||
if err != nil {
|
||||
|
@ -23,36 +19,17 @@ func migrate(flags []string) {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
if !*tables {
|
||||
builder := db.ItemStruct.InsertInto("Item")
|
||||
for _, item := range db.SampleData {
|
||||
builder.Values(item.UUID, item.Name, item.Price, item.Description)
|
||||
}
|
||||
query, args := builder.Build()
|
||||
|
||||
_, err = db.Conn.Exec(query, args...)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
fmt.Println("Success")
|
||||
}
|
||||
} else {
|
||||
itemTable := sb.CreateTable("Item").IfNotExists()
|
||||
itemTable.Define("uuid", "TEXT", "NOT NULL", "PRIMARY KEY")
|
||||
itemTable.Define("name", "TEXT", "NOT NULL")
|
||||
itemTable.Define("price", "INTEGER", "NOT NULL")
|
||||
itemTable.Define("description", "TEXT")
|
||||
query, args := itemTable.BuildWithFlavor(sb.SQLite)
|
||||
|
||||
if *verbose {
|
||||
fmt.Println(query, args)
|
||||
}
|
||||
|
||||
_, err := db.Conn.Exec(query, args...)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
fmt.Println("Success")
|
||||
}
|
||||
err = db.Migrate(!*downgrade)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
||||
func status(flags []string) {
|
||||
err := db.Status()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue