1Panel/backend/init/db/db.go
2023-03-08 23:59:55 +08:00

34 lines
789 B
Go

package db
import (
"fmt"
"github.com/glebarez/sqlite"
"gorm.io/gorm"
"os"
"github.com/1Panel-dev/1Panel/backend/global"
)
func Init() {
if _, err := os.Stat(global.CONF.System.DbPath); err != nil {
if err := os.MkdirAll(global.CONF.System.DbPath, os.ModePerm); err != nil {
panic(fmt.Errorf("init db dir falied, err: %v", err))
}
}
fullPath := global.CONF.System.DbPath + "/" + global.CONF.System.DbFile
if _, err := os.Stat(fullPath); err != nil {
if _, err := os.Create(fullPath); err != nil {
panic(fmt.Errorf("init db file falied, err: %v", err))
}
}
db, err := gorm.Open(sqlite.Open(fullPath), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
if err != nil {
panic(err)
}
global.DB = db
global.LOG.Info("init db successfully")
}