database-migrations
数据库迁移最佳实践,涵盖模式变更、数据迁移、回滚以及零停机部署,适用于PostgreSQL、MySQL及常用ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)。
Original · translation not yet availableTarget: Databases. Automatically collected · unverified.
Excerpt from the author’s documentation
Extracted from a fixed version of SKILL.md in its original language. This is the author’s documentation; installation, compatibility and runtime results have not been verified by this site.
Read the original Chinese text
Topics in the source
- 数据库迁移模式
- 何时激活
- 核心原则
- 迁移安全检查清单
- PostgreSQL 模式
- 安全地添加列
- 无停机添加索引
- 重命名列(零停机)
为生产系统提供安全、可逆的数据库模式变更。 创建或修改数据库表 添加/删除列或索引 运行数据迁移(回填、转换) 计划零停机模式变更 为新项目设置迁移工具 1. 每个变更都是一次迁移 — 切勿手动更改生产数据库 2. 迁移在生产环境中是只进不退的 — 回滚使用新的前向迁移 3. 模式迁移和数据迁移是分开的 — 切勿在一个迁移中混合 DDL 和 DML 4. 针对生产规模的数据测试迁移 — 适用于 100 行的迁移可能在 1000 万行时锁定 5. 迁移一旦部署就是不可变的 — 切勿编辑已在生产中运行的迁移 应用任何迁移之前: \[ ] 迁移同时包含 UP 和 DOWN(或明确标记为不可逆) \[ ] 对大表没有全表锁(使用并发操作) \[ ] 新列有默认值或可为空(切勿添加没有默认值的 NOT NULL) \[ ] 索引是并发创建的(对于现有表,不与 CREATE TABLE 内联创建) \[ ] 数据回填是与模式变更分开的迁移 \[ ] 已针对生产数据副本进行测试 \[ ] 回滚计划已记录 切勿在生产中直接重命名。使用扩展-收缩模式: 对于 Prisma 无法表达的操作(并发索引、数据回填): 从 Django 模型中删除列,而不立即从数据库中删除: 对于关键的生产变更,遵循扩展-收缩模式: | 反模式 | 为何会失败 | 更好的方法 | |-------------|-------------|-----------------| | 在生产中手动执行 SQL | 没有审计追踪,不可重复 | 始终使用迁移文件 | | 编辑已部署的迁移 | 导致环境间出现差异 | 改为创建新迁移 | | 没有默认值的 NOT NULL | 锁定表,重写所有行 | 添加可为空列,回填数据,然后添加约束 | | 在大表上内联创建索引 | 在构建期间阻塞写入 | 使用 CREATE INDEX CONCURRENTLY | | 在一个迁移中混合模式和数据的变更 | 难以回滚,事务时间长 | 分开的迁移 | | 在移除代码之前删除列 | 应用程序在缺失列时出错 | 先移除代码,下一次部署再删除列 |
- Source repository
- affaan-m/ECC
- Source file
- docs/zh-CN/skills/database-migrations/SKILL.md
- Fixed revision
- d2ef2680594f9a17526a6e1a2e18b3d242c5d17b
Sources and evidence
This record was collected automatically. Its documentation, suitability and runtime behavior have not been manually verified.
Author: affaan-m/ECC · original repository metadata. Documentation review: Not reviewed. Last source check: 2026-11-09T14:01:50.302Z.
Categories are inferred from names, descriptions and paths. They do not establish compatibility.
License and attribution
MIT. Third-party content and copyright remain with their authors. Refer to the original license and terms.
Original license ↗Synchronization, copying and user-reported success do not mark a resource as runtime-tested.
Limitations and notes
- Collecting metadata does not mean this site has installed, adapted, runtime-tested or security-reviewed the resource.