zdravko/internal/database.go

31 lines
603 B
Go
Raw Normal View History

2024-02-11 10:56:21 +00:00
package internal
import (
"code.tjo.space/mentos1386/zdravko/internal/models"
"code.tjo.space/mentos1386/zdravko/internal/models/query"
"gorm.io/driver/sqlite"
2024-02-11 10:56:21 +00:00
"gorm.io/gorm"
)
2024-02-11 11:57:57 +00:00
//go:generate just _generate-gorm
func ConnectToDatabase(path string) (*gorm.DB, *query.Query, error) {
db, err := gorm.Open(sqlite.Open(path), &gorm.Config{})
2024-02-11 10:56:21 +00:00
if err != nil {
return nil, nil, err
2024-02-11 10:56:21 +00:00
}
err = db.AutoMigrate(
&models.HealthcheckHTTP{},
&models.HealthcheckTCP{},
&models.Cronjob{},
&models.OAuth2State{},
)
2024-02-12 08:25:11 +00:00
if err != nil {
return nil, nil, err
}
q := query.Use(db)
return db, q, nil
2024-02-11 10:56:21 +00:00
}