重命名数据表

This commit is contained in:
RockYang
2025-09-01 07:35:37 +08:00
parent 9a4239290b
commit 3531c7f356
31 changed files with 133 additions and 60 deletions

View File

@@ -11,6 +11,7 @@ package types
type ModerationConfig struct { type ModerationConfig struct {
Enable bool `json:"enable"` // 是否启用文本审查 Enable bool `json:"enable"` // 是否启用文本审查
Active string `json:"active"` Active string `json:"active"`
EnableGuide bool `json:"enable_guide"` // 是否启用模型引导提示词
GuidePrompt string `json:"guide_prompt"` // 模型引导提示词 GuidePrompt string `json:"guide_prompt"` // 模型引导提示词
Gitee ModerationGiteeConfig `json:"gitee"` Gitee ModerationGiteeConfig `json:"gitee"`
Baidu ModerationBaiduConfig `json:"baidu"` Baidu ModerationBaiduConfig `json:"baidu"`

View File

@@ -9,7 +9,6 @@ package handler
import ( import (
"geekai/core" "geekai/core"
"geekai/core/middleware"
"geekai/store/model" "geekai/store/model"
"geekai/store/vo" "geekai/store/vo"
"geekai/utils" "geekai/utils"
@@ -30,12 +29,7 @@ func NewProductHandler(app *core.AppServer, db *gorm.DB) *ProductHandler {
// RegisterRoutes 注册路由 // RegisterRoutes 注册路由
func (h *ProductHandler) RegisterRoutes() { func (h *ProductHandler) RegisterRoutes() {
group := h.App.Engine.Group("/api/product/") group := h.App.Engine.Group("/api/product/")
group.GET("list", h.List)
// 需要用户授权的接口
group.Use(middleware.UserAuthMiddleware(h.App.Config.Session.SecretKey, h.App.Redis))
{
group.GET("list", h.List)
}
} }
// List 模型列表 // List 模型列表

View File

@@ -51,6 +51,7 @@ func (s *MigrationService) StartMigrate() {
s.MigrateConfig(s.appConfig) s.MigrateConfig(s.appConfig)
s.TableMigration() s.TableMigration()
s.MigrateLicense() s.MigrateLicense()
s.TableRename()
}() }()
} }
@@ -168,6 +169,63 @@ func (s *MigrationService) TableMigration() {
} }
} }
// 遍历所有的数据表,如果表名中包含 chatgpt_则重命名为 geekai_只执行一次
func (s *MigrationService) TableRename() {
key := "migrate:table_rename"
if s.redisClient.Get(context.Background(), key).Val() == "1" {
logger.Info("数据表重命名已执行,跳过重命名")
return
}
logger.Info("开始重命名数据表...")
// 定义需要重命名的表映射
tableRenames := map[string]string{
"chatgpt_users": "geekai_users",
"chatgpt_orders": "geekai_orders",
"chatgpt_products": "geekai_products",
"chatgpt_configs": "geekai_configs",
"chatgpt_sd_jobs": "geekai_sd_jobs",
"chatgpt_mj_jobs": "geekai_mj_jobs",
"chatgpt_suno_jobs": "geekai_suno_jobs",
"chatgpt_dall_jobs": "geekai_dall_jobs",
"chatgpt_video_jobs": "geekai_video_jobs",
"chatgpt_jimeng_jobs": "geekai_jimeng_jobs",
"chatgpt_files": "geekai_files",
"chatgpt_menus": "geekai_menus",
"chatgpt_functions": "geekai_functions",
"chatgpt_invite_codes": "geekai_invite_codes",
"chatgpt_invite_logs": "geekai_invite_logs",
"chatgpt_redeems": "geekai_redeems",
"chatgpt_power_logs": "geekai_power_logs",
"chatgpt_user_login_logs": "geekai_user_login_logs",
}
// 执行重命名操作
for oldTableName, newTableName := range tableRenames {
// 检查旧表是否存在
if s.db.Migrator().HasTable(oldTableName) {
// 检查新表是否已存在
if !s.db.Migrator().HasTable(newTableName) {
err := s.db.Exec(fmt.Sprintf("ALTER TABLE %s RENAME TO %s", oldTableName, newTableName)).Error
if err != nil {
logger.Errorf("重命名数据表 %s 到 %s 失败: %v", oldTableName, newTableName, err)
} else {
logger.Infof("成功重命名数据表: %s -> %s", oldTableName, newTableName)
}
} else {
logger.Infof("目标表 %s 已存在,跳过重命名 %s", newTableName, oldTableName)
}
} else {
logger.Infof("源表 %s 不存在,跳过重命名", oldTableName)
}
}
// 标记重命名已完成
s.redisClient.Set(context.Background(), key, "1", 0)
logger.Info("数据表重命名完成")
}
// 迁移配置数据 // 迁移配置数据
func (s *MigrationService) MigrateConfig(config *types.AppConfig) error { func (s *MigrationService) MigrateConfig(config *types.AppConfig) error {

View File

@@ -5,18 +5,18 @@ import (
) )
type AdminUser struct { type AdminUser 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"`
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"`
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"`
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"`
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"`
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"`
CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null;comment:创建时间" json:"created_at"` CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null;comment:创建时间" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null;comment:更新时间" json:"updated_at"` UpdatedAt time.Time `gorm:"column:updated_at;type:datetime;not null;comment:更新时间" json:"updated_at"`
} }
// TableName 表名 // TableName 表名
func (m *AdminUser) TableName() string { func (m *AdminUser) TableName() string {
return "chatgpt_admin_users" return "geekai_admin_users"
} }

