blt/models/init.go

33 lines
596 B
Go
Raw Normal View History

2023-11-27 22:36:58 -05:00
package models
import (
"log"
"gorm.io/gorm"
"gorm.io/driver/sqlite"
)
var (
DB *gorm.DB
)
// Initializes the Gorm database object
func Open() {
2023-11-27 22:36:58 -05:00
// Initialize database object
var err error
DB, err = gorm.Open(sqlite.Open("data/blt.db"), &gorm.Config{})
2023-11-27 22:36:58 -05:00
if err != nil {
panic("Failed to connect database")
}
// Migrate the schema
DB.AutoMigrate(&Project{}, &Hardware{}, &Benchmark{})
2023-11-27 22:36:58 -05:00
log.Println("Database migrations complete.")
}
// Used to close the database connection; should be only called in main() as a deferral
func Close() error {
return DB.Close()
}