nex_docus/backend/scripts/README.md

26 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# backend/scripts —— 随镜像发布的运行时脚本
这里的脚本会被 `backend/Dockerfile` 的 `COPY . .` 打进后端镜像,并在容器启动时执行:
```dockerfile
CMD ["sh", "-c", "python scripts/init_db.py && exec uvicorn main:app --host 0.0.0.0 --port 8000"]
```
因此它们**必须留在 `backend/` 内**(Compose 的 build context 就是 `./backend`,
无法 COPY 仓库根的 `scripts/`)。仓库层面的运维脚本请放到根目录 [`scripts/`](../../scripts/)。
| 脚本 | 用途 |
| --- | --- |
| `init_db.py` | 幂等地建表(`Base.metadata.create_all`)、执行结构迁移(`app.core.migrations.migrate_schema`)、初始化角色/菜单/管理员 |
| `generate_password.py` | 生成管理员密码的 bcrypt 哈希,用于手工建号等运维场景 |
## 数据库结构如何演进
1. 新增表:在 `backend/app/models/` 定义模型即可,`create_all` 会自动建表。
2. 新增列:在 `backend/app/core/migrations.py` 追加一条
`(表名, 列名, "ALTER TABLE ... ADD COLUMN ...")`,启动时幂等补齐。
3. 新增种子数据(菜单/角色等):写进 `init_db.py`,保证全新部署与存量部署结果一致。
> 历史遗留的一次性 `*.sql` / `add_*.py` 补丁脚本已在 v0.9.9 → v1.0.0 整理中删除,
> 它们的语义已完全被上述幂等机制覆盖。