agentleFS
rui66648rui66648/agents-dev-rules/skills/database-migration/SKILL.md

database-migration

Skill0 starsChanged 12 days agoApache-2.0View on GitHub

需要修改数据库 Schema(加表、加字段、改索引)时使用。严格遵循 Alembic 迁移流程,禁止手动改表,迁移必须含 downgrade。触发词: 迁移、migration、加字段、改表、alembic。

30 other projects use a version of this.

Adopted by

Most recently taken up by

Used with

Projects with it also use security-audit and mcp-builder.

Other files people use alongside this one.

Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
  • CloudflareSkill

    security-audit

    Security guidance and vulnerability review for codebases, APIs, services, CLI tools, libraries, and daemons. Use for security questions, focused reviews, vulnerability research, security audits, or pen tests. Run the complete workflow only for explicit codebase audit or pen-test requests, full/comprehensive/end-to-end reviews, or requested report artifacts.

    Also in 3 projects that use this

  • MicrosoftSkill

    mcp-builder

    Guide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).

    Also in 3 projects that use this

The file

Files
SKILL.md

Database Migration

负责代理

database-expert — 数据库 Schema 变更和迁移由数据库专家代理负责,确保迁移脚本安全可回滚。

何时使用

  • 需要新增、修改或删除数据库表时
  • 需要新增、修改或删除表字段时
  • 需要创建/删除索引或约束时
  • 需要执行任何数据库 Schema 变更时

触发关键词: "改表"、"加字段"、"数据库变更"、"migration"、"alembic"

执行流程

Step 1: 修改 Model

在 SQLAlchemy Model 中进行变更:

# models/skill_node.py
from sqlalchemy import String, Text, Integer
from sqlalchemy.orm import Mapped, mapped_column, relationship
from sqlalchemy.ext.asyncio import AsyncAttrs
from uuid import UUID, uuid4
from typing import List

class Base(AsyncAttrs, DeclarativeBase):
    pass

class SkillNode(Base):
    __tablename__ = "skill_nodes"

    id: Mapped[UUID] = mapped_column(primary_key=True, default=uuid4)
    name: Mapped[str] = mapped_column(String(255), nullable=False, index=True)
    path: Mapped[str] = mapped_column(String(1024), nullable=False, unique=True)
    description: Mapped[str | None] = mapped_column(Text)
    difficulty: Mapped[int] = mapped_column(Integer, default=0)
    # 关系定义
    children: Mapped[List["SkillNode"]] = relationship(back_populates="parent")
    parent: Mapped["SkillNode | None"] = relationship(back_populates="children", remote_side=[id])

Step 2: 生成迁移脚本

alembic revision --autogenerate -m "add description and difficulty to skill_nodes"

Step 3: 检查生成的迁移脚本

必须打开并审查生成的迁移文件:

# migrations/versions/xxxx_add_description_and_difficulty.py
from sqlalchemy import Text, Integer

def upgrade() -> None:
    op.add_column('skill_nodes', sa.Column('description', Text(), nullable=True))
    op.add_column('skill_nodes', sa.Column('difficulty', Integer(), server_default='0'))

def downgrade() -> None:
    op.drop_column('skill_nodes', 'difficulty')
    op.drop_column('skill_nodes', 'description')

检查清单:

  • ✅ 变更范围是否符合预期 (有无多余变更)
  • ✅ upgrade 逻辑正确
  • ✅ downgrade 逻辑正确且可执行
  • ✅ 无硬编码数据库连接字符串
  • ✅ 字段类型、约束、默认值正确

Step 4: 执行迁移

# 先备份数据 (生产环境)
pg_dump -U user -d dbname > backup_before_migration.sql

# 执行迁移
alembic upgrade head

Step 5: 验证迁移结果

# 检查数据库 Schema
alembic current
alembic history --verbose

# 通过 Pytest 验证数据操作正常
pytest tests/models/test_skill_node.py -v

Step 6: 编写测试

  • 为新增字段编写 Model 测试
  • 为涉及新字段的查询编写测试
  • 确保 downgrade 也可正常工作

Step 7: pgvector 向量索引 (PostgreSQL 17+)

对于向量检索场景,使用 pgvector 扩展:

# 向量表定义
from sqlalchemy import Text
from sqlalchemy.orm import Mapped, mapped_column

class Document(Base):
    __tablename__ = "documents"
    
    id: Mapped[int] = mapped_column(primary_key=True)
    content: Mapped[str] = mapped_column(Text)
    embedding: Mapped[list[float]] = mapped_column(type_="vector(1024)")

迁移脚本中创建向量索引:

def upgrade() -> None:
    op.create_index(
        "idx_doc_embedding",
        "documents",
        ["embedding"],
        postgresql_using="hnsw",
        postgresql_ops={"embedding": "vector_cosine_ops"}
    )

def downgrade() -> None:
    op.drop_index("idx_doc_embedding")

索引策略选择:

索引类型适用场景参数建议
HNSW数据量 < 100万、查询频繁m=16, ef_construction=200
IVFFlat数据量 > 100万、内存敏感lists=sqrt(行数)

硬性规则

  • ✅ 所有 Schema 变更必须通过 Alembic 迁移
  • ✅ 迁移脚本必须包含 downgrade 逻辑
  • ✅ 生成迁移后必须人工审查 SQL
  • ✅ 生产环境迁移前必须备份数据
  • ✅ 迁移后必须运行测试验证
  • ❌ 禁止手动改表 (直接 SQL ALTER TABLE)
  • ❌ 禁止硬编码数据库连接字符串在迁移脚本中
  • ❌ 禁止跳过审查直接执行迁移
  • ❌ 禁止在生产环境直接运行未测试的迁移
  • ❌ 禁止修改迁移脚本后不更新对应 Model

反模式

  • ❌ 直接用 psql 执行 ALTER TABLE — 绕过了版本控制
  • ❌ 迁移脚本写死 postgres://user:pass@host/db — 安全漏洞
  • ❌ 不写 downgrade — 出问题时无法回滚
  • ❌ 一个迁移脚本包含 10 个不相关的变更 — 应拆分为多个迁移
  • ❌ 在 migration 里写业务逻辑 — 迁移只应处理 Schema 变更

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.