mirror of https://github.com/Xhofe/alist
48 lines
999 B
Go
48 lines
999 B
Go
package db
|
|
|
|
import (
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/alist-org/alist/v3/internal/conf"
|
|
"github.com/alist-org/alist/v3/internal/model"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
var db *gorm.DB
|
|
|
|
func Init(d *gorm.DB) {
|
|
db = d
|
|
err := AutoMigrate(new(model.Storage), new(model.User), new(model.Meta), new(model.SettingItem), new(model.SearchNode), new(model.TaskItem), new(model.SSHPublicKey))
|
|
if err != nil {
|
|
log.Fatalf("failed migrate database: %s", err.Error())
|
|
}
|
|
}
|
|
|
|
func AutoMigrate(dst ...interface{}) error {
|
|
var err error
|
|
if conf.Conf.Database.Type == "mysql" {
|
|
err = db.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").AutoMigrate(dst...)
|
|
} else {
|
|
err = db.AutoMigrate(dst...)
|
|
}
|
|
return err
|
|
}
|
|
|
|
func GetDb() *gorm.DB {
|
|
return db
|
|
}
|
|
|
|
func Close() {
|
|
log.Info("closing db")
|
|
sqlDB, err := db.DB()
|
|
if err != nil {
|
|
log.Errorf("failed to get db: %s", err.Error())
|
|
return
|
|
}
|
|
err = sqlDB.Close()
|
|
if err != nil {
|
|
log.Errorf("failed to close db: %s", err.Error())
|
|
return
|
|
}
|
|
}
|