From adb5397473468ababe6025d7e6bcb912732d09a1 Mon Sep 17 00:00:00 2001 From: tbphp Date: Fri, 25 Jul 2025 13:02:06 +0800 Subject: [PATCH 1/5] =?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/5] =?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"` From f1a6bb042fef39dfbfaad12190a2ab3cc22a087e Mon Sep 17 00:00:00 2001 From: tbphp Date: Fri, 25 Jul 2025 14:08:28 +0800 Subject: [PATCH 3/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E5=AD=97=E6=AE=B5?= =?UTF-8?q?=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 | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/internal/models/types.go b/internal/models/types.go index 37ec4db..057e2ab 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,9 +78,9 @@ 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"` + 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"` From a1f5547866888556f9f3e466e4b80c08086763c3 Mon Sep 17 00:00:00 2001 From: tbphp Date: Fri, 25 Jul 2025 14:10:20 +0800 Subject: [PATCH 4/5] =?UTF-8?q?revert:=20=E5=88=A0=E9=99=A4=E8=BF=81?= =?UTF-8?q?=E7=A7=BB=E8=84=9A=E6=9C=AC?= 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 ------------------- 2 files changed, 1 insertion(+), 55 deletions(-) delete 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 e4c0b13..fac2eb6 100644 --- a/internal/db/migrations/migration.go +++ b/internal/db/migrations/migration.go @@ -6,12 +6,5 @@ import ( func MigrateDatabase(db *gorm.DB) error { // v1.0.13 修复请求日志数据 - 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 + return V1_0_13_FixRequestLogs(db) } 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 deleted file mode 100644 index 0c232c1..0000000 --- a/internal/db/migrations/v1.0.16_increase_key_value_length.go +++ /dev/null @@ -1,47 +0,0 @@ -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 - } -} From f3a9ed836a729cf3da84bcb66cadb60da199527b Mon Sep 17 00:00:00 2001 From: tbphp Date: Fri, 25 Jul 2025 15:58:32 +0800 Subject: [PATCH 5/5] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=E6=96=B0=E5=BB=BA?= =?UTF-8?q?=E5=88=86=E7=BB=84=E5=88=B7=E6=96=B0=E6=95=B0=E6=8D=AE=E5=BC=82?= =?UTF-8?q?=E5=B8=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- web/src/components/keys/GroupList.vue | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/web/src/components/keys/GroupList.vue b/web/src/components/keys/GroupList.vue index 2891341..18ef1c4 100644 --- a/web/src/components/keys/GroupList.vue +++ b/web/src/components/keys/GroupList.vue @@ -62,14 +62,12 @@ function openCreateGroupModal() { showGroupModal.value = true; } -function handleGroupCreated(_group: Group) { +function handleGroupCreated(group: Group) { showGroupModal.value = false; - emit("refresh"); -} - -function handleSwitchToGroup(groupId: number) { - // 创建成功后,通知父组件刷新并切换到新创建的分组 - emit("refresh-and-select", groupId); + if (group && group.id) { + // 创建成功后,通知父组件刷新并切换到新创建的分组 + emit("refresh-and-select", group.id); + } } @@ -132,7 +130,6 @@ function handleSwitchToGroup(groupId: number) {