AstroResearch/docs/database.md
Asfmq 8cc2b74abc feat: 手动上传绕防爬、下载错误诊断与健康检查工具;模块化重构 API 与批量同步
后端:
  - 将 handlers.rs (1338行) 拆分为 helpers/papers/notes/sync 四模块
  - 将 batch_sync.rs 拆分为 batch/{mod,meta,asset} 三模块
  - 新增 POST /api/upload 多部件文件上传接口
  - 新增 POST /api/no_resource 标记文献"无全文资源"
  - 新增 GET/POST /api/active_bibcode 追踪活跃文献
  - StandardPaper 结构体扩展 pdf_error / html_error 错误诊断字段
  - download.rs 记录下载失败详情至数据库
  - 新增 health_check 二进制工具,支持只读扫描与 --fix 自动修复
  - 移除 scratch/ 目录、recovered_handlers.rs 及调试日志

  前端:
  - 新建 CustomSelect 可复用组件,替换全部原生 select
  - LibraryPanel:同步按钮反馈动画、下载失败/无资源状态筛选与计数、
    文献类型筛选、状态优先排序、搜索一键清空
  - 详情弹窗:错误诊断展示、手动 PDF/HTML 上传区、无资源标记/恢复
  - SearchPanel:扩展文献类型徽章、下载失败状态提示
  - SyncPanel:同步启动乐观 UI 更新、日志容器内自动滚动
  - Tab 状态 localStorage 持久化、弹窗 z-index 修复
2026-06-11 22:56:36 +08:00

116 lines
4.6 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# AstroResearch Database Schema / 数据库设计
AstroResearch 使用轻量级、零配置的 **SQLite** 数据库作为持久化存储。数据库文件默认保存在项目根目录下的 `astro_research.db`(可通过 `.env` 中的 `DATABASE_URL` 配置),由 Rust 中的 `sqlx` 驱动管理并自动执行迁移。
---
## 1. 实体关系图 (Entity-Relationship Diagram)
```mermaid
erDiagram
PAPERS {
text bibcode PK
text title
text authors "JSON Array"
text year
text pub "Journal/Publisher"
text keywords "JSON Array"
text abstract
text doi
text arxiv_id
integer citation_count
integer reference_count
text doctype "文献类型"
text pdf_path "PDF 物理路径 或 error:诊断"
text html_path "HTML 物理路径 或 error:诊断"
text markdown_path "Markdown 物理路径"
text translation_path "翻译文件物理路径"
datetime created_at
}
NOTES {
integer id PK
text bibcode FK
integer paragraph_index
text note_text
text highlight_color
text selected_text
datetime created_at
}
CITATIONS_REFERENCES {
text source_bibcode PK
text target_bibcode PK
}
SYNC_QUERIES {
integer id PK
text query "检索关键词"
text source "数据源"
integer limit_count "拉取上限"
datetime last_run "最近运行时间"
datetime created_at "创建时间"
UNIQUE_query_source_limit "唯一去重约束"
}
PAPERS ||--o{ NOTES : "has"
PAPERS ||--o{ CITATIONS_REFERENCES : "cites / cited_by"
```
---
## 2. 数据表结构详述 (Table Schema Details)
### 2.1 papers 表 (文献元数据)
存储文献的核心元数据和本地物理存储路径。
- **特殊字段说明**
- `pdf_path` / `html_path`:正常情况下存储相对路径(如 `library/PDF/2024arXiv.pdf`)。当下载失败时,会以 `error:` 前缀存储诊断信息(如 `error:Cloudflare 拦截`)。特殊值 `error:no_resource` 表示用户手动标记了"无有效全文资源"。
- `doctype`:文献类型标识,如 `article`、`eprint`、`proceedings`、`phdthesis`、`catalog`、`software`、`circular`、`book` 等。
- **索引**
- `idx_papers_doi` -> 基于 `doi`
- `idx_papers_arxiv_id` -> 基于 `arxiv_id`
### 2.2 citations_references 表 (引文与参考文献拓扑)
多对多关联表,存储文献之间的引用网络(即拓扑星系图的基础数据)。
- **复合主键**`(source_bibcode, target_bibcode)`
- **索引**
- `idx_citations_ref_source` -> 优化以 `source_bibcode` 查询参考文献
- `idx_citations_ref_target` -> 优化以 `target_bibcode` 查询被引文献
### 2.3 notes 表 (高亮与阅读笔记)
存储学者在阅读器中对特定段落创建的高亮和笔记。
- **外键**`bibcode` 级联删除 (`ON DELETE CASCADE`)。
- **索引**
- `idx_notes_bibcode` -> 优化单篇文献的笔记列表查询。
### 2.4 sync_queries 表 (同步检索条件)
存储用户保存的批量同步检索条件,支持快速重新同步。
- **唯一约束**`UNIQUE(query, source, limit_count)` 确保相同条件的检索不会重复保存。
---
## 3. 数据库迁移说明
迁移脚本存放在 `migrations/` 下,服务启动时(`src/main.rs`)会自动调用 `sqlx::migrate!().run(&pool).await` 自动部署:
| 迁移文件 | 说明 |
|:---|:---|
| `20260608000000_init.sql` | 初始化 `papers``citations_references` 结构。 |
| `20260608000001_notes.sql` | 添加 `notes` 笔记高亮表,并为关联建立级联删除。 |
| `20260608000002_add_doctype.sql` | 为 `papers` 表新增 `doctype` 文献类型字段。 |
| `20260608000003_sync_features.sql` | 添加 `sync_queries` 同步检索条件表,支持唯一去重。 |
---
## 4. 错误诊断存储约定
系统使用 `papers` 表的 `pdf_path``html_path` 字段的双重语义来同时存储正常路径和错误诊断:
| 字段值模式 | 含义 | 前端展示 |
|:---|:---|:---|
| `NULL` | 尚未尝试下载 | 琥珀色"未下载"角标 |
| `library/PDF/xxx.pdf` | 下载成功,正常物理路径 | 蓝色"已下载"角标 |
| `error:具体原因` | 下载失败,原因为前缀后的文本 | 红色"下载失败"角标,悬浮显示原因 |
| `error:no_resource` | 用户手动标记为无有效全文资源 | 灰色"无资源"角标 |
`health_check` 工具在 `--fix` 模式下会清理损坏文件并将路径重置为 `NULL`,但**不会**清除 `error:` 前缀的记录(以保留诊断线索)。