mirror of
https://github.com/Fluffy-Bean/TastyBites.git
synced 2025-05-28 06:13:12 +00:00
Start building out the backend with database
This commit is contained in:
parent
17476d3f2c
commit
677b4277a6
11 changed files with 241 additions and 63 deletions
58
cmd/database.go
Normal file
58
cmd/database.go
Normal file
|
@ -0,0 +1,58 @@
|
|||
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")
|
||||
|
||||
err := cmd.Parse(flags)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue