schema-migration-planner

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Schema Migration Planner

Schema 迁移规划器

Source mapping: Tier 3 specialized skill derived from
Kotlin_Spring_Developer_Pipeline.md
(
SK-11
).
来源映射:源自
Kotlin_Spring_Developer_Pipeline.md
的 Tier 3 专项 skill(
SK-11
)。

Mission

任务

Change schema without breaking live traffic, deployment order, or rollback safety. Treat schema evolution as a multi-step compatibility exercise, not a single DDL statement.
在不破坏线上流量、部署顺序或回滚安全性的前提下变更 schema。 将 schema 演进视为多步骤的兼容性演练,而不是单条 DDL 语句。

Read First

必读内容

  • Current schema and the desired target shape.
  • Current code path, target code path, and deployment model.
  • Table sizes, traffic pattern, lock sensitivity, and replication or CDC constraints.
  • The actual migration tool and dialect in use.
  • Backfill capability, rollout windows, and rollback expectations.
  • 当前 schema 与期望的目标形态。
  • 当前代码路径、目标代码路径及部署模型。
  • 表大小、流量模式、锁敏感度,以及复制或 CDC 约束。
  • 实际使用的 migration 工具与 dialect。
  • backfill 能力、发布窗口及回滚预期。

Planning Workflow

规划工作流

  1. Identify whether the change is additive, destructive, semantic, or data-moving.
  2. Determine whether old and new application versions must coexist.
  3. Plan the rollout in phases:
    • expand
    • dual write or compatibility layer
    • backfill
    • switch reads
    • contract
  4. Decide whether rollback is realistic or whether roll-forward is the safer operational model.
  5. Define smoke checks, validation queries, and observability around the migration.
  1. 判断变更属于新增型(additive)、破坏型(destructive)、语义型(semantic)还是数据迁移型(data-moving)。
  2. 确定旧版与新版应用是否必须共存。
  3. 规划分阶段发布:
    • expand(扩展)
    • dual write(双写)或 compatibility layer(兼容层)
    • backfill(回填)
    • switch reads(切换读取)
    • contract(收缩)
  4. 判断 rollback 是否现实可行,或 roll-forward 是否是更安全的运维模型。
  5. 定义 smoke checks、validation queries,以及围绕 migration 的可观测性。

Core Migration Rules

核心迁移规则

  • Add before remove.
  • Make old code tolerate new schema before making new code require it.
  • Backfill separately from latency-sensitive request paths whenever possible.
  • Use explicit indexes and constraints as part of the migration design, not as an afterthought.
  • Keep migration scripts deterministic and rerunnable according to the tool's expectations.
  • 先添加,后删除。
  • 在让新代码依赖新 schema 之前,先确保旧代码能够容忍新 schema。
  • 只要可行,就将 backfill 与对延迟敏感的请求路径分离。
  • 将显式索引和约束纳入 migration 设计,而不是事后补充。
  • 根据工具的预期,让 migration 脚本保持确定性并可重复运行。

Advanced Migration Traps

高级迁移陷阱

  • Adding a non-null column with a default may rewrite or lock a large table depending on database and version. Small DDL can still be operationally expensive.
  • Unique constraints, index builds, and foreign-key validation can be more disruptive than column adds.
  • Backfills can saturate replicas, queue workers, caches, and downstream CDC consumers even when primary write latency looks fine.
  • Rolling deploys mean old and new code may both write for a while. Dual-write compatibility must be explicit.
  • Renames are usually additive-copy-switch-drop plans in disguise. Treat them that way.
  • Destructive clean-up should happen only after proving no old readers, writers, or reports still depend on the legacy shape.
  • Trigger-based compatibility shims can help but add operational complexity and hidden coupling. Use them knowingly.
  • Migration idempotency in tooling does not automatically mean business safety. Backfill scripts and data corrections need their own idempotency story.
  • 根据数据库和版本,添加带 default 的非空列可能会重写或锁定大型表。即使是很小的 DDL,也可能带来高昂的运维成本。
  • Unique constraints、index builds 和 foreign-key validation 带来的中断影响可能比添加列更大。
  • 即使主库写入延迟看起来正常,backfill 也可能使 replicas、queue workers、caches 和下游 CDC consumers 饱和。
  • Rolling deploys 意味着新旧代码可能会在短时间内同时写入。必须明确设计 dual-write 兼容性。
  • 重命名通常只是披着外衣的 additive-copy-switch-drop 计划。应按这种方式处理。
  • 只有在确认没有旧 readers、writers 或 reports 仍依赖旧结构后,才能执行破坏性清理。
  • 基于 trigger 的 compatibility shims 可以提供帮助,但会增加运维复杂度和隐藏耦合。应明确了解其影响后再使用。
  • 工具对 migration 幂等性的支持,并不会自动带来业务层面的安全性。Backfill 脚本和数据修正需要各自具备幂等策略。

Data Evolution Nuances

