You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

723 lines
20 KiB

  1. package model
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "strings"
  7. "sync"
  8. "time"
  9. "github.com/QuantumNous/new-api/common"
  10. "github.com/QuantumNous/new-api/constant"
  11. "github.com/glebarez/sqlite"
  12. "gorm.io/driver/mysql"
  13. "gorm.io/driver/postgres"
  14. "gorm.io/gorm"
  15. )
  16. var commonGroupCol string
  17. var commonKeyCol string
  18. var commonTrueVal string
  19. var commonFalseVal string
  20. var logKeyCol string
  21. var logGroupCol string
  22. func initCol() {
  23. // init common column names
  24. if common.UsingPostgreSQL {
  25. commonGroupCol = `"group"`
  26. commonKeyCol = `"key"`
  27. commonTrueVal = "true"
  28. commonFalseVal = "false"
  29. } else {
  30. commonGroupCol = "`group`"
  31. commonKeyCol = "`key`"
  32. commonTrueVal = "1"
  33. commonFalseVal = "0"
  34. }
  35. if os.Getenv("LOG_SQL_DSN") != "" {
  36. switch common.LogSqlType {
  37. case common.DatabaseTypePostgreSQL:
  38. logGroupCol = `"group"`
  39. logKeyCol = `"key"`
  40. default:
  41. logGroupCol = commonGroupCol
  42. logKeyCol = commonKeyCol
  43. }
  44. } else {
  45. // LOG_SQL_DSN 为空时,日志数据库与主数据库相同
  46. if common.UsingPostgreSQL {
  47. logGroupCol = `"group"`
  48. logKeyCol = `"key"`
  49. } else {
  50. logGroupCol = commonGroupCol
  51. logKeyCol = commonKeyCol
  52. }
  53. }
  54. // log sql type and database type
  55. //common.SysLog("Using Log SQL Type: " + common.LogSqlType)
  56. }
  57. var DB *gorm.DB
  58. var LOG_DB *gorm.DB
  59. func createRootAccountIfNeed() error {
  60. var user User
  61. //if user.Status != common.UserStatusEnabled {
  62. if err := DB.First(&user).Error; err != nil {
  63. common.SysLog("no user exists, create a root user for you: username is root, password is 123456")
  64. hashedPassword, err := common.Password2Hash("123456")
  65. if err != nil {
  66. return err
  67. }
  68. rootUser := User{
  69. Username: "root",
  70. Password: hashedPassword,
  71. Role: common.RoleRootUser,
  72. Status: common.UserStatusEnabled,
  73. DisplayName: "Root User",
  74. AccessToken: nil,
  75. Quota: 100000000,
  76. }
  77. DB.Create(&rootUser)
  78. }
  79. return nil
  80. }
  81. func CheckSetup() {
  82. setup := GetSetup()
  83. if setup == nil {
  84. // No setup record exists, check if we have a root user
  85. if RootUserExists() {
  86. common.SysLog("system is not initialized, but root user exists")
  87. // Create setup record
  88. newSetup := Setup{
  89. Version: common.Version,
  90. InitializedAt: time.Now().Unix(),
  91. }
  92. err := DB.Create(&newSetup).Error
  93. if err != nil {
  94. common.SysLog("failed to create setup record: " + err.Error())
  95. }
  96. constant.Setup = true
  97. } else {
  98. common.SysLog("system is not initialized and no root user exists")
  99. constant.Setup = false
  100. }
  101. } else {
  102. // Setup record exists, system is initialized
  103. common.SysLog("system is already initialized at: " + time.Unix(setup.InitializedAt, 0).String())
  104. constant.Setup = true
  105. }
  106. }
  107. func chooseDB(envName string, isLog bool) (*gorm.DB, error) {
  108. defer func() {
  109. initCol()
  110. }()
  111. dsn := os.Getenv(envName)
  112. if dsn != "" {
  113. if strings.HasPrefix(dsn, "postgres://") || strings.HasPrefix(dsn, "postgresql://") {
  114. // Use PostgreSQL
  115. common.SysLog("using PostgreSQL as database")
  116. if !isLog {
  117. common.UsingPostgreSQL = true
  118. } else {
  119. common.LogSqlType = common.DatabaseTypePostgreSQL
  120. }
  121. return gorm.Open(postgres.New(postgres.Config{
  122. DSN: dsn,
  123. PreferSimpleProtocol: true, // disables implicit prepared statement usage
  124. }), &gorm.Config{
  125. PrepareStmt: true, // precompile SQL
  126. })
  127. }
  128. if strings.HasPrefix(dsn, "local") {
  129. common.SysLog("SQL_DSN not set, using SQLite as database")
  130. if !isLog {
  131. common.UsingSQLite = true
  132. } else {
  133. common.LogSqlType = common.DatabaseTypeSQLite
  134. }
  135. return gorm.Open(sqlite.Open(common.SQLitePath), &gorm.Config{
  136. PrepareStmt: true, // precompile SQL
  137. })
  138. }
  139. // Use MySQL
  140. common.SysLog("using MySQL as database")
  141. // check parseTime
  142. if !strings.Contains(dsn, "parseTime") {
  143. if strings.Contains(dsn, "?") {
  144. dsn += "&parseTime=true"
  145. } else {
  146. dsn += "?parseTime=true"
  147. }
  148. }
  149. if !isLog {
  150. common.UsingMySQL = true
  151. } else {
  152. common.LogSqlType = common.DatabaseTypeMySQL
  153. }
  154. return gorm.Open(mysql.Open(dsn), &gorm.Config{
  155. PrepareStmt: true, // precompile SQL
  156. })
  157. }
  158. // Use SQLite
  159. common.SysLog("SQL_DSN not set, using SQLite as database")
  160. common.UsingSQLite = true
  161. return gorm.Open(sqlite.Open(common.SQLitePath), &gorm.Config{
  162. PrepareStmt: true, // precompile SQL
  163. })
  164. }
  165. func InitDB() (err error) {
  166. db, err := chooseDB("SQL_DSN", false)
  167. if err == nil {
  168. if common.DebugEnabled {
  169. db = db.Debug()
  170. }
  171. DB = db
  172. // MySQL charset/collation startup check: ensure Chinese-capable charset
  173. if common.UsingMySQL {
  174. if err := checkMySQLChineseSupport(DB); err != nil {
  175. panic(err)
  176. }
  177. }
  178. sqlDB, err := DB.DB()
  179. if err != nil {
  180. return err
  181. }
  182. sqlDB.SetMaxIdleConns(common.GetEnvOrDefault("SQL_MAX_IDLE_CONNS", 100))
  183. sqlDB.SetMaxOpenConns(common.GetEnvOrDefault("SQL_MAX_OPEN_CONNS", 1000))
  184. sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetEnvOrDefault("SQL_MAX_LIFETIME", 60)))
  185. if !common.IsMasterNode {
  186. return nil
  187. }
  188. if common.UsingMySQL {
  189. //_, _ = sqlDB.Exec("ALTER TABLE channels MODIFY model_mapping TEXT;") // TODO: delete this line when most users have upgraded
  190. }
  191. common.SysLog("database migration started")
  192. err = migrateDB()
  193. if err != nil {
  194. return err
  195. }
  196. LoadEmailQuotaCache()
  197. return nil
  198. } else {
  199. common.FatalLog(err)
  200. }
  201. return err
  202. }
  203. func InitLogDB() (err error) {
  204. if os.Getenv("LOG_SQL_DSN") == "" {
  205. LOG_DB = DB
  206. return
  207. }
  208. db, err := chooseDB("LOG_SQL_DSN", true)
  209. if err == nil {
  210. if common.DebugEnabled {
  211. db = db.Debug()
  212. }
  213. LOG_DB = db
  214. // If log DB is MySQL, also ensure Chinese-capable charset
  215. if common.LogSqlType == common.DatabaseTypeMySQL {
  216. if err := checkMySQLChineseSupport(LOG_DB); err != nil {
  217. panic(err)
  218. }
  219. }
  220. sqlDB, err := LOG_DB.DB()
  221. if err != nil {
  222. return err
  223. }
  224. sqlDB.SetMaxIdleConns(common.GetEnvOrDefault("SQL_MAX_IDLE_CONNS", 100))
  225. sqlDB.SetMaxOpenConns(common.GetEnvOrDefault("SQL_MAX_OPEN_CONNS", 1000))
  226. sqlDB.SetConnMaxLifetime(time.Second * time.Duration(common.GetEnvOrDefault("SQL_MAX_LIFETIME", 60)))
  227. if !common.IsMasterNode {
  228. return nil
  229. }
  230. common.SysLog("database migration started")
  231. err = migrateLOGDB()
  232. return err
  233. } else {
  234. common.FatalLog(err)
  235. }
  236. return err
  237. }
  238. func migrateDB() error {
  239. // Migrate price_amount column from float/double to decimal for existing tables
  240. migrateSubscriptionPlanPriceAmount()
  241. // Drop bound_channel_id column from tokens table (deprecated field)
  242. migrateTokenDropBoundChannelId()
  243. err := DB.AutoMigrate(
  244. &Channel{},
  245. &ChannelAssetCredential{},
  246. &Token{},
  247. &User{},
  248. &PasskeyCredential{},
  249. &Option{},
  250. &Redemption{},
  251. &Ability{},
  252. &Log{},
  253. &Midjourney{},
  254. &TopUp{},
  255. &QuotaData{},
  256. &Task{},
  257. &Model{},
  258. &Vendor{},
  259. &PrefillGroup{},
  260. &Setup{},
  261. &TwoFA{},
  262. &TwoFABackupCode{},
  263. &Checkin{},
  264. &SubscriptionOrder{},
  265. &UserSubscription{},
  266. &SubscriptionPreConsumeRecord{},
  267. &CustomOAuthProvider{},
  268. &UserOAuthBinding{},
  269. &PendingSyncRecord{},
  270. &QuotaSyncLog{},
  271. &EmailQuotaRule{},
  272. &UserModelRateLimit{},
  273. &UserAssetChannel{},
  274. &UserAssetGroup{},
  275. &UserMigrationBatch{},
  276. &UserMigrationItem{},
  277. &MigrationQuotaGrant{},
  278. )
  279. if err != nil {
  280. return err
  281. }
  282. if err := DB.Exec("DROP TABLE IF EXISTS channel_pricings").Error; err != nil {
  283. return err
  284. }
  285. if err := DB.Exec("DROP TABLE IF EXISTS pricing_tags").Error; err != nil {
  286. return err
  287. }
  288. if common.UsingSQLite {
  289. if err := ensureSubscriptionPlanTableSQLite(); err != nil {
  290. return err
  291. }
  292. } else {
  293. if err := DB.AutoMigrate(&SubscriptionPlan{}); err != nil {
  294. return err
  295. }
  296. }
  297. // 将现有 sort_order=0 的模型和供应商更新为默认大数
  298. DB.Model(&Model{}).Where("sort_order = 0").Update("sort_order", 999999)
  299. DB.Model(&Vendor{}).Where("sort_order = 0").Update("sort_order", 999999)
  300. migrateChannelPublicName()
  301. return nil
  302. }
  303. func migrateDBFast() error {
  304. // Drop bound_channel_id column from tokens table (deprecated field)
  305. migrateTokenDropBoundChannelId()
  306. var wg sync.WaitGroup
  307. migrations := []struct {
  308. model interface{}
  309. name string
  310. }{
  311. {&Channel{}, "Channel"},
  312. {&ChannelAssetCredential{}, "ChannelAssetCredential"},
  313. {&Token{}, "Token"},
  314. {&User{}, "User"},
  315. {&PasskeyCredential{}, "PasskeyCredential"},
  316. {&Option{}, "Option"},
  317. {&Redemption{}, "Redemption"},
  318. {&Ability{}, "Ability"},
  319. {&Log{}, "Log"},
  320. {&Midjourney{}, "Midjourney"},
  321. {&TopUp{}, "TopUp"},
  322. {&QuotaData{}, "QuotaData"},
  323. {&Task{}, "Task"},
  324. {&Model{}, "Model"},
  325. {&Vendor{}, "Vendor"},
  326. {&PrefillGroup{}, "PrefillGroup"},
  327. {&Setup{}, "Setup"},
  328. {&TwoFA{}, "TwoFA"},
  329. {&TwoFABackupCode{}, "TwoFABackupCode"},
  330. {&Checkin{}, "Checkin"},
  331. {&SubscriptionOrder{}, "SubscriptionOrder"},
  332. {&UserSubscription{}, "UserSubscription"},
  333. {&SubscriptionPreConsumeRecord{}, "SubscriptionPreConsumeRecord"},
  334. {&CustomOAuthProvider{}, "CustomOAuthProvider"},
  335. {&UserOAuthBinding{}, "UserOAuthBinding"},
  336. {&PendingSyncRecord{}, "PendingSyncRecord"},
  337. {&QuotaSyncLog{}, "QuotaSyncLog"},
  338. {&EmailQuotaRule{}, "EmailQuotaRule"},
  339. {&UserAssetChannel{}, "UserAssetChannel"},
  340. {&UserAssetGroup{}, "UserAssetGroup"},
  341. {&UserMigrationBatch{}, "UserMigrationBatch"},
  342. {&UserMigrationItem{}, "UserMigrationItem"},
  343. {&MigrationQuotaGrant{}, "MigrationQuotaGrant"},
  344. }
  345. // 动态计算migration数量,确保errChan缓冲区足够大
  346. errChan := make(chan error, len(migrations))
  347. for _, m := range migrations {
  348. wg.Add(1)
  349. go func(model interface{}, name string) {
  350. defer wg.Done()
  351. if err := DB.AutoMigrate(model); err != nil {
  352. errChan <- fmt.Errorf("failed to migrate %s: %v", name, err)
  353. }
  354. }(m.model, m.name)
  355. }
  356. // Wait for all migrations to complete
  357. wg.Wait()
  358. close(errChan)
  359. // Check for any errors
  360. for err := range errChan {
  361. if err != nil {
  362. return err
  363. }
  364. }
  365. if common.UsingSQLite {
  366. if err := ensureSubscriptionPlanTableSQLite(); err != nil {
  367. return err
  368. }
  369. } else {
  370. if err := DB.AutoMigrate(&SubscriptionPlan{}); err != nil {
  371. return err
  372. }
  373. }
  374. common.SysLog("database migrated")
  375. return nil
  376. }
  377. func migrateLOGDB() error {
  378. var err error
  379. if err = LOG_DB.AutoMigrate(&Log{}); err != nil {
  380. return err
  381. }
  382. return nil
  383. }
  384. type sqliteColumnDef struct {
  385. Name string
  386. DDL string
  387. }
  388. func ensureSubscriptionPlanTableSQLite() error {
  389. if !common.UsingSQLite {
  390. return nil
  391. }
  392. tableName := "subscription_plans"
  393. if !DB.Migrator().HasTable(tableName) {
  394. createSQL := `CREATE TABLE ` + "`" + tableName + "`" + ` (
  395. ` + "`id`" + ` integer,
  396. ` + "`title`" + ` varchar(128) NOT NULL,
  397. ` + "`subtitle`" + ` varchar(255) DEFAULT '',
  398. ` + "`price_amount`" + ` decimal(10,6) NOT NULL,
  399. ` + "`currency`" + ` varchar(8) NOT NULL DEFAULT 'USD',
  400. ` + "`duration_unit`" + ` varchar(16) NOT NULL DEFAULT 'month',
  401. ` + "`duration_value`" + ` integer NOT NULL DEFAULT 1,
  402. ` + "`custom_seconds`" + ` bigint NOT NULL DEFAULT 0,
  403. ` + "`enabled`" + ` numeric DEFAULT 1,
  404. ` + "`sort_order`" + ` integer DEFAULT 0,
  405. ` + "`stripe_price_id`" + ` varchar(128) DEFAULT '',
  406. ` + "`creem_product_id`" + ` varchar(128) DEFAULT '',
  407. ` + "`max_purchase_per_user`" + ` integer DEFAULT 0,
  408. ` + "`upgrade_group`" + ` varchar(64) DEFAULT '',
  409. ` + "`total_amount`" + ` bigint NOT NULL DEFAULT 0,
  410. ` + "`quota_reset_period`" + ` varchar(16) DEFAULT 'never',
  411. ` + "`quota_reset_custom_seconds`" + ` bigint DEFAULT 0,
  412. ` + "`created_at`" + ` bigint,
  413. ` + "`updated_at`" + ` bigint,
  414. PRIMARY KEY (` + "`id`" + `)
  415. )`
  416. return DB.Exec(createSQL).Error
  417. }
  418. var cols []struct {
  419. Name string `gorm:"column:name"`
  420. }
  421. if err := DB.Raw("PRAGMA table_info(`" + tableName + "`)").Scan(&cols).Error; err != nil {
  422. return err
  423. }
  424. existing := make(map[string]struct{}, len(cols))
  425. for _, c := range cols {
  426. existing[c.Name] = struct{}{}
  427. }
  428. required := []sqliteColumnDef{
  429. {Name: "title", DDL: "`title` varchar(128) NOT NULL"},
  430. {Name: "subtitle", DDL: "`subtitle` varchar(255) DEFAULT ''"},
  431. {Name: "price_amount", DDL: "`price_amount` decimal(10,6) NOT NULL"},
  432. {Name: "currency", DDL: "`currency` varchar(8) NOT NULL DEFAULT 'USD'"},
  433. {Name: "duration_unit", DDL: "`duration_unit` varchar(16) NOT NULL DEFAULT 'month'"},
  434. {Name: "duration_value", DDL: "`duration_value` integer NOT NULL DEFAULT 1"},
  435. {Name: "custom_seconds", DDL: "`custom_seconds` bigint NOT NULL DEFAULT 0"},
  436. {Name: "enabled", DDL: "`enabled` numeric DEFAULT 1"},
  437. {Name: "sort_order", DDL: "`sort_order` integer DEFAULT 0"},
  438. {Name: "stripe_price_id", DDL: "`stripe_price_id` varchar(128) DEFAULT ''"},
  439. {Name: "creem_product_id", DDL: "`creem_product_id` varchar(128) DEFAULT ''"},
  440. {Name: "max_purchase_per_user", DDL: "`max_purchase_per_user` integer DEFAULT 0"},
  441. {Name: "upgrade_group", DDL: "`upgrade_group` varchar(64) DEFAULT ''"},
  442. {Name: "total_amount", DDL: "`total_amount` bigint NOT NULL DEFAULT 0"},
  443. {Name: "quota_reset_period", DDL: "`quota_reset_period` varchar(16) DEFAULT 'never'"},
  444. {Name: "quota_reset_custom_seconds", DDL: "`quota_reset_custom_seconds` bigint DEFAULT 0"},
  445. {Name: "created_at", DDL: "`created_at` bigint"},
  446. {Name: "updated_at", DDL: "`updated_at` bigint"},
  447. }
  448. for _, col := range required {
  449. if _, ok := existing[col.Name]; ok {
  450. continue
  451. }
  452. if err := DB.Exec("ALTER TABLE `" + tableName + "` ADD COLUMN " + col.DDL).Error; err != nil {
  453. return err
  454. }
  455. }
  456. return nil
  457. }
  458. // migrateSubscriptionPlanPriceAmount migrates price_amount column from float/double to decimal(10,6)
  459. // This is safe to run multiple times - it checks the column type first
  460. func migrateSubscriptionPlanPriceAmount() {
  461. // SQLite doesn't support ALTER COLUMN, and its type affinity handles this automatically
  462. // Skip early to avoid GORM parsing the existing table DDL which may cause issues
  463. if common.UsingSQLite {
  464. return
  465. }
  466. tableName := "subscription_plans"
  467. columnName := "price_amount"
  468. // Check if table exists first
  469. if !DB.Migrator().HasTable(tableName) {
  470. return
  471. }
  472. // Check if column exists
  473. if !DB.Migrator().HasColumn(&SubscriptionPlan{}, columnName) {
  474. return
  475. }
  476. var alterSQL string
  477. if common.UsingPostgreSQL {
  478. // PostgreSQL: Check if already decimal/numeric
  479. var dataType string
  480. DB.Raw(`SELECT data_type FROM information_schema.columns
  481. WHERE table_name = ? AND column_name = ?`, tableName, columnName).Scan(&dataType)
  482. if dataType == "numeric" {
  483. return // Already decimal/numeric
  484. }
  485. alterSQL = fmt.Sprintf(`ALTER TABLE %s ALTER COLUMN %s TYPE decimal(10,6) USING %s::decimal(10,6)`,
  486. tableName, columnName, columnName)
  487. } else if common.UsingMySQL {
  488. // MySQL: Check if already decimal
  489. var columnType string
  490. DB.Raw(`SELECT COLUMN_TYPE FROM information_schema.columns
  491. WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`,
  492. tableName, columnName).Scan(&columnType)
  493. if strings.HasPrefix(strings.ToLower(columnType), "decimal") {
  494. return // Already decimal
  495. }
  496. alterSQL = fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s decimal(10,6) NOT NULL DEFAULT 0",
  497. tableName, columnName)
  498. } else {
  499. return
  500. }
  501. if alterSQL != "" {
  502. if err := DB.Exec(alterSQL).Error; err != nil {
  503. common.SysLog(fmt.Sprintf("Warning: failed to migrate %s.%s to decimal: %v", tableName, columnName, err))
  504. } else {
  505. common.SysLog(fmt.Sprintf("Successfully migrated %s.%s to decimal(10,6)", tableName, columnName))
  506. }
  507. }
  508. }
  509. // migrateTokenDropBoundChannelId 删除 tokens 表中的 bound_channel_id 列(已废弃的字段)
  510. func migrateTokenDropBoundChannelId() {
  511. // SQLite 不支持 DROP COLUMN,跳过迁移
  512. if common.UsingSQLite {
  513. return
  514. }
  515. tableName := "tokens"
  516. columnName := "bound_channel_id"
  517. // 检查表是否存在
  518. if !DB.Migrator().HasTable(tableName) {
  519. return
  520. }
  521. // 检查列是否存在
  522. if !DB.Migrator().HasColumn(&Token{}, columnName) {
  523. return
  524. }
  525. // MySQL 和 PostgreSQL 都支持此语法
  526. dropSQL := fmt.Sprintf("ALTER TABLE %s DROP COLUMN %s", tableName, columnName)
  527. if err := DB.Exec(dropSQL).Error; err != nil {
  528. common.SysLog(fmt.Sprintf("Warning: failed to drop %s.%s: %v", tableName, columnName, err))
  529. } else {
  530. common.SysLog(fmt.Sprintf("Successfully dropped deprecated column %s.%s", tableName, columnName))
  531. }
  532. }
  533. func closeDB(db *gorm.DB) error {
  534. sqlDB, err := db.DB()
  535. if err != nil {
  536. return err
  537. }
  538. err = sqlDB.Close()
  539. return err
  540. }
  541. func CloseDB() error {
  542. if LOG_DB != DB {
  543. err := closeDB(LOG_DB)
  544. if err != nil {
  545. return err
  546. }
  547. }
  548. return closeDB(DB)
  549. }
  550. // checkMySQLChineseSupport ensures the MySQL connection and current schema
  551. // default charset/collation can store Chinese characters. It allows common
  552. // Chinese-capable charsets (utf8mb4, utf8, gbk, big5, gb18030) and panics otherwise.
  553. func checkMySQLChineseSupport(db *gorm.DB) error {
  554. // 仅检测:当前库默认字符集/排序规则 + 各表的排序规则(隐含字符集)
  555. // Read current schema defaults
  556. var schemaCharset, schemaCollation string
  557. err := db.Raw("SELECT DEFAULT_CHARACTER_SET_NAME, DEFAULT_COLLATION_NAME FROM information_schema.SCHEMATA WHERE SCHEMA_NAME = DATABASE()").Row().Scan(&schemaCharset, &schemaCollation)
  558. if err != nil {
  559. return fmt.Errorf("读取当前库默认字符集/排序规则失败 / Failed to read schema default charset/collation: %v", err)
  560. }
  561. toLower := func(s string) string { return strings.ToLower(s) }
  562. // Allowed charsets that can store Chinese text
  563. allowedCharsets := map[string]string{
  564. "utf8mb4": "utf8mb4_",
  565. "utf8": "utf8_",
  566. "gbk": "gbk_",
  567. "big5": "big5_",
  568. "gb18030": "gb18030_",
  569. }
  570. isChineseCapable := func(cs, cl string) bool {
  571. csLower := toLower(cs)
  572. clLower := toLower(cl)
  573. if prefix, ok := allowedCharsets[csLower]; ok {
  574. if clLower == "" {
  575. return true
  576. }
  577. return strings.HasPrefix(clLower, prefix)
  578. }
  579. // 如果仅提供了排序规则,尝试按排序规则前缀判断
  580. for _, prefix := range allowedCharsets {
  581. if strings.HasPrefix(clLower, prefix) {
  582. return true
  583. }
  584. }
  585. return false
  586. }
  587. // 1) 当前库默认值必须支持中文
  588. if !isChineseCapable(schemaCharset, schemaCollation) {
  589. return fmt.Errorf("当前库默认字符集/排序规则不支持中文:schema(%s/%s)。请将库设置为 utf8mb4/utf8/gbk/big5/gb18030 / Schema default charset/collation is not Chinese-capable: schema(%s/%s). Please set to utf8mb4/utf8/gbk/big5/gb18030",
  590. schemaCharset, schemaCollation, schemaCharset, schemaCollation)
  591. }
  592. // 2) 所有物理表的排序规则(隐含字符集)必须支持中文
  593. type tableInfo struct {
  594. Name string
  595. Collation *string
  596. }
  597. var tables []tableInfo
  598. if err := db.Raw("SELECT TABLE_NAME, TABLE_COLLATION FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() AND TABLE_TYPE = 'BASE TABLE'").Scan(&tables).Error; err != nil {
  599. return fmt.Errorf("读取表排序规则失败 / Failed to read table collations: %v", err)
  600. }
  601. var badTables []string
  602. for _, t := range tables {
  603. // NULL 或空表示继承库默认设置,已在上面校验库默认,视为通过
  604. if t.Collation == nil || *t.Collation == "" {
  605. continue
  606. }
  607. cl := *t.Collation
  608. // 仅凭排序规则判断是否中文可用
  609. ok := false
  610. lower := strings.ToLower(cl)
  611. for _, prefix := range allowedCharsets {
  612. if strings.HasPrefix(lower, prefix) {
  613. ok = true
  614. break
  615. }
  616. }
  617. if !ok {
  618. badTables = append(badTables, fmt.Sprintf("%s(%s)", t.Name, cl))
  619. }
  620. }
  621. if len(badTables) > 0 {
  622. // 限制输出数量以避免日志过长
  623. maxShow := 20
  624. shown := badTables
  625. if len(shown) > maxShow {
  626. shown = shown[:maxShow]
  627. }
  628. return fmt.Errorf(
  629. "存在不支持中文的表,请修复其排序规则/字符集。示例(最多展示 %d 项):%v / Found tables not Chinese-capable. Please fix their collation/charset. Examples (showing up to %d): %v",
  630. maxShow, shown, maxShow, shown,
  631. )
  632. }
  633. return nil
  634. }
  635. var (
  636. lastPingTime time.Time
  637. pingMutex sync.Mutex
  638. )
  639. func PingDB() error {
  640. pingMutex.Lock()
  641. defer pingMutex.Unlock()
  642. if time.Since(lastPingTime) < time.Second*10 {
  643. return nil
  644. }
  645. sqlDB, err := DB.DB()
  646. if err != nil {
  647. log.Printf("Error getting sql.DB from GORM: %v", err)
  648. return err
  649. }
  650. err = sqlDB.Ping()
  651. if err != nil {
  652. log.Printf("Error pinging DB: %v", err)
  653. return err
  654. }
  655. lastPingTime = time.Now()
  656. common.SysLog("Database pinged successfully")
  657. return nil
  658. }
  659. func migrateChannelPublicName() {
  660. result := DB.Model(&Channel{}).
  661. Where("public_name = '' OR public_name IS NULL").
  662. Update("public_name", gorm.Expr("name"))
  663. if result.Error != nil {
  664. common.SysError("[Migration] migrateChannelPublicName failed: " + result.Error.Error())
  665. } else if result.RowsAffected > 0 {
  666. common.SysLog(fmt.Sprintf("[Migration] migrateChannelPublicName: backfilled %d channels", result.RowsAffected))
  667. }
  668. }