database-safety

A set of database-safety rules for schema changes, queries, and data updates, including rollback paths and safeguards for large tables.

In plain words
What is it for?
Use it when writing migrations, indexes, queries, updates, or deletes, and when investigating slow queries, lock waits, or high database load.
Why use it?
It reduces risks such as being unable to undo a migration, locking a large table, or sending one database query per item in a list.

Skill for Claude CodeCodex

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add skills/wade-devcode/awesome-coding-skills-cn/database-safety
Any agent
npx skills add Wade-DevCode/awesome-coding-skills-cn --skill database-safety
Clone the repo
git clone --depth 1 https://github.com/Wade-DevCode/awesome-coding-skills-cn

Made for: Claude Code, Codex.

Per session 30 Skills are progressive disclosure: only the name and description are preloaded; the body loads when the skill is used.
When invoked 2,652 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00030 $0.02652
Opus 5 $0.00015 $0.01326
Sonnet 5 $0.00006 $0.00530
Haiku 4.5 $0.00003 $0.00265

Measured 2d ago against content hash e5ac1a81ad01, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

database-safety scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

skills/database-safety/SKILL.md · 167 lines

How it starts

The opening of the file, as written. The whole thing — 167 lines — stays where its author put it; the contents beside it link to each section on GitHub.

数据库安全

何时用

  • 新增或修改数据库迁移脚本(migration),上线前评估影响时。
  • 写查询逻辑,需要确认性能是否可接受、是否存在 N+1 时。
  • 写更新/删除语句,操作生产数据或批量变更前。
  • 遇到慢查询告警、锁等待、数据库 CPU 飙高时定位根因。

核心规则

1. 迁移可回滚,大表结构变更用在线 DDL

规则: 每个 migration 必须配 down 方法(回滚路径);对行数超过百万的表加列或建索引,必须评估锁表风险,使用在线 DDL 工具(pt-online-schema-changegh-ost 或数据库自带的 ALGORITHM=INPLACE)而非直接 ALTER TABLE

为什么: AI 生成迁移脚本时几乎从不写 down——"反正一般不需要回滚"。但生产故障时那个"一般"就变成了"现在":新代码引发 panic,需要立刻回滚版本,却发现数据库结构已经变了,应用旧版本跑不起来,只能手动改表,在最紧张的时候操作最危险的事。大表直接 ALTER TABLE ADD COLUMN 在 MySQL 5.x 上会持有表级写锁,百万行意味着分钟级锁表,期间所有写操作排队,直接触发超时告警。

怎么做:

  • 框架约定(Flyway/Liquibase/Alembic/Rails migrations)每个文件都要有回滚逻辑,CI 跑 migrate up 后紧接着跑 migrate downmigrate up,验证回滚可用。
  • 估算表行数:SELECT COUNT(*) 或查 information_schema;超过 50 万行的表结构变更,方案里必须写明用哪种在线 DDL。
  • 建索引用 CREATE INDEX CONCURRENTLY(PostgreSQL)或 ALTER TABLE ... ALGORITHM=INPLACE, LOCK=NONE(MySQL 8+),不阻塞读写。

2. 禁止在循环里查库,用批量/JOIN/预加载

规则: 任何"先取列表,再对每条记录发一次查询"的模式都是 N+1,必须改为一次批量查询或 JOIN。

为什么: AI 生成 ORM 代码时,N+1 是最高频的性能 bug,而且在小数据集的本地环境下完全感觉不到——10 条记录发 11 次查询,每次 1 ms,总耗时 11 ms,"挺快的"。到了生产环境 1000 条记录,就变成 1001 次查询,慢查询日志被打爆,数据库连接池耗尽,整个服务开始抖动。这类问题在代码 review 时也容易被忽略,因为"逻辑上没错"。

怎么做:

# 反例:N+1
orders = Order.query.all()
for order in orders:
    user = User.query.get(order.user_id)  # ❌ 每次循环一次查询
    print(user.name)

# 正例:预加载 / 批量查询
orders = Order.query.options(joinedload(Order.user)).all()  # ✅ 一次 JOIN
# 或者
user_ids = [o.user_id for o in orders]
users = {u.id: u for u in User.query.filter(User.id.in_(user_ids)).all()}
for order in orders:
    print(users[order.user_id].name)
  • 使用 ORM 时显式指定 eager loading:SQLAlchemy 用 joinedload/selectinload,Django ORM 用 select_related/prefetch_related
  • 批量插入用 bulk_insert_mappingsINSERT INTO ... VALUES (...),(...) 而非循环单条 INSERT。
  • 用 Django Debug Toolbar、SQLAlchemy 的 echo=True 或慢查询日志确认实际 SQL 数量。

3. 写操作用事务,明确隔离级别与死锁风险

规则: 涉及多张表或多条记录的写操作必须放在同一个事务里;事务要尽量短;并发写场景需评估隔离级别是否会产生幻读/不可重复读,以及多事务并发时的死锁顺序。

为什么: AI 生成的代码里最常见的是"多个 UPDATE 语句顺序执行但没有事务包裹"——前两条成功、第三条失败,数据进入不一致状态,没有任何报错,只有用户几天后发现账目对不上。另一个常见错误是事务里夹了 HTTP 调用或发邮件,事务持有行锁长达秒级,把并发吞吐量打到个位数。死锁则多发于两个事务以相反顺序锁定同两行的场景,AI 生成时不会自动对齐加锁顺序。

Read the full file on GitHub · 167 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 167 lines · 30 tokens per session scan A e5ac1a81ad01

Subscribe to this mod's changes

database-safety is a skill published in the GitHub repository Wade-DevCode/awesome-coding-skills-cn (6 stars, last pushed 2mo ago), licensed MIT. It adds 30 tokens to every session and 2,652 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-31.

Related

Other skills, from other repositories

chinese-documentation

中文文档排版参考——中英文空格、全半角标点、术语保留、链接格式、中文文案排版指北约定。仅在用户显式 /chinese-documentation 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-git-workflow

国内 Git 平台配置参考——Gitee、Coding.net、极狐 GitLab、CNB 的 SSH/HTTPS/凭据/CI 接入差异与镜像同步配置。仅在用户显式 /chinese-git-workflow 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 69 tokens

brainstorming

在任何创造性工作之前必须使用此技能——创建功能、构建组件、添加功能或修改行为。在实现之前先探索用户意图、需求和设计。.

jnMetaCode/superpowers-zh · 40 tokens

chinese-code-review

中文 review 沟通参考——话术模板、分级标注(必须修复/建议修改/仅供参考)、国内团队常见反模式应对。仅在用户显式 /chinese-code-review 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 62 tokens

chinese-commit-conventions

中文 commit 与 changelog 配置参考——Conventional Commits 中文适配、commitlint/husky/commitizen 中文模板、conventional-changelog 中文配置。仅在用户显式 /chinese-commit-conventions 时调用,不要根据上下文自动触发。.

jnMetaCode/superpowers-zh · 65 tokens

mcp-builder

MCP 服务器构建方法论 — 系统化构建生产级 MCP 工具,让 AI 助手连接外部能力.

jnMetaCode/superpowers-zh · 32 tokens