View File

@@ -6,13 +6,13 @@ import (
// ApiKey OpenAI API 模型 // ApiKey OpenAI API 模型
type ApiKey struct { type ApiKey struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
Name string `gorm:"column:name;type:varchar(30);comment:名称" json:"name"` Name string `gorm:"column:name;type:varchar(30);comment:名称" json:"name"`
Value string `gorm:"column:value;type:varchar(255);not null;comment:API KEY value" json:"value"` Value string `gorm:"column:value;type:varchar(255);not null;comment:API KEY value" json:"value"`
Type string `gorm:"column:type;type:varchar(10);default:chat;not null;comment:用途chat=>聊天img=>图片)" json:"type"` Type string `gorm:"column:type;type:varchar(10);default:chat;not null;comment:用途chat=>聊天img=>图片)" json:"type"`
LastUsedAt int64 `gorm:"column:last_used_at;type:int;not null;comment:最后使用时间" json:"last_used_at"` LastUsedAt int64 `gorm:"column:last_used_at;type:int;not null;comment:最后使用时间" json:"last_used_at"`
ApiURL string `gorm:"column:api_url;type:varchar(255);comment:API 地址" json:"api_url"` ApiURL string `gorm:"column:api_url;type:varchar(255);comment:API 地址" json:"api_url"`
Enabled bool `gorm:"column:enabled;type:tinyint(1);comment:是否启用" json:"enabled"` Enabled bool `gorm:"column:enabled;type:tinyint(1);comment:是否启用" json:"enabled"`
ProxyURL string `gorm:"column:proxy_url;type:varchar(100);comment:代理地址" json:"proxy_url"` ProxyURL string `gorm:"column:proxy_url;type:varchar(100);comment:代理地址" json:"proxy_url"`
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"`
@@ -20,5 +20,5 @@ type ApiKey struct {
// TableName 表名 // TableName 表名
func (m *ApiKey) TableName() string { func (m *ApiKey) TableName() string {
return "chatgpt_api_keys" return "geekai_api_keys"
} }

View File

@@ -3,15 +3,15 @@ package model
import "time" import "time"
type AppType struct { type AppType struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
Name string `gorm:"column:name;type:varchar(50);not null;comment:名称" json:"name"` Name string `gorm:"column:name;type:varchar(50);not null;comment:名称" json:"name"`
Icon string `gorm:"column:icon;type:varchar(255);not null;comment:图标URL" json:"icon"` Icon string `gorm:"column:icon;type:varchar(255);not null;comment:图标URL" json:"icon"`
SortNum int `gorm:"column:sort_num;type:tinyint;not null;comment:排序" json:"sort_num"` SortNum int `gorm:"column:sort_num;type:tinyint;not null;comment:排序" json:"sort_num"`
Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:是否启用" json:"enabled"` Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:是否启用" json:"enabled"`
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"`
} }
// TableName 表名 // TableName 表名
func (m *AppType) TableName() string { func (m *AppType) TableName() string {
return "chatgpt_app_types" return "geekai_app_types"
} }

View File

@@ -20,5 +20,5 @@ type ChatApp struct {
} }
func (m *ChatApp) TableName() string { func (m *ChatApp) TableName() string {
return "chatgpt_chat_roles" return "geekai_chat_roles"
} }

View File

@@ -17,5 +17,5 @@ type ChatItem struct {
} }
func (m *ChatItem) TableName() string { func (m *ChatItem) TableName() string {
return "chatgpt_chat_items" return "geekai_chat_items"
} }

View File

