Skip to content

Commit b289c0d

Browse files
committed
refactor: 修改实体类的主键 id 为 bigint 类型
1 parent abaeea8 commit b289c0d

7 files changed

Lines changed: 129 additions & 9 deletions

File tree

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Worker Log Bigint ID Implementation Plan
2+
3+
> **For Claude:** REQUIRED SUB-SKILL: Use superpowers:executing-plans to implement this plan task-by-task.
4+
5+
**Goal:**`execution_logs.id` 改为数据库自增的整数主键,并同步代码与前端类型。
6+
7+
**Architecture:** 不新增运行时迁移逻辑,默认由用户手动删除旧表后重新建表。后端将 `WorkerLog.ID` 改为 `int64` 自增主键,写入时不再生成 UUID,前端日志类型同步改为数字。
8+
9+
**Tech Stack:** Go, GORM, SQLite, React, TypeScript
10+
11+
---
12+
13+
### Task 1: 锁定数据库自增行为
14+
15+
**Files:**
16+
- Create: `internal/db/worker_log_test.go`
17+
18+
**Step 1: Write the failing test**
19+
20+
编写数据库测试,插入两条 `WorkerLog`,断言查询结果中的 `id` 为正整数且后写入记录的 `id` 更大。
21+
22+
**Step 2: Run test to verify it fails**
23+
24+
Run: `go test ./internal/db -run TestWorkerLogInsertUsesAutoIncrementID -count=1`
25+
Expected: FAIL,因为当前 `WorkerLog.ID` 是字符串主键。
26+
27+
### Task 2: 修改模型与建表定义
28+
29+
**Files:**
30+
- Modify: `internal/model/worker_log.go`
31+
- Modify: `migrations/001_init.sql`
32+
33+
**Step 1: Write minimal implementation**
34+
35+
`WorkerLog.ID` 改为 `int64` 并标记为自增主键;初始化 SQL 改为整数自增主键定义。
36+
37+
**Step 2: Run targeted test**
38+
39+
Run: `go test ./internal/db -run TestWorkerLogInsertUsesAutoIncrementID -count=1`
40+
Expected: PASS
41+
42+
### Task 3: 移除手动分配 ID 并同步前端类型
43+
44+
**Files:**
45+
- Modify: `internal/router/server.go`
46+
- Modify: `pages/src/components/worker-log-list.tsx`
47+
- Modify: `pages/src/pages/WorkerDetailPage.tsx`
48+
49+
**Step 1: Write minimal implementation**
50+
51+
删除日志写入时的 UUID 赋值;将前端 `worker_log.id`、展开状态集合和回调参数同步为数字类型。
52+
53+
**Step 2: Run full verification**
54+
55+
Run: `gofmt -w internal/model/worker_log.go internal/db/worker_log_test.go internal/router/server.go`
56+
Run: `go test ./...`
57+
Expected: PASS

internal/db/worker_log_test.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package db
2+
3+
import (
4+
"context"
5+
"path/filepath"
6+
"testing"
7+
"time"
8+
9+
"callit/internal/model"
10+
)
11+
12+
func TestWorkerLogInsertUsesAutoIncrementID(t *testing.T) {
13+
store, err := Open(filepath.Join(t.TempDir(), "app.db"))
14+
if err != nil {
15+
t.Fatalf("open store failed: %v", err)
16+
}
17+
t.Cleanup(func() {
18+
if closeErr := store.Close(); closeErr != nil {
19+
t.Fatalf("close store failed: %v", closeErr)
20+
}
21+
})
22+
23+
ctx := context.Background()
24+
first := model.WorkerLog{
25+
WorkerID: "worker-a",
26+
RequestID: "req-1",
27+
Status: 200,
28+
DurationMS: 10,
29+
CreatedAt: time.Date(2026, 3, 18, 1, 0, 0, 0, time.UTC),
30+
}
31+
second := model.WorkerLog{
32+
WorkerID: "worker-a",
33+
RequestID: "req-2",
34+
Status: 500,
35+
DurationMS: 20,
36+
CreatedAt: time.Date(2026, 3, 18, 1, 0, 1, 0, time.UTC),
37+
}
38+
39+
if err := store.WorkerLog.Insert(ctx, first); err != nil {
40+
t.Fatalf("insert first log failed: %v", err)
41+
}
42+
if err := store.WorkerLog.Insert(ctx, second); err != nil {
43+
t.Fatalf("insert second log failed: %v", err)
44+
}
45+
46+
logs, total, err := store.WorkerLog.ListPaged(ctx, "worker-a", 1, 10)
47+
if err != nil {
48+
t.Fatalf("list logs failed: %v", err)
49+
}
50+
if total != 2 {
51+
t.Fatalf("unexpected total: got=%d want=2", total)
52+
}
53+
if len(logs) != 2 {
54+
t.Fatalf("unexpected log count: got=%d want=2", len(logs))
55+
}
56+
if logs[0].ID <= 0 {
57+
t.Fatalf("expected auto-increment id, got=%d", logs[0].ID)
58+
}
59+
if logs[1].ID <= 0 {
60+
t.Fatalf("expected auto-increment id, got=%d", logs[1].ID)
61+
}
62+
if logs[0].ID <= logs[1].ID {
63+
t.Fatalf("expected newer log id to be larger, got first=%d second=%d", logs[0].ID, logs[1].ID)
64+
}
65+
}

