proofdb/scriptdoc/setup_database.md
2026-05-11 15:23:34 +08:00

53 lines
1.1 KiB
Markdown

# 数据库初始化脚本
## 脚本路径
```text
scripts/setup_database.php
```
## 脚本作用
初始化或升级 Proof DB 使用的 PostgreSQL 结构。
当前版本会确保:
- `archives` 表存在。
- `chunks` 表存在。
- `archives.content``archives.raw` 冗余列会被移除。
- 档案与 chunk 的常用索引存在。
- embedding / search index 相关状态字段存在。
- `updated_time` 自动更新时间触发器存在。
## 运行前提
- 当前环境中的 PostgreSQL 配置可用。
- 项目依赖已安装完成。
- 从项目根目录执行命令。
## 运行命令
```bash
php scripts/setup_database.php
```
## 成功输出示例
```text
Database connection ok: postgre
Tables initialized: archives, chunks
```
## 适用场景
- 首次部署环境。
- 拉取了数据库结构相关代码后同步 schema。
- 新增了状态字段、索引或 trigger 后补齐现有数据库。
## 常见失败信号
- `PDOException`
说明数据库地址、账号密码、网络或 DNS 有问题。
- SQL 执行错误
说明权限不足,或者现有 schema 与代码预期不一致。