-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.go
More file actions
33 lines (26 loc) · 905 Bytes
/
Copy pathdatabase.go
File metadata and controls
33 lines (26 loc) · 905 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
import (
"errors"
"log"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
// setupDatabase initializes the database connection and runs migrations
func setupDatabase() *gorm.DB {
db, err := gorm.Open(sqlite.Open("weather.db"), &gorm.Config{})
if err != nil {
log.Fatal("failed to connect database", err)
}
db.AutoMigrate(&WeatherReport{}, &DeviceModel{}, &OllamaRecommendation{})
return db
}
// checkForDeviceModel checks whether the device model exists in the database, if it doesn't, adds it
func checkForDeviceModel(db *gorm.DB, weatherReport WeatherReport) {
var deviceModelInfo DeviceModel
deviceModelInfo.DeviceModel = weatherReport.DeviceModel
deviceModelInfo.Name = weatherReport.DeviceModel
result := db.Where("device_model = ?", weatherReport.DeviceModel).First(&deviceModelInfo)
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
db.Create(&deviceModelInfo)
}
}