修复用户表数据迁移bug

This commit is contained in:
GeekMaster
2025-05-06 18:51:55 +08:00
parent 5e59b3a708
commit dffdbf697b
5 changed files with 49 additions and 31 deletions

View File

@@ -99,6 +99,20 @@ func (s *AppServer) Run(db *gorm.DB) error {
&model.UserLoginLog{}, &model.UserLoginLog{},
&model.DallJob{}, &model.DallJob{},
) )
// 手动删除字段
if db.Migrator().HasColumn(&model.Order{}, "deleted_at") {
db.Migrator().DropColumn(&model.Order{}, "deleted_at")
}
if db.Migrator().HasColumn(&model.ChatItem{}, "deleted_at") {
db.Migrator().DropColumn(&model.ChatItem{}, "deleted_at")
}
if db.Migrator().HasColumn(&model.ChatMessage{}, "deleted_at") {
db.Migrator().DropColumn(&model.ChatMessage{}, "deleted_at")
}
if db.Migrator().HasColumn(&model.User{}, "chat_config") {
db.Migrator().DropColumn(&model.User{}, "chat_config")
}
logger.Info("Database tables migrated successfully") logger.Info("Database tables migrated successfully")
// 统计安装信息 // 统计安装信息

View File

@@ -178,6 +178,7 @@ func (h *UserHandler) Save(c *gin.Context) {
Power: data.Power, Power: data.Power,
Status: true, Status: true,
ChatRoles: utils.JsonEncode(data.ChatRoles), ChatRoles: utils.JsonEncode(data.ChatRoles),
ChatConfig: "{}",
ChatModels: utils.JsonEncode(data.ChatModels), ChatModels: utils.JsonEncode(data.ChatModels),
ExpiredTime: utils.Str2stamp(data.ExpiredTime), ExpiredTime: utils.Str2stamp(data.ExpiredTime),
} }
@@ -353,4 +354,4 @@ func (h *UserHandler) GenLoginLink(c *gin.Context) {
} }
resp.SUCCESS(c, tokenString) resp.SUCCESS(c, tokenString)
} }

View File

