Database Migrations

跨 PostgreSQL、MySQL 及常用 ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)的模式变更(schema changes)、数据迁移、回滚及零停机部署(zero-downtime deployments)的数据库迁移最佳实践。

2,037 stars 332 forksDatabases Plugin marketplace

Install

Claude Code plugin
/plugin marketplace add xu-xiang/everything-claude-code-zh

The repo is a Claude Code plugin marketplace. Run /plugin afterwards to pick the plugin that includes this skill.

skills CLI (Claude Code, Cursor, Codex and more)
npx skills add xu-xiang/everything-claude-code-zh --skill database-migrations
Manual install
git clone --depth 1 https://github.com/xu-xiang/everything-claude-code-zh.git /tmp/everything-claude-code-zh
mkdir -p ~/.claude/skills
cp -r /tmp/everything-claude-code-zh/skills/database-migrations ~/.claude/skills/database-migrations

Personal skills live in ~/.claude/skills. Use .claude/skills in a project folder to share a skill with your team.

From the SKILL.md

# 数据库迁移模式(Database Migration Patterns)

面向生产系统的安全、可逆的数据库模式变更(Schema Changes)。

## 激活时机

- 创建或修改数据库表
- 添加/删除列或索引
- 执行数据迁移(数据回填 backfill、转换 transform)
- 规划零停机(zero-downtime)模式变更
- 为新项目设置迁移工具链

## 核心原则

1. **所有变更皆为迁移** —— 严禁手动修改生产环境数据库
2. **生产环境仅向前迁移** —— 回滚需使用新的向前迁移(forward migrations)
3. **模式迁移与数据迁移分离** —— 严禁在一次迁移中混用 DDL 和 DML
4. **针对生产规模数据进行测试** —— 在 100 行数据上正常的迁移可能会导致 1000 万行数据锁表
5. **部署后的迁移不可变** —— 严禁编辑已在生产环境运行过的迁移文件

## 迁移安全核查表(Migration Safety Checklist)

在执行任何迁移之前:

- [ ] 迁移包含 UP 和 DOWN 脚本(或明确标记为不可逆)
- [ ] 对大表不执行全表锁(使用并发操作)
- [ ] 新列具有默认值或可为空(严禁添加不带默认值的 NOT NULL)
- [ ] 索引并发创建(对于现有表,不与 CREATE TABLE 同行执行)
- [ ] 数据回填与模式变更分为独立迁移
- [ ] 已针对生产数据副本进行测试
- [ ] 已记录回滚方案

## PostgreSQL 模式

### 安全地添加列

```sql
-- 推荐:可为空的列,无锁
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;
-- 这会锁定表并重写每一行
```

### 无停机添加索引

```sql
-- 糟糕:在大表上会阻塞写入
CREATE INDEX idx_users_email ON users (email);

-- 推荐:非阻塞,允许并发写入
CREATE INDEX CONCURRENTLY idx_users_email ON users (email);

-- 注意:CONCURRENTLY 不能在事务块内运行
-- 大多数迁移工具需要对此进行特殊处理
```

### 重命名列(零停机)

严禁在生产环境直接重命名。请使用“扩展-收缩(expand-contract)”模式:
Read the full skill on GitHub

Details

Skill name
database-migrations
Path
skills/database-migrations/SKILL.md
License
MIT
Repo updated
5 March 2026

FAQ

What does the Database Migrations skill do?

跨 PostgreSQL、MySQL 及常用 ORM(Prisma、Drizzle、Django、TypeORM、golang-migrate)的模式变更(schema changes)、数据迁移、回滚及零停机部署(zero-downtime deployments)的数据库迁移最佳实践。

How do I install the Database Migrations skill?

Claude Code plugin: /plugin marketplace add xu-xiang/everything-claude-code-zh. Skills go in ~/.claude/skills (personal) or .claude/skills (project), and Claude loads them automatically when a task matches the description.

Is Database Migrations popular?

It lives in xu-xiang/everything-claude-code-zh, which has 2,037 GitHub stars and 332 forks.

Related skills

Browse the most popular Claude skills, all databases skills, or write your own with the skills generator.