Files
baby-tracker/sql/README.md
T
2026-09-26 03:58:48 +08:00

37 lines
2.3 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.
# SQL 数据库脚本说明
本项目所有数据库脚本均**统一在本目录 (`/sql`) 下维护**,不再散落在各子工程内部。
## 目录结构
```text
sql/
├── migration/ # Flyway 增量版本迁移脚本目录(由 Maven 打包自动装配至后端运行时)
│ ├── V1__init_mvp_schema.sql # V1: MVP 基础表结构与最小种子数据
│ ├── V2__audit_fields_and_logic_delete.sql # V2: 通用审计字段与字段清理
│ └── V3__add_breastfeeding_split_durations.sql # V3: 亲喂左侧/右侧独立耗时结构化字段
├── schema-full.sql # 当前最新完整建表与初始化数据脚本(全新环境初始化直接执行)
├── schema-evolution.sql # 数据库结构版本演进与变更记录
├── database.sql # 历史初始原型脚本(仅供归档参考)
└── README.md # 本说明文件
```
## 维护原则
1. **版本化迁移脚本 (`sql/migration/Vx__*.sql`)**:
- 所有数据库结构变更(DDL、DML、历史数据迁移)必须以 Flyway 命名规范存放于 `sql/migration/`;
- 后端 Maven 打包已通过 `<resources>` 自动将本目录映射并打包进 `target/classes/db/migration`,Spring Boot 启动时由 Flyway 自动执行增量迁移。
2. **最新全量建表脚本 (`sql/schema-full.sql`)**:
- 每次数据库表结构或核心字段变更后,需同步更新 `schema-full.sql`;
- 新部署环境、本地或容器一键初始化数据库时,直接执行 `schema-full.sql` 即可获得最新的标准数据库;
- `schema-full.sql` 末尾已内置初始化 `flyway_schema_history` 基准记录,同时 `application.yml` 配置了 `baseline-version: 3`,确保在直接执行全量脚本后,Flyway 不会误判为历史空库而重复回放早期的增量迁移。
3. **字段规范**:
- 所有表必须包含 6 大标准通用字段:
- `remark`: 备注
- `deleted`: 逻辑删除状态 (0-未删除, 1-已删除)
- `created_by`: 创建人 (默认 admin)
- `created_time`: 创建时间
- `updated_by`: 修改人 (默认 admin)
- `updated_time`: 修改时间
- 不为 `deleted` 建立单独索引。