Database Migrations
affaan-m/ECC
数据库迁移最佳实践,涵盖模式变更、数据迁移、回滚以及零停机部署,适用于PostgreSQL、MySQL及常用ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)。
跨 PostgreSQL、MySQL 及常用 ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)的模式变更(schema changes)、数据迁移、回滚及零停机部署(zero-downtime deployments)的数据库迁移最佳实践。
$ npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a claude-codeProject install by default; add -g for ~/.claude/skills/.
$ gh skill install xu-xiang/everything-claude-code-zh database-migrations --agent claude-codeProject scope by default; add --scope user for a personal install. Needs GitHub CLI 2.90.0 or later (public preview).
$ git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git skills-src && mkdir -p .claude/skills && cp -r skills-src/skills/database-migrations .claude/skills/database-migrations && rm -rf skills-srcUse ~/.claude/skills/ instead of .claude/skills for a personal install. The folder must contain SKILL.md.
Claude Code skills documentation · loads skills from .claude/skills/
Install the "database-migrations" agent skill from https://github.com/xu-xiang/everything-claude-code-zh/tree/main/skills/database-migrations into .claude/skills/database-migrations/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "database-migrations", then confirm the skill loads.Claude Code copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$skill-installer install https://github.com/xu-xiang/everything-claude-code-zh/tree/main/skills/database-migrationsType this inside Codex. $skill-installer <name> installs a curated skill from openai/skills. The installer writes to $CODEX_HOME/skills (default ~/.codex/skills). Restart Codex if the skill does not show up.
$ npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a codexProject install goes to .agents/skills/; add -g for ~/.codex/skills/.
$ gh skill install xu-xiang/everything-claude-code-zh database-migrations --agent codexProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git skills-src && mkdir -p .agents/skills && cp -r skills-src/skills/database-migrations .agents/skills/database-migrations && rm -rf skills-srcUse ~/.agents/skills/ instead of .agents/skills for a personal install.
Codex skills documentation · loads skills from .agents/skills/
Install the "database-migrations" agent skill from https://github.com/xu-xiang/everything-claude-code-zh/tree/main/skills/database-migrations into .agents/skills/database-migrations/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "database-migrations", then confirm the skill loads.Codex copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a cursorProject install goes to .agents/skills/; add -g for ~/.cursor/skills/.
$ gh skill install xu-xiang/everything-claude-code-zh database-migrations --agent cursorProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git skills-src && mkdir -p .cursor/skills && cp -r skills-src/skills/database-migrations .cursor/skills/database-migrations && rm -rf skills-srcUse ~/.cursor/skills/ instead of .cursor/skills for a personal install.
Cursor skills documentation · loads skills from .cursor/skills/, .agents/skills/, .claude/skills/, .codex/skills/
Install the "database-migrations" agent skill from https://github.com/xu-xiang/everything-claude-code-zh/tree/main/skills/database-migrations into .cursor/skills/database-migrations/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "database-migrations", then confirm the skill loads.Cursor copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gemini skills install https://github.com/xu-xiang/everything-claude-code-zh.git --path skills/database-migrations--scope user (default) or --scope workspace; --path is the subfolder of the repo that holds the skill; --consent skips the security confirmation prompt.
$ npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a gemini-cliProject install goes to .agents/skills/; add -g for ~/.gemini/skills/.
$ gh skill install xu-xiang/everything-claude-code-zh database-migrations --agent gemini-cliProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git skills-src && mkdir -p .gemini/skills && cp -r skills-src/skills/database-migrations .gemini/skills/database-migrations && rm -rf skills-srcUse ~/.gemini/skills/ instead of .gemini/skills for a personal install, then run /skills reload.
Gemini CLI skills documentation · loads skills from .gemini/skills/, .agents/skills/
Install the "database-migrations" agent skill from https://github.com/xu-xiang/everything-claude-code-zh/tree/main/skills/database-migrations into .gemini/skills/database-migrations/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "database-migrations", then confirm the skill loads.Gemini CLI copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ gh skill install xu-xiang/everything-claude-code-zh database-migrationsInstalls for Copilot at project scope by default; add --scope user for a personal install. Preview a skill first with gh skill preview. Needs GitHub CLI 2.90.0 or later (public preview).
$ npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a github-copilotProject install goes to .agents/skills/; add -g for ~/.copilot/skills/.
$ git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git skills-src && mkdir -p .github/skills && cp -r skills-src/skills/database-migrations .github/skills/database-migrations && rm -rf skills-srcUse ~/.copilot/skills/ instead of .github/skills for a personal install. Commit .github/skills so cloud agent and code review can use it.
GitHub Copilot skills documentation · loads skills from .github/skills/, .claude/skills/, .agents/skills/
Install the "database-migrations" agent skill from https://github.com/xu-xiang/everything-claude-code-zh/tree/main/skills/database-migrations into .github/skills/database-migrations/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "database-migrations", then confirm the skill loads.GitHub Copilot copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
$ npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a opencodeOpenCode documents no install command of its own. Project install goes to .agents/skills/; add -g for ~/.config/opencode/skills/.
$ gh skill install xu-xiang/everything-claude-code-zh database-migrations --agent opencodeProject scope by default (.agents/skills/); add --scope user for a personal install.
$ git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git skills-src && mkdir -p .opencode/skills && cp -r skills-src/skills/database-migrations .opencode/skills/database-migrations && rm -rf skills-srcUse ~/.config/opencode/skills/ instead of .opencode/skills for a personal install.
OpenCode skills documentation · loads skills from .opencode/skills/, .claude/skills/, .agents/skills/
Install the "database-migrations" agent skill from https://github.com/xu-xiang/everything-claude-code-zh/tree/main/skills/database-migrations into .opencode/skills/database-migrations/ in this project. Copy the whole folder (SKILL.md and every file beside it), keep the folder name "database-migrations", then confirm the skill loads.OpenCode copies the folder itself, the same result as the manual copy. Check what it changed before you commit it.
database-migrations跨 PostgreSQL、MySQL 及常用 ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)的模式变更(schema changes)、数据迁移、回滚及零停机部署(zero-downtime deployments)的数据库迁移最佳实践。
Database Migrations is an agent skill from xu-xiang/everything-claude-code-zh. 跨 PostgreSQL、MySQL 及常用 ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)的模式变更(schema changes)、数据迁移、回滚及零停机部署(zero-downtime deployments)的数据库迁移最佳实践。
Its SKILL.md is about 1.7k tokens, which your agent loads only when the skill is triggered. It is a single SKILL.md file with no bundled scripts.
It sits in Databases, covering ORMs and data access, Database migrations and Backend development. It works with Django, Prisma, PostgreSQL and Go. The repository describes itself as: everything-claude-code 中文翻译项目:完整的 Claude Code 配置集合(agents, skills, hooks, commands, rules, MCPs)。源自 Anthropic 黑客松获胜者的实战配置,助力中文工程师高效理解与使用 Claude Code。 The licence is MIT.
5 steps, taken from the first numbered list in SKILL.md.
Read from SKILL.md and the folder at commit dfbf946. It shows what the files ask for, not the result of running them.
Pre-approves nothing: there is no allowed-tools line, so your agent's usual permission prompts apply.
From allowed-tools in the SKILL.md frontmatter.
Shell commands in SKILL.md call:
npxpythonFrom the folder's file list and the shell code blocks in SKILL.md.
No URLs in SKILL.md. Its commands use npx, which can reach the network depending on how they are called.
From URLs in SKILL.md, links to its own repository left out.
Names no API keys, tokens, secrets or passwords.
From names ending in _API_KEY, _TOKEN, _SECRET, _KEY or _PASSWORD in SKILL.md.
Database Migrations loads about 1.7k tokens when it runs. Until then it costs about 41 tokens; SKILL.md has 126 words of instructions outside code blocks.
Estimates: characters ÷ 4, the usual rule of thumb; real counts depend on the model's tokenizer. Scripts and assets cost tokens only if the agent reads them.
The automated check found no risky patterns in SKILL.md.
Automated static check — not a guarantee. Review scripts before installing. It scans the text of SKILL.md for risky patterns (piping downloads into a shell, reading credential files, hidden Unicode, destructive commands); files beside SKILL.md are not scanned.
The full file from xu-xiang/everything-claude-code-zh at commit dfbf946, republished under its MIT licence (© xu-xiang). 126 words, ~1,719 tokens.
.claude/skills/database-migrations/SKILL.md (or your agent's skills folder).面向生产系统的安全、可逆的数据库模式变更(Schema Changes)。
在执行任何迁移之前:
-- 推荐:可为空的列,无锁
ALTER TABLE users ADD COLUMN avatar_url TEXT;
-- 推荐:带默认值的列(Postgres 11+ 是瞬时的,无需重写表)
ALTER TABLE users ADD COLUMN is_active BOOLEAN NOT NULL DEFAULT true;
-- 糟糕:在现有表上添加不带默认值的 NOT NULL(需要全表重写)
ALTER TABLE users ADD COLUMN role TEXT NOT NULL;
-- 这会锁定表并重写每一行-- 糟糕:在大表上会阻塞写入
CREATE INDEX idx_users_email ON users (email);
-- 推荐:非阻塞,允许并发写入
CREATE INDEX CONCURRENTLY idx_users_email ON users (email);
-- 注意:CONCURRENTLY 不能在事务块内运行
-- 大多数迁移工具需要对此进行特殊处理严禁在生产环境直接重命名。请使用“扩展-收缩(expand-contract)”模式:
-- 第 1 步:添加新列(迁移文件 001)
ALTER TABLE users ADD COLUMN display_name TEXT;
-- 第 2 步:回填数据(迁移文件 002,数据迁移)
UPDATE users SET display_name = username WHERE display_name IS NULL;
-- 第 3 步:更新应用代码以同时读/写这两个列
-- 部署应用变更
-- 第 4 步:停止写入旧列,将其删除(迁移文件 003)
ALTER TABLE users DROP COLUMN username;-- 第 1 步:移除应用中所有对该列的引用
-- 第 2 步:部署不包含该列引用的应用
-- 第 3 步:在下一次迁移中删除列
ALTER TABLE orders DROP COLUMN legacy_status;
-- 对于 Django:使用 SeparateDatabaseAndState 从模型中移除
-- 而不生成 DROP COLUMN 指令(然后在下一次迁移中删除)-- 糟糕:在单个事务中更新所有行(会导致锁表)
UPDATE users SET normalized_email = LOWER(email);
-- 推荐:带进度的分批更新
DO $$
DECLARE
batch_size INT := 10000;
rows_updated INT;
BEGIN
LOOP
UPDATE users
SET normalized_email = LOWER(email)
WHERE id IN (
SELECT id FROM users
WHERE normalized_email IS NULL
LIMIT batch_size
FOR UPDATE SKIP LOCKED
);
GET DIAGNOSTICS rows_updated = ROW_COUNT;
RAISE NOTICE 'Updated % rows', rows_updated;
EXIT WHEN rows_updated = 0;
COMMIT;
END LOOP;
END $$;# 根据模式变更创建迁移
npx prisma migrate dev --name add_user_avatar
# 在生产环境执行待处理的迁移
npx prisma migrate deploy
# 重置数据库(仅限开发环境)
npx prisma migrate reset
# 模式变更后生成客户端
npx prisma generatemodel User {
id String @id @default(cuid())
email String @unique
name String?
avatarUrl String? @map("avatar_url")
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
orders Order[]
@@map("users")
@@index([email])
}用于 Prisma 无法表达的操作(并发索引、数据回填等):
# 仅创建空白迁移,然后手动编辑 SQL
npx prisma migrate dev --create-only --name add_email_index-- migrations/20240115_add_email_index/migration.sql
-- Prisma 无法生成 CONCURRENTLY,因此我们手动编写
CREATE INDEX CONCURRENTLY IF NOT EXISTS idx_users_email ON users (email);# 根据模式变更生成迁移文件
npx drizzle-kit generate
# 执行迁移
npx drizzle-kit migrate
# 直接推送模式(仅限开发环境,不生成迁移文件)
npx drizzle-kit pushimport { pgTable, text, timestamp, uuid, boolean } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
id: uuid("id").primaryKey().defaultRandom(),
email: text("email").notNull().unique(),
name: text("name"),
isActive: boolean("is_active").notNull().default(true),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});# 根据模型变更生成迁移文件
python manage.py makemigrations
# 执行迁移
python manage.py migrate
# 显示迁移状态
python manage.py showmigrations
# 为自定义 SQL 生成空白迁移
python manage.py makemigrations --empty app_name -n descriptionfrom django.db import migrations
def backfill_display_names(apps, schema_editor):
User = apps.get_model("accounts", "User")
batch_size = 5000
users = User.objects.filter(display_name="")
while users.exists():
batch = list(users[:batch_size])
for user in batch:
user.display_name = user.username
User.objects.bulk_update(batch, ["display_name"], batch_size=batch_size)
def reverse_backfill(apps, schema_editor):
pass # 数据迁移,无需回滚逻辑
class Migration(migrations.Migration):
dependencies = [("accounts", "0015_add_display_name")]
operations = [
migrations.RunPython(backfill_display_names, reverse_backfill),
]从 Django 模型中移除列,但不立即从数据库中删除:
class Migration(migrations.Migration):
operations = [
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RemoveField(model_name="user", name="legacy_field"),
],
database_operations=[], # 暂时不触碰数据库
),
]# 创建一对迁移文件
migrate create -ext sql -dir migrations -seq add_user_avatar
# 执行所有待处理的迁移
migrate -path migrations -database "$DATABASE_URL" up
# 回滚最后一次迁移
migrate -path migrations -database "$DATABASE_URL" down 1
# 强制指定版本(修复 dirty 状态)
migrate -path migrations -database "$DATABASE_URL" force VERSION-- migrations/000003_add_user_avatar.up.sql
ALTER TABLE users ADD COLUMN avatar_url TEXT;
CREATE INDEX CONCURRENTLY idx_users_avatar ON users (avatar_url) WHERE avatar_url IS NOT NULL;
-- migrations/000003_add_user_avatar.down.sql
DROP INDEX IF EXISTS idx_users_avatar;
ALTER TABLE users DROP COLUMN IF EXISTS avatar_url;对于关键生产变更,遵循“扩展-收缩(expand-contract)”模式:
阶段 1:扩展(EXPAND)
- 添加新列/表(可为空或带默认值)
- 部署:应用同时写入旧列和新列
- 回填现有数据
阶段 2:迁移(MIGRATE)
- 部署:应用从新列读取,同时写入旧列和新列
- 验证数据一致性
阶段 3:收缩(CONTRACT)
- 部署:应用仅使用新列
- 在独立的迁移中删除旧列/表第 1 天:通过迁移添加 new_status 列(可为空)
第 1 天:部署应用 v2 —— 同时写入 status 和 new_status
第 2 天:为现有行运行数据回填迁移
第 3 天:部署应用 v3 —— 仅从 new_status 读取
第 7 天:通过迁移删除旧的 status 列| 反模式 | 失败原因 | 更好做法 |
|---|---|---|
| 在生产环境手动执行 SQL | 无审计跟踪,不可重复 | 始终使用迁移文件 |
| 编辑已部署的迁移 | 导致环境间出现差异(drift) | 创建新的迁移文件 |
| 不带默认值的 NOT NULL | 锁表,重写所有行 | 先添加可为空列,回填数据,再添加约束 |
| 在大表上使用内联索引 | 在构建期间阻塞写入 | 使用 CREATE INDEX CONCURRENTLY |
| 模式与数据混在同一个迁移中 | 难以回滚,事务过长 | 将其分为独立的迁移 |
| 在移除代码前删除列 | 应用因缺少列而报错 | 先移除代码,在下一次部署时删除列 |
© xu-xiang, MIT. Rendered from Markdown: HTML in the file is shown as text, images as links, and headings moved down two levels. Raw file
Just SKILL.md in skills/database-migrations of xu-xiang/everything-claude-code-zh.
Open the folder on GitHubat commit dfbf946
Database Migrations next to the 5 skills that share the most tags, products or categories with it. Stars are the repository's; “used in” counts other GitHub owners with a copy.
| Skill | Stars | Used in | Tokens | Auto-check | Licence | Repo updated |
|---|---|---|---|---|---|---|
| Database Migrations this skillxu-xiang/everything-claude-code-zh | 2k | — | ~1.7k | Automated safety check: Pass | MIT | |
| Database Migrationsaffaan-m/ECC | 276k | 3 repos | ~1.9k | Automated safety check: Pass | MIT | |
| Database Migrationsaffaan-m/ECC | 276k | 1 repos | ~2.4k | Automated safety check: Pass | MIT | |
| Database Migrationsaffaan-m/ECC | 276k | — | ~335 | Automated safety check: Pass | MIT | |
| Safe Database Migration Patternsaffaan-m/ECC | 276k | — | ~3.3k | Automated safety check: Pass | MIT | |
| Database Migrationsaffaan-m/ECC | 276k | 4 repos | ~3k | Automated safety check: Pass | MIT |
affaan-m/ECC
数据库迁移最佳实践,涵盖模式变更、数据迁移、回滚以及零停机部署,适用于PostgreSQL、MySQL及常用ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)。
affaan-m/ECC
Şema değişiklikleri, veri migration'ları, rollback'ler ve PostgreSQL, MySQL ve yaygın ORM'ler (Prisma, Drizzle, Django, TypeORM, golang-migrate) arasında sıfır kesinti deployment'ları için…
affaan-m/ECC
PostgreSQL、MySQL、一般的なORM(Prisma、Drizzle、Kysely、Django、TypeORM、golang-migrate)全体のスキーマ変更、データマイグレーション、ロールバック、ゼロダウンタイムデプロイメントのためのデータベースマイグレーションベストプラクティス。
affaan-m/ECC
Rules and examples for safe, reversible schema changes in production: zero-downtime column and index changes, large data backfills and ORM migration workflows.
affaan-m/ECC
Safe, reversible database migration patterns: forward-only production changes, expand-contract zero-downtime renames, concurrent indexes, batched backfills, and per-tool workflows for PostgreSQL…
cin12211/orca-q
Database performance optimization, schema design, query analysis, and connection management across PostgreSQL, MySQL, MongoDB, and SQLite with ORM integration.
xu-xiang/everything-claude-code-zh
Everything Claude Code 的交互式安装程序 — 引导用户选择并安装技能和规则到用户级或项目级目录,验证路径,并可选择优化已安装文件。
xu-xiang/everything-claude-code-zh
基于本能(Instinct)的学习系统,通过钩子(hooks)观察会话,创建带有置信度评分的原子本能,并将其演化为技能(Skills)、命令(Commands)或智能体(Agents)。v2.1 版本增加了项目作用域(project-scoped)的本能,以防止跨项目污染。
xu-xiang/everything-claude-code-zh
生产级 API 的 REST API 设计模式,包括资源命名、状态码、分页、过滤、错误响应、版本控制和速率限制. An agent skill from xu-xiang/everything-claude-code-zh.
xu-xiang/everything-claude-code-zh
后端架构模式、API 设计、数据库优化以及适用于 Node.js、Express 和 Next.js API 路由的服务端最佳实践。
xu-xiang/everything-claude-code-zh
后端架构模式、API 设计、数据库优化以及 Node.js、Express 和 Next.js API 路由的服务端最佳实践。
xu-xiang/everything-claude-code-zh
后端架构模式、API 设计、数据库优化以及针对 Node.js、Express 和 Next.js API 路由的服务端最佳实践。
Categories
跨 PostgreSQL、MySQL 及常用 ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)的模式变更(schema changes)、数据迁移、回滚及零停机部署(zero-downtime deployments)的数据库迁移最佳实践。. Database Migrations is an agent skill from xu-xiang/everything-claude-code-zh.
Database Migrations fits situations like: tasks that involve ORMs and data access; tasks that involve Database migrations; tasks that involve Backend development.
Run `npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a claude-code`. Or copy the skill folder (skills/database-migrations in xu-xiang/everything-claude-code-zh) into .claude/skills/database-migrations in your project. Claude Code loads it when a task matches its description.
Run `npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a codex`. Or copy the skill folder (skills/database-migrations in xu-xiang/everything-claude-code-zh) into .agents/skills/database-migrations in your project. Codex loads it when a task matches its description.
Cursor, Gemini CLI, GitHub Copilot and OpenCode also load SKILL.md folders. With the skills CLI, run `npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations -a cursor` (or -a gemini-cli, github-copilot or opencode for the others). To copy it by hand, put the folder in .cursor/skills/database-migrations, .gemini/skills/database-migrations, .github/skills/database-migrations and .opencode/skills/database-migrations in your project.
Going by SKILL.md and its folder, Database Migrations needs the command-line tools its instructions call (npx and python). Our summary lists: Python 3; Node.js.
SKILL.md contains no URLs. Its commands use npx, which can reach the network depending on how they are called. This is read from the text; nothing was executed.
Our automated static check of SKILL.md found no risky patterns, such as piping downloads into a shell, reading credential files or hidden Unicode. It is not a guarantee. Review the folder before installing.
Database Migrations is published under the MIT licence (the repository's licence). It allows redistribution, so the full SKILL.md is shown on this page.
About 1.7k tokens (SKILL.md is roughly 6.9k characters). Agents keep only the skill's name and description in context until a task matches; then they load SKILL.md in full.
Skills that share tags, products or a category with Database Migrations: Database Migrations (affaan-m/ECC, 276k stars), Database Migrations (affaan-m/ECC, 276k stars), Database Migrations (affaan-m/ECC, 276k stars) and Safe Database Migration Patterns (affaan-m/ECC, 276k stars). The comparison table on this page puts their stars, adoption, token cost, safety result and licence side by side.
xu-xiang (a GitHub user) maintains it in xu-xiang/everything-claude-code-zh, which has 1,976 GitHub stars. The repository holds 78 skills in this directory. The repository was last updated on March 5, 2026.
Source: xu-xiang/everything-claude-code-zh on GitHub. Facts on this page come from the repository at the commit we read; the author's words are quoted as theirs.