@@ -21,5 +21,5 @@ type ChatMessage struct {
} }
func (m *ChatMessage) TableName() string { func (m *ChatMessage) TableName() string {
return "chatgpt_chat_history" return "geekai_chat_history"
} }

View File

@@ -25,5 +25,5 @@ type ChatModel struct {
} }
func (m *ChatModel) TableName() string { func (m *ChatModel) TableName() string {
return "chatgpt_chat_models" return "geekai_chat_models"
} }

View File

@@ -7,5 +7,5 @@ type Config struct {
} }
func (m *Config) TableName() string { func (m *Config) TableName() string {
return "chatgpt_configs" return "geekai_configs"
} }

View File

@@ -17,5 +17,5 @@ type DallJob struct {
} }
func (m *DallJob) TableName() string { func (m *DallJob) TableName() string {
return "chatgpt_dall_jobs" return "geekai_dall_jobs"
} }

View File

@@ -14,5 +14,5 @@ type File struct {
} }
func (m *File) TableName() string { func (m *File) TableName() string {
return "chatgpt_files" return "geekai_files"
} }

View File

@@ -1,16 +1,16 @@
package model package model
type Function struct { type Function struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
Name string `gorm:"column:name;type:varchar(30);uniqueIndex;not null;comment:函数名称" json:"name"` Name string `gorm:"column:name;type:varchar(30);uniqueIndex;not null;comment:函数名称" json:"name"`
Label string `gorm:"column:label;type:varchar(30);comment:函数标签" json:"label"` Label string `gorm:"column:label;type:varchar(30);comment:函数标签" json:"label"`
Description string `gorm:"column:description;type:varchar(255);comment:函数描述" json:"description"` Description string `gorm:"column:description;type:varchar(255);comment:函数描述" json:"description"`
Parameters string `gorm:"column:parameters;type:text;comment:函数参数JSON" json:"parameters"` Parameters string `gorm:"column:parameters;type:text;comment:函数参数JSON" json:"parameters"`
Token string `gorm:"column:token;type:varchar(255);comment:API授权token" json:"token"` Token string `gorm:"column:token;type:varchar(255);comment:API授权token" json:"token"`
Action string `gorm:"column:action;type:varchar(255);comment:函数处理 API" json:"action"` Action string `gorm:"column:action;type:varchar(255);comment:函数处理 API" json:"action"`
Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;default:0;comment:是否启用" json:"enabled"` Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;default:0;comment:是否启用" json:"enabled"`
} }
func (m *Function) TableName() string { func (m *Function) TableName() string {
return "chatgpt_functions" return "geekai_functions"
} }

View File

@@ -12,5 +12,5 @@ type InviteCode struct {
} }
func (m *InviteCode) TableName() string { func (m *InviteCode) TableName() string {
return "chatgpt_invite_codes" return "geekai_invite_codes"
} }

View File

@@ -15,5 +15,5 @@ type InviteLog struct {
} }
func (m *InviteLog) TableName() string { func (m *InviteLog) TableName() string {
return "chatgpt_invite_logs" return "geekai_invite_logs"
} }

View File

@@ -51,5 +51,5 @@ const (
// TableName 返回数据表名称 // TableName 返回数据表名称
func (JimengJob) TableName() string { func (JimengJob) TableName() string {
return "chatgpt_jimeng_jobs" return "geekai_jimeng_jobs"
} }

View File

@@ -2,14 +2,14 @@ package model
// Menu 系统菜单 // Menu 系统菜单
type Menu struct { type Menu struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"` Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
Name string `gorm:"column:name;type:varchar(30);not null;comment:菜单名称" json:"name"` Name string `gorm:"column:name;type:varchar(30);not null;comment:菜单名称" json:"name"`
Icon string `gorm:"column:icon;type:varchar(150);not null;comment:菜单图标" json:"icon"` Icon string `gorm:"column:icon;type:varchar(150);not null;comment:菜单图标" json:"icon"`
URL string `gorm:"column:url;type:varchar(100);not null;comment:地址" json:"url"` URL string `gorm:"column:url;type:varchar(100);not null;comment:地址" json:"url"`
SortNum int `gorm:"column:sort_num;type:smallint;not null;comment:排序" json:"sort_num"` SortNum int `gorm:"column:sort_num;type:smallint;not null;comment:排序" json:"sort_num"`
Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:是否启用" json:"enabled"` Enabled bool `gorm:"column:enabled;type:tinyint(1);not null;comment:是否启用" json:"enabled"`
} }
func (m *Menu) TableName() string { func (m *Menu) TableName() string {
return "chatgpt_menus" return "geekai_menus"
} }

View File

