Cloudreve/models/migration.go

33 lines
737 B
Go
Raw Normal View History

2019-11-06 22:35:31 +08:00
package model
2019-11-07 15:56:05 +08:00
import (
"Cloudreve/pkg/util"
"github.com/jinzhu/gorm"
)
2019-11-06 22:35:31 +08:00
2019-11-07 15:56:05 +08:00
//执行数据迁移
2019-11-06 22:35:31 +08:00
func migration() {
// 自动迁移模式
2019-11-07 15:56:05 +08:00
DB.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})
// 添加初始用户
_, err := GetUser(1)
if gorm.IsRecordNotFoundError(err) {
defaultUser := NewUser()
//TODO 动态生成密码
defaultUser.Email = "admin@cloudreve.org"
defaultUser.Nick = "admin"
defaultUser.Status = Active
defaultUser.Group = 1
defaultUser.PrimaryGroup = 1
err := defaultUser.SetPassword("admin")
if err != nil {
util.Log().Panic("无法创建密码, ", err)
}
if err := DB.Create(&defaultUser).Error; err != nil {
util.Log().Panic("无法创建初始用户, ", err)
}
}
2019-11-06 22:35:31 +08:00
}