数据演进细节

  • Type changes such as numeric widening, timezone reinterpretation, enum reshaping, or JSON-structure evolution can be more dangerous than simple adds and drops.
  • NOT NULL
    and uniqueness enforcement often need a staged approach: detect violations, clean data, validate constraint, then enforce strictly.
  • Backfill chunk size, ordering, pause or resume semantics, and checkpointing are part of the migration design for large tables.
  • Dual-read or shadow-read phases may be safer than immediate read switching when data transformation logic is non-trivial.
  • Online schema change tools or shadow-table strategies may be necessary when ordinary DDL locking is too expensive for the workload.
  • Replication lag and CDC downstream consumers can become the real bottleneck during backfill even when primary database metrics look acceptable.
  • 类型变更,例如 numeric widening、timezone reinterpretation、enum reshaping 或 JSON-structure evolution,可能比简单的新增和删除更危险。
  • NOT NULL
    和 uniqueness enforcement 通常需要分阶段处理:检测违规、清理数据、验证约束,然后严格执行。
  • 对于大型表,backfill 分块大小、顺序、暂停或恢复语义以及 checkpointing 都是 migration 设计的一部分。
  • 如果数据转换逻辑较复杂,dual-read 或 shadow-read 阶段可能比立即切换读取更安全。
  • 当普通 DDL 的锁对工作负载而言代价过高时,可能需要 online schema change 工具或 shadow-table 策略。
  • 即使 primary database 指标看起来可接受,replication lag 和 CDC 下游 consumers 仍可能在 backfill 期间成为真正的瓶颈。

Dialect And Tool Nuances

Dialect 与工具细节

  • Online index creation, concurrent index build, and lock behavior are vendor-specific. Plan by dialect, not by generic SQL intuition.
  • Flyway and Liquibase have different strengths for rollback modeling, checksum handling, and branching workflows. Fit the plan to the tool already in use.
  • Some schema tools treat checksum drift and edited history harshly. Never rewrite applied migrations casually.
  • Partitioned tables, sharded systems, and CDC pipelines require migration plans that account for topology, not only DDL syntax.
  • Online index creation、concurrent index build 和 lock 行为因 vendor 而异。应按 dialect 规划,而不是凭通用 SQL 直觉判断。
  • Flyway 和 Liquibase 在 rollback 建模、checksum 处理及 branching workflows 方面各有所长。应让计划适配现有工具。
  • 某些 schema 工具会严厉处理 checksum drift 和被编辑的 history。绝不要随意重写已应用的 migrations。
  • Partitioned tables、sharded systems 和 CDC pipelines 需要考虑 topology 的 migration 计划,而不只是 DDL syntax。

Expert Heuristics

专家经验法则

  • Prefer roll-forward designs over rollback fantasies when data shape already changed in production.
  • If the table is large or business-critical, separate compatibility change, data movement, and cleanup into different releases.
  • If a migration changes query shape, validate execution plans as part of the migration, not only schema correctness.
  • If zero downtime matters, prove compatibility between adjacent deploy versions explicitly.
  • Treat cleanup as a separate project step with an explicit proof threshold, not a footnote in the initial rollout plan.
  • Validate constraints against real production-shaped data before assuming the schema is enforceable.
  • If data correctness matters more than release speed, prefer longer coexistence windows over aggressive cleanup.
  • Make the migration observable: counters for rows backfilled, lag, retries, validation failures, and cutover readiness should exist before the dangerous step begins.
  • 当 data shape 已在生产环境中改变时,优先采用 roll-forward 设计,而不是对 rollback 抱有不切实际的幻想。
  • 如果表很大或属于关键业务路径,应将 compatibility change、data movement 和 cleanup 拆分为不同 release。
  • 如果 migration 改变 query shape,应将 execution plans 的验证纳入 migration,而不仅仅验证 schema 正确性。
  • 如果 zero downtime 很重要,应明确证明相邻部署版本之间的兼容性。
  • 将 cleanup 视为独立项目步骤,并设定明确的 proof threshold,而不是初始 rollout 计划中的附注。
  • 在假定 schema 可以强制执行约束之前,应使用具有真实生产形态的数据验证约束。
  • 如果 data correctness 比 release speed 更重要,应采用更长的 coexistence window,而不是激进 cleanup。
  • 让 migration 可观测:危险步骤开始前,应已具备针对 rows backfilled、lag、retries、validation failures 和 cutover readiness 的 counters。

Output Contract

输出约定

Return these sections:
  • Schema change type
    : additive, destructive, semantic, or data-moving.
  • Phased migration plan
    : the exact expand/contract sequence.
  • Compatibility story
    : how old and new code coexist safely.
  • Operational risks
    : locks, backfill load, replication, CDC, or indexing risk.
  • Verification
    : SQL checks, smoke checks, and rollout checkpoints.
  • Cleanup phase
    : what can be removed later and under what proof.
返回以下部分:
  • Schema change type
    :additive、destructive、semantic 或 data-moving。
  • Phased migration plan
    :精确的 expand/contract 顺序。
  • Compatibility story
    :旧代码与新代码如何安全共存。
  • Operational risks
    :locks、backfill load、replication、CDC 或 indexing risk。
  • Verification
    :SQL checks、smoke checks 和 rollout checkpoints。
  • Cleanup phase
    :之后可以移除哪些内容,以及需要何种证明。

Guardrails

防护规则

  • Do not recommend direct destructive DDL on live systems without a phased plan.
  • Do not assume rollback is safe once data has been transformed.
  • Do not couple request latency to large backfills unless no other option exists.
  • Do not edit historical applied migrations casually.
  • 不要在没有 phased plan 的情况下,建议对线上系统直接执行 destructive DDL。
  • 不要假定数据已被转换后 rollback 仍然安全。
  • 除非别无选择,否则不要将 request latency 与大型 backfill 耦合。
  • 不要随意编辑历史上已应用的 migrations。

Quality Bar

质量标准

A good run of this skill gives the team a deployable, compatibility-safe migration sequence with operational awareness. A bad run writes correct SQL that is still dangerous to run on a live system.
高质量执行此 skill,应为团队提供可部署、兼容性安全且具备运维意识的 migration 顺序。 低质量执行则只是写出正确的 SQL,却让它在 live system 上仍然危险运行。