package initialize import ( "os" "git.echol.cn/loser/st/server/global" "git.echol.cn/loser/st/server/model/app" "git.echol.cn/loser/st/server/model/example" "git.echol.cn/loser/st/server/model/system" "go.uber.org/zap" "gorm.io/gorm" ) func Gorm() *gorm.DB { switch global.GVA_CONFIG.System.DbType { case "mysql": global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Mysql.Dbname return GormMysql() case "pgsql": global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Pgsql.Dbname return GormPgSql() case "oracle": global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Oracle.Dbname return GormOracle() case "mssql": global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Mssql.Dbname return GormMssql() case "sqlite": global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Sqlite.Dbname return GormSqlite() default: global.GVA_ACTIVE_DBNAME = &global.GVA_CONFIG.Mysql.Dbname return GormMysql() } } func RegisterTables() { if global.GVA_CONFIG.System.DisableAutoMigrate { global.GVA_LOG.Info("auto-migrate is disabled, skipping table registration") return } // 初始化 PostgreSQL 扩展(仅创建 pgvector 扩展) InitPgSQLExtension() db := global.GVA_DB err := db.AutoMigrate( // System tables (管理后台表 - 不修改) system.SysApi{}, system.SysIgnoreApi{}, system.SysUser{}, system.SysBaseMenu{}, system.JwtBlacklist{}, system.SysAuthority{}, system.SysDictionary{}, system.SysOperationRecord{}, system.SysAutoCodeHistory{}, system.SysDictionaryDetail{}, system.SysBaseMenuParameter{}, system.SysBaseMenuBtn{}, system.SysAuthorityBtn{}, system.SysAutoCodePackage{}, system.SysExportTemplate{}, system.Condition{}, system.JoinTemplate{}, system.SysParams{}, system.SysVersion{}, system.SysError{}, system.SysApiToken{}, system.SysLoginLog{}, // Example tables example.ExaFile{}, example.ExaCustomer{}, example.ExaFileChunk{}, example.ExaFileUploadAndDownload{}, example.ExaAttachmentCategory{}, // App tables (前台应用表 - 新增) app.AppUser{}, app.AppUserSession{}, app.AICharacter{}, app.AppUserFavoriteCharacter{}, app.AIChat{}, app.AIChatMember{}, app.AIMessage{}, app.AIMessageSwipe{}, app.AIMemoryVector{}, app.AIProvider{}, app.AIModel{}, app.AIFile{}, app.AIPreset{}, app.AIWorldInfo{}, app.AIUsageStat{}, ) if err != nil { global.GVA_LOG.Error("register table failed", zap.Error(err)) os.Exit(0) } // 修复 PostgreSQL 序列(确保序列值与现有数据一致) fixPostgresSequences() // 创建向量索引(必须在 AutoMigrate 之后) CreateVectorIndexes() err = bizModel() if err != nil { global.GVA_LOG.Error("register biz_table failed", zap.Error(err)) os.Exit(0) } global.GVA_LOG.Info("register table success") } // fixPostgresSequences 修复 PostgreSQL 自增序列与现有数据不一致的问题 // AutoMigrate 后序列可能落后于已有数据的 max(id),导致插入时主键冲突 func fixPostgresSequences() { if global.GVA_CONFIG.System.DbType != "pgsql" { return } // 需要修复序列的表名列表 tables := []string{ "ai_characters", "app_users", "app_user_sessions", "app_user_favorite_characters", } for _, table := range tables { seqName := table + "_id_seq" sql := "SELECT setval('" + seqName + "', COALESCE((SELECT MAX(id) FROM " + table + "), 0) + 1, false)" if err := global.GVA_DB.Exec(sql).Error; err != nil { global.GVA_LOG.Warn("fix sequence failed", zap.String("table", table), zap.Error(err)) } } }