2023-05-31 18:00:21 +08:00
|
|
|
package infrastructure
|
|
|
|
|
|
|
|
import (
|
2023-06-01 19:38:07 +08:00
|
|
|
"bill-go-fiber/internal/bill"
|
|
|
|
"bill-go-fiber/internal/label"
|
2023-05-31 18:00:21 +08:00
|
|
|
"gorm.io/driver/sqlite"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ConnectToSqlite() (*gorm.DB, error) {
|
2023-06-01 17:55:41 +08:00
|
|
|
dsn := "bill.db"
|
2023-05-31 18:00:21 +08:00
|
|
|
//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-05-31 18:00:21 +08:00
|
|
|
}
|
|
|
|
|
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"})
|
|
|
|
|
2023-05-31 18:00:21 +08:00
|
|
|
return db, nil
|
|
|
|
}
|