2024-02-11 10:56:21 +00:00
|
|
|
package internal
|
|
|
|
|
|
|
|
import (
|
2024-02-11 19:28:00 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/models"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/models/query"
|
2024-02-13 20:52:10 +00:00
|
|
|
"gorm.io/driver/sqlite"
|
2024-02-11 10:56:21 +00:00
|
|
|
"gorm.io/gorm"
|
2024-02-24 21:07:49 +00:00
|
|
|
"gorm.io/gorm/logger"
|
2024-02-11 10:56:21 +00:00
|
|
|
)
|
|
|
|
|
2024-02-11 11:57:57 +00:00
|
|
|
//go:generate just _generate-gorm
|
2024-02-11 19:28:00 +00:00
|
|
|
func ConnectToDatabase(path string) (*gorm.DB, *query.Query, error) {
|
2024-02-24 21:07:49 +00:00
|
|
|
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{
|
|
|
|
Logger: logger.Default.LogMode(logger.Info),
|
|
|
|
})
|
2024-02-11 10:56:21 +00:00
|
|
|
if err != nil {
|
2024-02-11 19:28:00 +00:00
|
|
|
return nil, nil, err
|
2024-02-11 10:56:21 +00:00
|
|
|
}
|
|
|
|
|
2024-02-15 22:47:56 +00:00
|
|
|
err = db.AutoMigrate(
|
2024-02-24 11:57:53 +00:00
|
|
|
models.Monitor{},
|
2024-02-24 21:07:49 +00:00
|
|
|
models.WorkerGroup{},
|
2024-02-24 11:57:53 +00:00
|
|
|
models.MonitorHistory{},
|
2024-02-16 12:07:29 +00:00
|
|
|
models.OAuth2State{},
|
2024-02-15 22:47:56 +00:00
|
|
|
)
|
2024-02-12 08:25:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
2024-02-11 19:28:00 +00:00
|
|
|
|
|
|
|
q := query.Use(db)
|
|
|
|
|
|
|
|
return db, q, nil
|
2024-02-11 10:56:21 +00:00
|
|
|
}
|