internal/model/worker_log.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import "time"
44

55
// WorkerLog 表示函数执行日志。
66
type WorkerLog struct {
7-
ID string `json:"id" gorm:"column:id;primaryKey;type:text"`
7+
ID int64 `json:"id" gorm:"column:id;primaryKey;autoIncrement"`
88
WorkerID string `json:"worker_id" gorm:"column:worker_id;type:text;not null;index:idx_execution_logs_worker_created,priority:1"`
99
RequestID string `json:"request_id" gorm:"column:request_id;type:text;not null;index:idx_execution_logs_request_id"`
1010
Status int `json:"status" gorm:"column:status"`

internal/router/server.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"callit/internal/requestparse"
2525

2626
"github.com/gin-gonic/gin"
27-
"github.com/google/uuid"
2827
)
2928

3029
// Server 表示 Router 服务。
@@ -376,7 +375,6 @@ func (s *Server) recordRunningLog(workerID string, requestID string, input model
376375
}
377376

378377
s.insertWorkerLogAsync(model.WorkerLog{
379-
ID: uuid.NewString(),
380378
WorkerID: workerID,
381379
RequestID: requestID,
382380
Status: statusForLog,

migrations/001_init.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ CREATE TABLE worker (
1010
);
1111

1212
CREATE TABLE execution_logs (
13-
id TEXT PRIMARY KEY,
13+
id INTEGER PRIMARY KEY AUTOINCREMENT,
1414
worker_id TEXT NOT NULL,
1515
request_id TEXT NOT NULL,
1616
status INTEGER,

pages/src/components/worker-log-list.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Button, Pagination } from "@heroui/react";
22

33
export type WorkerLogItem = {
4-
id: string;
4+
id: number;
55
worker_id: string;
66
request_id: string;
77
status: number;
@@ -17,12 +17,12 @@ export type WorkerLogItem = {
1717
type WorkerLogListProps = {
1818
loading: boolean;
1919
items: WorkerLogItem[];
20-
expandedIds: Set<string>;
20+
expandedIds: Set<number>;
2121
page: number;
2222
pageSize: number;
2323
total: number;
2424
totalPages: number;
25-
onToggle: (id: string) => void;
25+
onToggle: (id: number) => void;
2626
onPageChange: (page: number) => void;
2727
};
2828

pages/src/pages/WorkerDetailPage.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ export default function WorkerDetailPage() {
8787
const [logPage, setLogPage] = useState(1);
8888
const [logTotal, setLogTotal] = useState(0);
8989
const [logItems, setLogItems] = useState<WorkerLogItem[]>([]);
90-
const [expandedLogIds, setExpandedLogIds] = useState<Set<string>>(new Set());
90+
const [expandedLogIds, setExpandedLogIds] = useState<Set<number>>(new Set());
9191

9292
const selectedKeys = useMemo(() => (selectedFile ? new Set<string>([selectedFile]) : new Set<string>()), [selectedFile]);
9393
const defaultRunUrl = useMemo(() => {
@@ -404,7 +404,7 @@ export default function WorkerDetailPage() {
404404
setLogModalOpen(true);
405405
};
406406

407-
const toggleLogDetails = (logID: string) => {
407+
const toggleLogDetails = (logID: number) => {
408408
setExpandedLogIds((prev) => {
409409
const next = new Set(prev);
410410
if (next.has(logID)) {

0 commit comments

Comments
 (0)