@@ -137,13 +137,15 @@ func (h *UserHandler) Register(c *gin.Context) {
salt := utils.RandString(8) salt := utils.RandString(8)
user := model.User{ user := model.User{
Username: data.Username, Username: data.Username,
Password: utils.GenPassword(data.Password, salt), Password: utils.GenPassword(data.Password, salt),
Avatar: "/images/avatar/user.png", Avatar: "/images/avatar/user.png",
Salt: salt, Salt: salt,
Status: true, Status: true,
ChatRoles: utils.JsonEncode([]string{"gpt"}), // 默认只订阅通用助手角色 ChatRoles: utils.JsonEncode([]string{"gpt"}), // 默认只订阅通用助手角色
Power: h.App.SysConfig.InitPower, ChatConfig: "{}",
ChatModels: "{}",
Power: h.App.SysConfig.InitPower,
} }
// check if the username is existing // check if the username is existing

View File

@@ -5,27 +5,27 @@ import (
) )
type User struct { type User struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
Username string `gorm:"column:username;type:varchar(30);uniqueIndex;not null;comment:用户名" json:"username"` Username string `gorm:"column:username;type:varchar(30);uniqueIndex;not null;comment:用户名" json:"username"`
Mobile string `gorm:"column:mobile;type:char(11);comment:手机号" json:"mobile"` Mobile string `gorm:"column:mobile;type:char(11);comment:手机号" json:"mobile"`
Email string `gorm:"column:email;type:varchar(50);comment:邮箱地址" json:"email"` Email string `gorm:"column:email;type:varchar(50);comment:邮箱地址" json:"email"`
Nickname string `gorm:"column:nickname;type:varchar(30);not null;comment:昵称" json:"nickname"` Nickname string `gorm:"column:nickname;type:varchar(30);not null;comment:昵称" json:"nickname"`
Password string `gorm:"column:password;type:char(64);not null;comment:密码" json:"password"` Password string `gorm:"column:password;type:char(64);not null;comment:密码" json:"password"`
Avatar string `gorm:"column:avatar;type:varchar(255);not null;comment:头像" json:"avatar"` Avatar string `gorm:"column:avatar;type:varchar(255);not null;comment:头像" json:"avatar"`
Salt string `gorm:"column:salt;type:char(12);not null;comment:密码盐" json:"salt"` Salt string `gorm:"column:salt;type:char(12);not null;comment:密码盐" json:"salt"`
Power int `gorm:"column:power;type:int;not null;default:0;comment:剩余算力" json:"power"` Power int `gorm:"column:power;type:int;default:0;comment:剩余算力" json:"power"`
ExpiredTime int64 `gorm:"column:expired_time;type:int;not null;comment:用户过期时间" json:"expired_time"` ExpiredTime int64 `gorm:"column:expired_time;type:int;not null;comment:用户过期时间" json:"expired_time"`
Status bool `gorm:"column:status;type:tinyint(1);not null;comment:当前状态" json:"status"` Status bool `gorm:"column:status;type:tinyint(1);not null;comment:当前状态" json:"status"`
ChatConfig string `gorm:"column:chat_config;type:text;not null;comment:聊天配置json" json:"chat_config"` ChatConfig string `gorm:"column:chat_config_json;type:text;default:null;comment:聊天配置json" json:"chat_config"`
ChatRoles string `gorm:"column:chat_roles_json;type:text;not null;comment:聊天角色 json" json:"chat_roles_json"` ChatRoles string `gorm:"column:chat_roles_json;type:text;default:null;comment:聊天角色 json" json:"chat_roles"`
ChatModels string `gorm:"column:chat_models_json;type:text;not null;comment:AI模型 json" json:"chat_models_json"` ChatModels string `gorm:"column:chat_models_json;type:text;default:null;comment:AI模型 json" json:"chat_models"`
LastLoginAt int64 `gorm:"column:last_login_at;type:int;not null;comment:最后登录时间" json:"last_login_at"` LastLoginAt int64 `gorm:"column:last_login_at;type:int;not null;comment:最后登录时间" json:"last_login_at"`
Vip bool `gorm:"column:vip;type:tinyint(1);not null;default:0;comment:是否会员" json:"vip"` Vip bool `gorm:"column:vip;type:tinyint(1);not null;default:0;comment:是否会员" json:"vip"`
LastLoginIp string `gorm:"column:last_login_ip;type:char(16);not null;comment:最后登录 IP" json:"last_login_ip"` LastLoginIp string `gorm:"column:last_login_ip;type:char(16);not null;comment:最后登录 IP" json:"last_login_ip"`
OpenId string `gorm:"column:openid;type:varchar(100);comment:第三方登录账号ID" json:"openid"` OpenId string `gorm:"column:openid;type:varchar(100);comment:第三方登录账号ID" json:"openid"`
Platform string `gorm:"column:platform;type:varchar(30);comment:登录平台" json:"platform"` Platform string `gorm:"column:platform;type:varchar(30);comment:登录平台" json:"platform"`
CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null" json:"created_at"` CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null" json:"updated_at"` UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null" json:"updated_at"`
} }
func (m *User) TableName() string { func (m *User) TableName() string {

View File

@@ -9,11 +9,12 @@ package store
import ( import (
"geekai/core/types" "geekai/core/types"
"time"
"gorm.io/driver/mysql" "gorm.io/driver/mysql"
"gorm.io/gorm" "gorm.io/gorm"
"gorm.io/gorm/logger" "gorm.io/gorm/logger"
"gorm.io/gorm/schema" "gorm.io/gorm/schema"
"time"
) )
func NewGormConfig() *gorm.Config { func NewGormConfig() *gorm.Config {
@@ -36,9 +37,9 @@ func NewMysql(config *gorm.Config, appConfig *types.AppConfig) (*gorm.DB, error)
if err != nil { if err != nil {
return nil, err return nil, err
} }
sqlDB.SetMaxIdleConns(32) sqlDB.SetMaxIdleConns(32)
sqlDB.SetMaxOpenConns(512) sqlDB.SetMaxOpenConns(512)
sqlDB.SetConnMaxLifetime(time.Hour) sqlDB.SetConnMaxLifetime(time.Hour)
return db, nil return db, nil
} }