Refactored models code to follow a better approach for opening and closing the database connection
This commit is contained in:
@ -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()
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user