Require Confirmation for Schema-Altering SQL
A schema change run directly through a shell (not through a reviewed migration file) is one of the easiest ways for an agent to make an unreviewable, hard-to-roll-back production change. Blocks ALTER/DROP/raw migrate invocations so they go through the normal review path instead.
1. Save this as the hook script
#!/usr/bin/env python3
"""PreToolUse hook — blocks schema-altering SQL run directly via a shell
command. Wire this to the Bash matcher."""
import json, re, sys
data = json.load(sys.stdin)
command = (data.get("tool_input") or {}).get("command", "")
PATTERNS = [r"(?i)ALTER\s+TABLE", r"(?i)DROP\s+(TABLE|COLUMN|DATABASE)",
r"manage\.py\s+migrate\b"]
if any(re.search(p, command) for p in PATTERNS):
print("BLOCKED: this looks like a direct schema change. Run it through "
"a reviewed migration file instead, or confirm manually outside the agent.", file=sys.stderr)
sys.exit(2)
sys.exit(0)
Save to a path inside your project, e.g. .claude/hooks/require-confirmation-schema-sql.sh, then chmod +x it.
2. Wire it into .claude/settings.json
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{"type": "command", "command": "python3 .claude/hooks/require-confirmation-schema-sql.py"}
]
}
]
}
}
Before you use this
A hook runs with your local shell's permissions — review the script above before adding it, the same way you'd review any script you didn't write yourself. See our guide on hook security risks for the specific CVEs this category of feature has already produced.
Related hooks