@@ -24,5 +24,5 @@ type MidJourneyJob struct {
} }
func (m *MidJourneyJob) TableName() string { func (m *MidJourneyJob) TableName() string {
return "chatgpt_mj_jobs" return "geekai_mj_jobs"
} }

View File

@@ -0,0 +1,16 @@
package model
import "time"
type Moderation struct {
Id uint `gorm:"column:id;primaryKey;autoIncrement" json:"id"`
UserId uint `gorm:"column:user_id;type:int(11);not null;comment:用户ID" json:"user_id"`
Input string `gorm:"column:prompt;type:text;not null;comment:用户输入" json:"input"`
Output string `gorm:"column:output;type:text;not null;comment:AI 输出" json:"output"`
Result string `gorm:"column:result;type:text;not null;comment:鉴别结果" json:"result"`
CreatedAt time.Time `gorm:"column:created_at;type:datetime;not null" json:"created_at"`
}
func (m *Moderation) TableName() string {
return "geekai_moderation"
}

View File

@@ -26,5 +26,5 @@ type Order struct {
} }
func (m *Order) TableName() string { func (m *Order) TableName() string {
return "chatgpt_orders" return "geekai_orders"
} }

View File

@@ -20,5 +20,5 @@ type PowerLog struct {
} }
func (m *PowerLog) TableName() string { func (m *PowerLog) TableName() string {
return "chatgpt_power_logs" return "geekai_power_logs"
} }

View File

@@ -18,5 +18,5 @@ type Product struct {
} }
func (m *Product) TableName() string { func (m *Product) TableName() string {
return "chatgpt_products" return "geekai_products"
} }

View File

@@ -16,5 +16,5 @@ type Redeem struct {
} }
func (m *Redeem) TableName() string { func (m *Redeem) TableName() string {
return "chatgpt_redeems" return "geekai_redeems"
} }

View File

@@ -19,5 +19,5 @@ type SdJob struct {
} }
func (m *SdJob) TableName() string { func (m *SdJob) TableName() string {
return "chatgpt_sd_jobs" return "geekai_sd_jobs"
} }

View File

@@ -31,5 +31,5 @@ type SunoJob struct {
} }
func (m *SunoJob) TableName() string { func (m *SunoJob) TableName() string {
return "chatgpt_suno_jobs" return "geekai_suno_jobs"
} }

View File

@@ -29,5 +29,5 @@ type User struct {
} }
func (m *User) TableName() string { func (m *User) TableName() string {
return "chatgpt_users" return "geekai_users"
} }

View File

@@ -15,5 +15,5 @@ type UserLoginLog struct {
} }
func (m *UserLoginLog) TableName() string { func (m *UserLoginLog) TableName() string {
return "chatgpt_user_login_logs" return "geekai_user_login_logs"
} }

View File

@@ -23,5 +23,5 @@ type VideoJob struct {
} }
func (m *VideoJob) TableName() string { func (m *VideoJob) TableName() string {
return "chatgpt_video_jobs" return "geekai_video_jobs"
} }

View File

@@ -21,8 +21,8 @@ func NewGormConfig() *gorm.Config {
return &gorm.Config{ return &gorm.Config{
Logger: logger.Default.LogMode(logger.Warn), Logger: logger.Default.LogMode(logger.Warn),
NamingStrategy: schema.NamingStrategy{ NamingStrategy: schema.NamingStrategy{
TablePrefix: "chatgpt_", // 设置表前缀 TablePrefix: "geekai_", // 设置表前缀
SingularTable: false, // 使用单数表名形式 SingularTable: false, // 使用单数表名形式
}, },
} }
} }

View File

@@ -72,11 +72,11 @@
</el-tabs> </el-tabs>
<el-form :model="configs" label-position="top" class="py-5"> <el-form :model="configs" label-position="top" class="py-5">
<el-form-item label="启用文本审查"> <el-form-item label="启用模型引导提示词">
<el-switch v-model="configs.enable" /> <el-switch v-model="configs.enable_guide" />
</el-form-item> </el-form-item>
<el-form-item> <el-form-item v-if="configs.enable_guide">
<template #label> <template #label>
<span class="mr-2">大模型引导提示词</span> <span class="mr-2">大模型引导提示词</span>
<el-tooltip <el-tooltip
@@ -96,7 +96,11 @@
/> />
</el-form-item> </el-form-item>
<el-form-item> <el-form-item label="启用文本审查服务">
<el-switch v-model="configs.enable" />
</el-form-item>
<el-form-item v-if="configs.enable">
<template #label> <template #label>
<div class="flex items-center"> <div class="flex items-center">
<span class="mr-2">选择审查服务</span> <span class="mr-2">选择审查服务</span>