Просмотр исходного кода

Support longer legacy token keys

XiaoAI1024 2 недель назад
Родитель
Сommit
2431efc01f
2 измененных файлов с 26 добавлено и 1 удалено
  1. 25 0
      controller/token_test.go
  2. 1 1
      model/token.go

+ 25 - 0
controller/token_test.go

@@ -38,6 +38,11 @@ type tokenKeyResponse struct {
 	Key string `json:"key"`
 }
 
+type sqliteColumnInfo struct {
+	Name string `gorm:"column:name"`
+	Type string `gorm:"column:type"`
+}
+
 func setupTokenControllerTestDB(t *testing.T) *gorm.DB {
 	t.Helper()
 
@@ -124,6 +129,26 @@ func decodeAPIResponse(t *testing.T, recorder *httptest.ResponseRecorder) tokenA
 	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) {
 	db := setupTokenControllerTestDB(t)
 	token := seedToken(t, db, 1, "list-token", "abcd1234efgh5678")

+ 1 - 1
model/token.go

@@ -14,7 +14,7 @@ import (
 type Token struct {
 	Id                 int            `json:"id"`
 	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"`
 	Name               string         `json:"name" gorm:"index" `
 	CreatedTime        int64          `json:"created_time" gorm:"bigint"`