alist/bootstrap/model.go

85 lines
2.4 KiB
Go
Raw Normal View History

2021-10-27 14:45:36 +00:00
package bootstrap
2021-10-25 10:53:59 +00:00
import (
"fmt"
"github.com/Xhofe/alist/conf"
2021-10-27 14:45:36 +00:00
"github.com/Xhofe/alist/model"
2021-10-25 10:53:59 +00:00
log "github.com/sirupsen/logrus"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
2022-02-25 15:55:57 +00:00
"gorm.io/driver/sqlite"
2021-10-25 10:53:59 +00:00
"gorm.io/gorm"
2021-11-01 15:14:34 +00:00
"gorm.io/gorm/logger"
2021-10-25 10:53:59 +00:00
"gorm.io/gorm/schema"
2021-11-01 15:14:34 +00:00
log2 "log"
"os"
2021-10-25 10:53:59 +00:00
"strings"
2021-11-01 15:14:34 +00:00
"time"
2021-10-25 10:53:59 +00:00
)
func InitModel() {
log.Infof("init model...")
2022-01-04 07:16:18 +00:00
var err error
2021-11-01 15:14:34 +00:00
databaseConfig := conf.Conf.Database
newLogger := logger.New(
log2.New(os.Stdout, "\r\n", log2.LstdFlags),
logger.Config{
SlowThreshold: time.Second,
LogLevel: logger.Silent,
IgnoreRecordNotFoundError: true,
Colorful: true,
},
)
gormConfig := &gorm.Config{
NamingStrategy: schema.NamingStrategy{
TablePrefix: databaseConfig.TablePrefix,
},
Logger: newLogger,
}
switch databaseConfig.Type {
2021-10-25 10:53:59 +00:00
case "sqlite3":
{
2021-11-01 15:14:34 +00:00
if !(strings.HasSuffix(databaseConfig.DBFile, ".db") && len(databaseConfig.DBFile) > 3) {
2021-10-25 10:53:59 +00:00
log.Fatalf("db name error.")
}
2021-11-01 15:14:34 +00:00
db, err := gorm.Open(sqlite.Open(databaseConfig.DBFile), gormConfig)
2021-10-25 10:53:59 +00:00
if err != nil {
log.Fatalf("failed to connect database:%s", err.Error())
}
conf.DB = db
}
case "mysql":
{
dsn := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local",
2021-11-01 15:14:34 +00:00
databaseConfig.User, databaseConfig.Password, databaseConfig.Host, databaseConfig.Port, databaseConfig.Name)
db, err := gorm.Open(mysql.Open(dsn), gormConfig)
2021-10-25 10:53:59 +00:00
if err != nil {
log.Fatalf("failed to connect database:%s", err.Error())
}
conf.DB = db
}
case "postgres":
{
2022-01-19 01:14:31 +00:00
dsn := fmt.Sprintf("host=%s user=%s password=%s dbname=%s port=%d sslmode=%s TimeZone=Asia/Shanghai",
databaseConfig.Host, databaseConfig.User, databaseConfig.Password, databaseConfig.Name, databaseConfig.Port, databaseConfig.SslMode)
2021-11-01 15:14:34 +00:00
db, err := gorm.Open(postgres.Open(dsn), gormConfig)
2021-10-25 10:53:59 +00:00
if err != nil {
log.Errorf("failed to connect database:%s", err.Error())
}
conf.DB = db
}
default:
2021-11-01 15:14:34 +00:00
log.Fatalf("not supported database type: %s", databaseConfig.Type)
2021-10-25 10:53:59 +00:00
}
2021-11-26 12:20:32 +00:00
log.Infof("auto migrate model...")
2022-01-04 07:16:18 +00:00
if databaseConfig.Type == "mysql" {
err = conf.DB.Set("gorm:table_options", "ENGINE=InnoDB CHARSET=utf8mb4").
AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{})
} else {
err = conf.DB.AutoMigrate(&model.SettingItem{}, &model.Account{}, &model.Meta{})
}
2021-10-25 10:53:59 +00:00
if err != nil {
log.Fatalf("failed to auto migrate: %s", err.Error())
2021-10-25 10:53:59 +00:00
}
2021-10-26 14:28:37 +00:00
}