bill-server-go/internal/infrastructure/sqlite.go

31 lines
620 B
Go
Raw Normal View History

package infrastructure
import (
2023-06-01 19:38:07 +08:00
"bill-go-fiber/internal/bill"
"bill-go-fiber/internal/label"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
func ConnectToSqlite() (*gorm.DB, error) {
2023-06-01 17:55:41 +08:00
dsn := "bill.db"
//dsn := "file::memory:?cache=shared"
db, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{})
if err != nil {
2023-06-01 17:55:41 +08:00
panic("failed to connect database.\n")
}
2023-06-01 19:38:07 +08:00
err = db.AutoMigrate(&label.Label{})
if err != nil {
panic("failed to migrate database.\n")
}
err = db.AutoMigrate(&bill.Bill{})
if err != nil {
panic("failed to migrate database.\n")
}
db.Create(&label.Label{ID: 1, Name: "bill"})
return db, nil
}