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

This commit is contained in:
Gregory Ballantine
2023-11-28 14:40:12 -05:00
parent 19670e9abd
commit bae65994f8
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()
}