Support longer legacy token keys

This commit is contained in:
XiaoAI1024
2026-04-23 13:29:00 +08:00
parent 01c2e909a0
commit 2431efc01f
2 changed files with 26 additions and 1 deletions
+25
View File
@@ -38,6 +38,11 @@ type tokenKeyResponse struct {
Key string `json:"key"` Key string `json:"key"`
} }
type sqliteColumnInfo struct {
Name string `gorm:"column:name"`
Type string `gorm:"column:type"`
}
func setupTokenControllerTestDB(t *testing.T) *gorm.DB { func setupTokenControllerTestDB(t *testing.T) *gorm.DB {
t.Helper() t.Helper()
@@ -124,6 +129,26 @@ func decodeAPIResponse(t *testing.T, recorder *httptest.ResponseRecorder) tokenA
return response return response
} }
func TestTokenAutoMigrateUsesVarchar128KeyColumn(t *testing.T) {
db := setupTokenControllerTestDB(t)
var columns []sqliteColumnInfo
if err := db.Raw("PRAGMA table_info(tokens)").Scan(&columns).Error; err != nil {
t.Fatalf("failed to inspect token table schema: %v", err)
}
for _, column := range columns {
if column.Name == "key" {
if strings.ToLower(column.Type) != "varchar(128)" {
t.Fatalf("expected key column type varchar(128), got %q", column.Type)
}
return
}
}
t.Fatal("key column not found in token table schema")
}
func TestGetAllTokensMasksKeyInResponse(t *testing.T) { func TestGetAllTokensMasksKeyInResponse(t *testing.T) {
db := setupTokenControllerTestDB(t) db := setupTokenControllerTestDB(t)
token := seedToken(t, db, 1, "list-token", "abcd1234efgh5678") token := seedToken(t, db, 1, "list-token", "abcd1234efgh5678")
+1 -1
View File
@@ -14,7 +14,7 @@ import (
type Token struct { type Token struct {
Id int `json:"id"` Id int `json:"id"`
UserId int `json:"user_id" gorm:"index"` UserId int `json:"user_id" gorm:"index"`
Key string `json:"key" gorm:"type:char(48);uniqueIndex"` Key string `json:"key" gorm:"type:varchar(128);uniqueIndex"`
Status int `json:"status" gorm:"default:1"` Status int `json:"status" gorm:"default:1"`
Name string `json:"name" gorm:"index" ` Name string `json:"name" gorm:"index" `
CreatedTime int64 `json:"created_time" gorm:"bigint"` CreatedTime int64 `json:"created_time" gorm:"bigint"`