feat: add init command and setup db

This commit is contained in:
2025-09-22 21:13:17 +05:30
parent 1847b1795d
commit 13702d117a
7 changed files with 157 additions and 17 deletions

44
cmd/utils/db.go Normal file
View File

@@ -0,0 +1,44 @@
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{})
}