From adb5397473468ababe6025d7e6bcb912732d09a1 Mon Sep 17 00:00:00 2001 From: tbphp Date: Fri, 25 Jul 2025 13:02:06 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9apikey=E9=95=BF?= =?UTF-8?q?=E5=BA=A6=E4=B8=BA1024?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/db/migrations/migration.go | 9 +++- .../v1.0.16_increase_key_value_length.go | 47 +++++++++++++++++++ internal/models/types.go | 4 +- 3 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 internal/db/migrations/v1.0.16_increase_key_value_length.go diff --git a/internal/db/migrations/migration.go b/internal/db/migrations/migration.go index fac2eb6..e4c0b13 100644 --- a/internal/db/migrations/migration.go +++ b/internal/db/migrations/migration.go @@ -6,5 +6,12 @@ import ( func MigrateDatabase(db *gorm.DB) error { // v1.0.13 修复请求日志数据 - return V1_0_13_FixRequestLogs(db) + if err := V1_0_13_FixRequestLogs(db); err != nil { + return err + } + // v1.0.16 增加 key_value 字段长度 + if err := V1_0_16_IncreaseKeyValueLength(db); err != nil { + return err + } + return nil } diff --git a/internal/db/migrations/v1.0.16_increase_key_value_length.go b/internal/db/migrations/v1.0.16_increase_key_value_length.go new file mode 100644 index 0000000..0c232c1 --- /dev/null +++ b/internal/db/migrations/v1.0.16_increase_key_value_length.go @@ -0,0 +1,47 @@ +package db + +import ( + "fmt" + + "gorm.io/gorm" +) + +// V1_0_16_IncreaseKeyValueLength migrates the key_value column length. +func V1_0_16_IncreaseKeyValueLength(db *gorm.DB) error { + if err := alterColumnType(db, "api_keys", "key_value", "varchar(1024)"); err != nil { + return fmt.Errorf("failed to migrate api_keys table: %w", err) + } + + if err := alterColumnType(db, "request_logs", "key_value", "varchar(1024)"); err != nil { + return fmt.Errorf("failed to migrate request_logs table: %w", err) + } + + return nil +} + +func alterColumnType(db *gorm.DB, tableName, columnName, newType string) error { + var currentType string + switch db.Dialector.Name() { + case "sqlite": + return nil + case "mysql": + err := db.Raw("SELECT COLUMN_TYPE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = ? AND TABLE_NAME = ? AND COLUMN_NAME = ?", + db.Migrator().CurrentDatabase(), tableName, columnName).Scan(¤tType).Error + if err != nil { + return err + } + if currentType == newType { + return nil + } + return db.Exec(fmt.Sprintf("ALTER TABLE %s MODIFY COLUMN %s %s", tableName, columnName, newType)).Error + case "postgres": + err := db.Raw("SELECT data_type FROM information_schema.columns WHERE table_name = ? AND column_name = ?", + tableName, columnName).Scan(¤tType).Error + if err != nil { + return err + } + return db.Exec(fmt.Sprintf("ALTER TABLE %s ALTER COLUMN %s TYPE %s", tableName, columnName, newType)).Error + default: + return nil + } +} diff --git a/internal/models/types.go b/internal/models/types.go index 37ec4db..460b10c 100644 --- a/internal/models/types.go +++ b/internal/models/types.go @@ -62,7 +62,7 @@ type Group struct { // APIKey 对应 api_keys 表 type APIKey struct { ID uint `gorm:"primaryKey;autoIncrement" json:"id"` - KeyValue string `gorm:"type:varchar(512);not null;uniqueIndex:idx_group_key" json:"key_value"` + KeyValue string `gorm:"type:varchar(1024);not null;uniqueIndex:idx_group_key" json:"key_value"` GroupID uint `gorm:"not null;uniqueIndex:idx_group_key" json:"group_id"` Status string `gorm:"type:varchar(50);not null;default:'active'" json:"status"` RequestCount int64 `gorm:"not null;default:0" json:"request_count"` @@ -78,7 +78,7 @@ type RequestLog struct { Timestamp time.Time `gorm:"not null;index" json:"timestamp"` GroupID uint `gorm:"not null;index" json:"group_id"` GroupName string `gorm:"type:varchar(255);index" json:"group_name"` - KeyValue string `gorm:"type:varchar(512)" json:"key_value"` + KeyValue string `gorm:"type:varchar(1024)" json:"key_value"` IsSuccess bool `gorm:"not null" json:"is_success"` SourceIP string `gorm:"type:varchar(45)" json:"source_ip"` StatusCode int `gorm:"not null" json:"status_code"` From 53cf5b0b7ef1011510e10cfc994a8dc5a3bc2dc2 Mon Sep 17 00:00:00 2001 From: tbphp Date: Fri, 25 Jul 2025 13:46:16 +0800 Subject: [PATCH 2/2] =?UTF-8?q?fix:=20sourceip=2064=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/models/types.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/models/types.go b/internal/models/types.go index 460b10c..057e2ab 100644 --- a/internal/models/types.go +++ b/internal/models/types.go @@ -80,7 +80,7 @@ type RequestLog struct { GroupName string `gorm:"type:varchar(255);index" json:"group_name"` KeyValue string `gorm:"type:varchar(1024)" json:"key_value"` IsSuccess bool `gorm:"not null" json:"is_success"` - SourceIP string `gorm:"type:varchar(45)" json:"source_ip"` + SourceIP string `gorm:"type:varchar(64)" json:"source_ip"` StatusCode int `gorm:"not null" json:"status_code"` RequestPath string `gorm:"type:varchar(500)" json:"request_path"` Duration int64 `gorm:"not null" json:"duration_ms"`