数据库迁移(schema 更改)是 CI/CD 中最棘手的部分之一——与无状态应用代码不同,数据库持有状态,不能简单地替换或回滚。仔细处理迁移(自动化、向后兼容、安全模式)对于可靠的部署至关重要。
为什么数据库迁移很难
App code is stateless (replace it, roll back freely). DATABASES hold STATE:
→ can't just "roll back" a migration that dropped a column or transformed data
(the data may be gone) → rollbacks are risky/impossible for destructive changes
→ during deployment, OLD and NEW app code may run SIMULTANEOUSLY (rolling deploy) —
both must work with the database schema at that moment
→ Schema changes need careful coordination with code deployment.
在管道中自动化迁移
→ Migrations defined as CODE (versioned migration files; Flyway, Liquibase, ORM tools)
→ Run migrations as a pipeline STEP (apply pending migrations on deploy)
→ Migrations are versioned and applied in order, idempotently
→ Test migrations in staging before production
