136 lines
5.1 KiB
Markdown
136 lines
5.1 KiB
Markdown
|
|
# 表名统一修改说明
|
|||
|
|
|
|||
|
|
## 修改时间
|
|||
|
|
2026-03-10
|
|||
|
|
|
|||
|
|
## 修改目的
|
|||
|
|
将数据库表名从复数形式改为单数形式,使其与 ORM 实体类名保持一致(除了下划线和驼峰的区别)。
|
|||
|
|
|
|||
|
|
## 修改内容
|
|||
|
|
|
|||
|
|
### 1. Flyway 迁移脚本
|
|||
|
|
创建了新的 Flyway 迁移脚本:
|
|||
|
|
- 文件:`reading-platform-java/src/main/resources/db/migration/V22__rename_tables_to_singular.sql`
|
|||
|
|
- 功能:将所有数据库表名从复数改为单数
|
|||
|
|
|
|||
|
|
### 2. 实体类 @TableName 注解更新
|
|||
|
|
更新了 36 个实体类的 `@TableName` 注解:
|
|||
|
|
|
|||
|
|
| 序号 | 原表名(复数) | 新表名(单数) | 实体类名 |
|
|||
|
|
|-----|---------------|---------------|---------|
|
|||
|
|
| 1 | `admin_users` | `admin_user` | `AdminUser` |
|
|||
|
|
| 2 | `tenants` | `tenant` | `Tenant` |
|
|||
|
|
| 3 | `teachers` | `teacher` | `Teacher` |
|
|||
|
|
| 4 | `students` | `student` | `Student` |
|
|||
|
|
| 5 | `parents` | `parent` | `Parent` |
|
|||
|
|
| 6 | `parent_students` | `parent_student` | `ParentStudent` |
|
|||
|
|
| 7 | `classes` | `clazz` | `Clazz` |
|
|||
|
|
| 8 | `class_teachers` | `class_teacher` | `ClassTeacher` |
|
|||
|
|
| 9 | `student_class_history` | `student_class_history` | `StudentClassHistory` (无需修改) |
|
|||
|
|
| 10 | `courses` | `course` | `Course` |
|
|||
|
|
| 11 | `course_versions` | `course_version` | `CourseVersion` |
|
|||
|
|
| 12 | `course_resources` | `course_resource` | `CourseResource` |
|
|||
|
|
| 13 | `course_scripts` | `course_script` | `CourseScript` |
|
|||
|
|
| 14 | `course_script_pages` | `course_script_page` | `CourseScriptPage` |
|
|||
|
|
| 15 | `course_activities` | `course_activity` | `CourseActivity` |
|
|||
|
|
| 16 | `course_packages` | `course_package` | `CoursePackage` |
|
|||
|
|
| 17 | `school_courses` | `school_course` | `SchoolCourse` |
|
|||
|
|
| 18 | `tenant_courses` | `tenant_course` | `TenantCourse` |
|
|||
|
|
| 19 | `lessons` | `lesson` | `Lesson` |
|
|||
|
|
| 20 | `lesson_feedbacks` | `lesson_feedback` | `LessonFeedback` |
|
|||
|
|
| 21 | `student_records` | `student_record` | `StudentRecord` |
|
|||
|
|
| 22 | `tasks` | `task` | `Task` |
|
|||
|
|
| 23 | `task_targets` | `task_target` | `TaskTarget` |
|
|||
|
|
| 24 | `task_completions` | `task_completion` | `TaskCompletion` |
|
|||
|
|
| 25 | `task_templates` | `task_template` | `TaskTemplate` |
|
|||
|
|
| 26 | `growth_records` | `growth_record` | `GrowthRecord` |
|
|||
|
|
| 27 | `resource_libraries` | `resource_library` | `ResourceLibrary` |
|
|||
|
|
| 28 | `resource_items` | `resource_item` | `ResourceItem` |
|
|||
|
|
| 29 | `schedule_plans` | `schedule_plan` | `SchedulePlan` |
|
|||
|
|
| 30 | `schedule_templates` | `schedule_template` | `ScheduleTemplate` |
|
|||
|
|
| 31 | `system_settings` | `system_setting` | `SystemSetting` |
|
|||
|
|
| 32 | `themes` | `theme` | `Theme` |
|
|||
|
|
| 33 | `tags` | `tag` | `Tag` |
|
|||
|
|
| 34 | `notifications` | `notification` | `Notification` |
|
|||
|
|
| 35 | `operation_logs` | `operation_log` | `OperationLog` |
|
|||
|
|
|
|||
|
|
### 3. Mapper 接口 SQL 更新
|
|||
|
|
更新了以下 Mapper 接口中的硬编码 SQL 表名:
|
|||
|
|
|
|||
|
|
| 文件 | 修改内容 |
|
|||
|
|
|-----|---------|
|
|||
|
|
| `ThemeMapper.java` | `DELETE FROM themes` → `DELETE FROM theme` |
|
|||
|
|
| `ParentMapper.java` | `DELETE FROM parents` → `DELETE FROM parent` |
|
|||
|
|
| `TenantMapper.java` | `DELETE FROM tenants` → `DELETE FROM tenant` (2 处) |
|
|||
|
|
| `TeacherMapper.java` | `DELETE FROM teachers` → `DELETE FROM teacher` |
|
|||
|
|
| `StudentMapper.java` | `DELETE FROM students` → `DELETE FROM student` |
|
|||
|
|
|
|||
|
|
## 部署步骤
|
|||
|
|
|
|||
|
|
### 步骤 1:备份数据库
|
|||
|
|
在执行迁移前,务必备份开发/生产数据库:
|
|||
|
|
```bash
|
|||
|
|
mysqldump -u root -p reading_platform > backup_$(date +%Y%m%d).sql
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 2:执行 Flyway 迁移
|
|||
|
|
启动后端服务,Flyway 会自动执行 `V22__rename_tables_to_singular.sql` 迁移脚本。
|
|||
|
|
|
|||
|
|
或者手动执行:
|
|||
|
|
```bash
|
|||
|
|
mysql -u root -p reading_platform < reading-platform-java/src/main/resources/db/migration/V22__rename_tables_to_singular.sql
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 3:验证迁移结果
|
|||
|
|
在 MySQL 中执行:
|
|||
|
|
```sql
|
|||
|
|
-- 查看所有表名
|
|||
|
|
SHOW TABLES;
|
|||
|
|
|
|||
|
|
-- 验证特定表是否存在
|
|||
|
|
SHOW TABLES LIKE 'student';
|
|||
|
|
SHOW TABLES LIKE 'student_record';
|
|||
|
|
SHOW TABLES LIKE 'lesson_feedback';
|
|||
|
|
```
|
|||
|
|
|
|||
|
|
### 步骤 4:测试功能
|
|||
|
|
启动后端服务后,测试以下功能:
|
|||
|
|
1. 用户管理(管理员、教师、学生、家长)
|
|||
|
|
2. 班级管理
|
|||
|
|
3. 课程管理
|
|||
|
|
4. 课时管理
|
|||
|
|
5. 任务管理
|
|||
|
|
6. 成长记录
|
|||
|
|
7. 资源管理
|
|||
|
|
8. 日程管理
|
|||
|
|
|
|||
|
|
## 注意事项
|
|||
|
|
|
|||
|
|
1. **外键处理**:迁移脚本中先禁用外键检查(`SET FOREIGN_KEY_CHECKS = 0`),执行完后再启用
|
|||
|
|
2. **Flyway 历史**:确保 `flyway_schema_history` 表正确记录迁移历史
|
|||
|
|
3. **生产环境**:在生产环境执行前,务必在开发/测试环境充分测试
|
|||
|
|
4. **回滚方案**:如需回滚,可以准备反向迁移脚本(将单数表名改回复数)
|
|||
|
|
|
|||
|
|
## 相关文件清单
|
|||
|
|
|
|||
|
|
### 新增文件
|
|||
|
|
- `reading-platform-java/src/main/resources/db/migration/V22__rename_tables_to_singular.sql`
|
|||
|
|
|
|||
|
|
### 修改的文件
|
|||
|
|
- 所有 Entity 类(36 个文件)
|
|||
|
|
- 5 个 Mapper 接口
|
|||
|
|
|
|||
|
|
## 验证清单
|
|||
|
|
|
|||
|
|
- [x] 所有 Entity 类的 `@TableName` 注解已更新
|
|||
|
|
- [x] 所有 Mapper 接口的 SQL 已更新
|
|||
|
|
- [x] Controller 中无硬编码表名引用
|
|||
|
|
- [x] Service 中无硬编码表名引用
|
|||
|
|
- [x] Flyway 迁移脚本已创建
|
|||
|
|
|
|||
|
|
## 后续工作
|
|||
|
|
|
|||
|
|
1. 在开发环境执行迁移并验证
|
|||
|
|
2. 更新 API 文档中的表名引用(如有)
|
|||
|
|
3. 更新项目文档中的表名引用(如有)
|