Refactored models code to follow a better approach for opening and closing the database connection

This commit is contained in:
2025-07-14 13:53:50 -04:00
parent 0f89087134
commit c9ad5df2ed
4 changed files with 42 additions and 7 deletions

View File

@ -11,9 +11,11 @@ var (
DB *gorm.DB
)
func InitDatabase() {
// Initializes the Gorm database object
func Open() {
// Initialize database object
DB, err := gorm.Open(sqlite.Open("data/blt.db"), &gorm.Config{})
var err error
DB, err = gorm.Open(sqlite.Open("data/blt.db"), &gorm.Config{})
if err != nil {
panic("Failed to connect database")
}
@ -22,3 +24,9 @@ func InitDatabase() {
DB.AutoMigrate(&Project{}, &Hardware{}, &Benchmark{})
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()
}