Files
envy/cmd/utils/db.go

45 lines
812 B
Go

package utils
import (
"fmt"
"os"
"path"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
)
type Project struct {
ID uint `gorm:"primaryKey"`
Name string
Environments []Environment
}
type Environment struct {
ID uint `gorm:"primaryKey"`
Name string
Data string
Active bool
ProjectID uint
Project Project `gorm:"constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`
}
var DB *gorm.DB
func Init() {
config, err := os.UserConfigDir()
if err != nil {
fmt.Println(ErrStyle.Render("Error accessing config directory:", err.Error()))
return
}
DB, err = gorm.Open(sqlite.Open(path.Join(config, "envy.db")), &gorm.Config{})
if err != nil {
ErrPrint("Error while opening database:" + err.Error())
}
}
func Migrate() {
DB.AutoMigrate(&Project{}, &Environment{})
}