- history/message_ops.go(新): Search(会话内, q/after互斥, LIKE参数化+%/_转义+大小写不敏感, 严格限定会话doc) / UpdateMessage(返回更新后Message) / DeleteMessage / messageByID / escapeLike
- history/session_pin.go(新): SetPinned; 迁移0009 chat_sessions加pinned INTEGER DEFAULT 0; store.go SessionMeta.Pinned + createSessionsTable加列
- session_preview.go: 查询加s.pinned + ORDER BY pinned DESC,(type=main) DESC,created_at ASC
- gateway history_write.go(新,保持history.go≤500): handlePatchSession(pinned+title) / handlePatchMessage / handleDeleteMessage; server.go加PATCH/DELETE /api/v1/messages/{id} + /sessions/{id}置顶
- history.go: handleListMessages读q分流Search; 契约PATCH title/置顶
- D2会话内搜索(不做跨会话,隔离会话不外泄红线); D3不做重发端点(客户端编辑后再发); D5用pinned bool
- 测试(message_ops/history_messages/migrate_pinned), build/vet/test绿+3平台交叉编译; Docs/api-client v0.30
- 未做: 编辑/删除WS实时广播(message_edited/deleted), 下批
59 lines
1.9 KiB
Go
59 lines
1.9 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
)
|
|
|
|
// columnExists reports whether table has a column named column.
|
|
func columnExists(t *testing.T, ctx context.Context, conn *sql.DB, table, column string) bool {
|
|
t.Helper()
|
|
var n int
|
|
if err := conn.QueryRowContext(ctx,
|
|
"SELECT count(*) FROM pragma_table_info(?) WHERE name = ?", table, column).Scan(&n); err != nil {
|
|
t.Fatalf("pragma_table_info(%s): %v", table, err)
|
|
}
|
|
return n == 1
|
|
}
|
|
|
|
// TestMigrate0009PinnedColumnIdempotent verifies the pinned migration (Android
|
|
// collaboration batch ③a / D5) is reversible and re-appliable: up adds the column,
|
|
// a second up is a no-op, down drops it, and a re-up restores it.
|
|
func TestMigrate0009PinnedColumnIdempotent(t *testing.T) {
|
|
t.Parallel()
|
|
ctx := context.Background()
|
|
conn := openMemory(t)
|
|
|
|
if err := MigrateUp(ctx, conn, testLogger()); err != nil {
|
|
t.Fatalf("MigrateUp: %v", err)
|
|
}
|
|
if !columnExists(t, ctx, conn, "chat_sessions", "pinned") {
|
|
t.Fatal("chat_sessions.pinned should exist after MigrateUp")
|
|
}
|
|
|
|
// Up twice: the recorded migration is skipped, so the column stays and no error.
|
|
if err := MigrateUp(ctx, conn, testLogger()); err != nil {
|
|
t.Fatalf("MigrateUp second call: %v", err)
|
|
}
|
|
if !columnExists(t, ctx, conn, "chat_sessions", "pinned") {
|
|
t.Fatal("chat_sessions.pinned should still exist after idempotent re-run")
|
|
}
|
|
|
|
// Down one step rolls back only 0009, dropping the column.
|
|
if err := MigrateDown(ctx, conn, 1, testLogger()); err != nil {
|
|
t.Fatalf("MigrateDown(1): %v", err)
|
|
}
|
|
if columnExists(t, ctx, conn, "chat_sessions", "pinned") {
|
|
t.Fatal("chat_sessions.pinned should be dropped by the 0009 down")
|
|
}
|
|
|
|
// Re-applying restores it end to end.
|
|
if err := MigrateUp(ctx, conn, testLogger()); err != nil {
|
|
t.Fatalf("re-MigrateUp: %v", err)
|
|
}
|
|
if !columnExists(t, ctx, conn, "chat_sessions", "pinned") {
|
|
t.Fatal("chat_sessions.pinned should exist again after re-MigrateUp")
|
|
}
|
|
}
|