跳到正文
Skillpedia
API 与服务器(后端)9.5k · #12250

convex-migration-helper

它能做什么

这个技能会规划已有数据的 Convex 应用的数据结构变更。适合需要一份不影响用户的分步计划的开发者。

适用场景

  • ✓当你需要在已有数据的 Convex 应用中修改字段类型或拆分数据表时。
  • ✓当你要重命名或删除现有记录仍在使用的字段时。
  • ✓当你希望对后端变更进行零停机发布时。

技能链接

https://github.com/openclaw/clawhub/blob/main/.agents/skills/convex-migration-helper/SKILL.md

复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。

使用方法

最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。

可直接粘贴的消息

请用这个技能帮我:https://github.com/openclaw/clawhub/blob/main/.agents/skills/convex-migration-helper/SKILL.md 阅读该链接中的 SKILL.md 文件,并在我的任务中按照它的说明来做。
想让你的 AI 自己找到这样的技能吗?安装免费的 Skillpedia MCP:只需一行,每次任务前它都会在 15,000+ 个技能中搜索。1 分钟完成安装 →

可以这样问 AI

“请帮我规划如何把 Convex 的 users 表拆分成两张表,同时不影响我的应用。”

每周 5 个新技能,1 分钟读完。

工作原理

智能体会规划一系列步骤:先拓宽 schema,使新旧数据都能使用;然后分批迁移现有记录;所有数据更新完成后再收紧 schema。

技术内容(原始 SKILL.md)

作者在 GitHub 上的英文原文。

Convex Migration Helper

Safely migrate Convex schemas and data when making breaking changes.

When to Use

  • Adding new required fields to existing tables
  • Changing field types or structure
  • Splitting or merging tables
  • Renaming or deleting fields
  • Migrating from nested to relational data

When Not to Use

  • Greenfield schema with no existing data in production or dev
  • Adding optional fields that do not need backfilling
  • Adding new tables with no existing data to migrate
  • Adding or removing indexes with no correctness concern
  • Questions about Convex schema design without a migration need

Key Concepts

Schema Validation Drives the Workflow

Convex will not let you deploy a schema that does not match the data at rest. This is the fundamental constraint that shapes every migration:

  • You cannot add a required field if existing documents don't have it
  • You cannot change a field's type if existing documents have the old type
  • You cannot remove a field from the schema if existing documents still have it

This means migrations follow a predictable pattern: widen the schema, migrate the data, narrow the schema.

Online Migrations

Convex migrations run online, meaning the app continues serving requests while data is updated asynchronously in batches. During the migration window, your code must handle both old and new data formats.

Prefer New Fields Over Changing Types

When changing the shape of data, create a new field rather than modifying an existing one. This makes the transition safer and easier to roll back.

Don't Delete Data

Unless you are certain, prefer deprecating fields over deleting them. Mark the field as v.optional and add a code comment explaining it is deprecated and why it existed.

Safe Changes (No Migration Needed)

Adding Optional Field

// Before
users: defineTable({
  name: v.string(),
});

// After - safe, new field is optional
users: defineTable({
  name: v.string(),
  bio: v.optional(v.string()),
});

Adding New Table

posts: defineTable({
  userId: v.id("users"),
  title: v.string(),
}).index("by_user", ["userId"]);

Adding Index

users: defineTable({
  name: v.string(),
  email: v.string(),
}).index("by_email", ["email"]);

Breaking Changes: The Deployment Workflow

Every breaking migration follows the same multi-deploy pattern:

Deploy 1 - Widen the schema:

  1. Update schema to allow both old and new formats (e.g., add optional new field)
  2. Update code to handle both formats when reading
  3. Update code to write the new format for new documents
  4. Deploy

Between deploys - Migrate data:

  1. Run migration to backfill existing documents
  2. Verify all documents are migrated

Deploy 2 - Narrow the schema:

  1. Update schema to require the new format only
  2. Remove code that handles the old format
  3. Deploy

Using the Migrations Component

For any non-trivial migration, use the @convex-dev/migrations component. It handles batching, cursor-based pagination, state tracking, resume from failure, dry runs, and progress monitoring.

See references/migrations-component.md for installation, setup, defining and running migrations directly with npx convex run migrations:myMigration, dry runs, status monitoring, and configuration options.

Common Migration Patterns

See references/migration-patterns.md for complete patterns with code examples covering:

  • Adding a required field
  • Deleting a field
  • Changing a field type
  • Splitting nested data into a separate table
  • Cleaning up orphaned documents
  • Zero-downtime strategies (dual write, dual read)
  • Small table shortcut (single internalMutation without the component)
  • Verifying a migration is complete

Common Pitfalls

  1. Making a field required before migrating data: Convex rejects the deploy because existing documents lack the field. Always widen the schema first.
  2. Using .collect() on large tables: Hits transaction limits or causes timeouts. Use the migrations component for proper batched pagination. .collect() is only safe for tables you know are small.
  3. Not writing the new format before migrating: Documents created during the migration window will be missed, leaving unmigrated data after the migration "completes."
  4. Skipping the dry run: Use dryRun: true to validate migration logic before committing changes to produ
常见问题

为什么要分步骤进行?

Convex 会根据 schema 检查现有数据,因此变更必须按顺序进行,以避免部署失败。

变更期间应用会保持在线吗?

会。迁移会在后台分批运行,但代码在此期间必须能同时处理新旧数据。

什么时候不需要这个技能?

对于没有现有数据的新项目,或只是添加无需回填的可选字段时。

详情

作者的原始描述

Plans Convex schema and data migrations with widen-migrate-narrow and @convex-dev/migrations. Use for breaking schema changes, backfills, table reshaping, or zero-downtime rollouts.

repo: openclaw/clawhub

热度

GitHub 星标
9.5k
排名
#12250
许可证
MIT
更新时间
2026/11/2

这个技能有用吗?

0

登录 后即可投票。

评论

  • …

同一仓库的更多技能

相似技能