
When I moved hoja.in from TYPO3 7.6 to 14.3, Claude Code did a lot of the legwork for me: reading the old database, writing the import script, running the TYPO3 console. That is comfortable until you remember one simple fact. Everything the agent does in files, git can undo. Everything it does in the database, git cannot.
A sentence in a prompt like “please make a backup first” is a wish. The model follows it most of the time. “Most of the time” is not what I want between me and a broken database. So the rule lives in a hook, and the hook is code that runs every time, no matter what the model thinks.
What a PreToolUse hook is
Claude Code can run your own command before a tool call is executed. For the event PreToolUse with the matcher Bash, your script gets the planned shell command as JSON on stdin, before anything runs. It contains, among others, tool_name, tool_input.command and cwd. Your script then has two ways to answer:
- Exit code 2 blocks the call. Whatever you print to stderr is given back to Claude as the reason.
- Exit code 0 with JSON on stdout gives a finer decision:
hookSpecificOutput.permissionDecisioncan beallow,denyorask(the docs listdefertoo), with apermissionDecisionReason.
The second form is what I use, because ask is very useful: some commands are not wrong, they just need a human to say yes.
The rules I wanted
The hook belongs to a small Claude Code plugin I wrote for TYPO3 project upgrades. Its rules are deliberately few:
- Deny TYPO3 commands that write the database (
extension:setup,upgrade:run,upgrade:mark:undone,database:updateschema), also when they come throughddev exec, unless a dump younger than 24 hours exists in.project-migration/backups/or.ddev/db_snapshots/. - Ask for anything that removes or replaces data: destructive schema changes,
ddev import-db,ddev delete,DROP DATABASE,DROP TABLE,TRUNCATE. - Deny two shortcuts that hide failures:
git commit --no-verifyand Composer’s--ignore-platform-reqoptions.
Everything else passes. The hook should not make the agent useless, only stop the few commands that cannot be undone.
The script
Here is the core of it, shortened to the dump check. It needs jq.
#!/usr/bin/env bash
# .claude/hooks/guard-db.sh - PreToolUse hook for Bash
set -euo pipefail
payload="$(cat)"
cmd="$(printf '%s' "$payload" | jq -r '.tool_input.command // empty')"
cwd="$(printf '%s' "$payload" | jq -r '.cwd // empty')"
[[ -z "$cmd" ]] && exit 0
[[ -n "$cwd" && -d "$cwd" ]] && cd "$cwd"
decide() {
jq -n --arg d "$1" --arg r "$2" '{
hookSpecificOutput: {
hookEventName: "PreToolUse",
permissionDecision: $d,
permissionDecisionReason: $r
}
}'
exit 0
}
has() { printf '%s' "$cmd" | grep -Eiq -- "$1"; }
# Replacing or dropping data: always the human's call
if has 'ddev[[:space:]]+(import-db|delete)'
|| has 'drop[[:space:]]+(database|schema|table)|truncate[[:space:]]'; then
decide ask "This replaces or removes database content. Confirm only on a copy with a current dump."
fi
# Writing the database: only with a dump from the last 24 hours
if has '(typo3|t3.sh)[[:space:]]+[^|;&]*(extension:setup|upgrade:run|upgrade:mark:undone|database:updateschema)'; then
recent="$(find .project-migration/backups .ddev/db_snapshots -maxdepth 1 -mmin -1440
( -name '*.sql' -o -name '*.sql.gz' -o -name '*.gz' -o -name '*.zst' ) 2>/dev/null | head -1 || true)"
if [[ -z "$recent" ]]; then
decide deny "No database dump from the last 24 h. Run 'ddev export-db --file=.project-migration/backups/pre-step.sql.gz' or 'ddev snapshot --name=pre-step' first, then try again."
fi
fi
exit 0The deny reason matters more than it looks. Claude reads it and acts on it. With a concrete command in the reason, the agent usually takes the dump and retries on its own, which is exactly the flow I want: no human needed for the safe path, and the unsafe path does not exist.
Registering it
In the plugin, the hook sits in hooks/hooks.json and points to the script with ${CLAUDE_PLUGIN_ROOT}. For a single project without a plugin, put the same block in .claude/settings.json, which you can commit so everybody on the project gets it:
{
"hooks": {
"PreToolUse": [
{
"matcher": "Bash",
"hooks": [
{
"type": "command",
"command": ""$CLAUDE_PROJECT_DIR"/.claude/hooks/guard-db.sh"
}
]
}
]
}
}Do not forget chmod +x on the script. Inside Claude Code, the /hooks command shows which hooks are active and where they come from, which is the fastest way to see if your file was picked up at all.
Testing the hook without the agent
A hook is just a script that reads JSON. So I test it like a script, with fake payloads, and I do not need a model for that:
work="$(mktemp -d)"
probe() {
local out
out="$(jq -n --arg c "$1" --arg d "$work" '{cwd: $d, tool_input: {command: $c}}'
| .claude/hooks/guard-db.sh)"
[[ -z "$out" ]] && { echo allow; return; }
jq -r '.hookSpecificOutput.permissionDecision' <<<"$out"
}
probe 'ddev exec vendor/bin/typo3 upgrade:run' # deny
probe 'ddev import-db --file=prod.sql.gz' # ask
probe 'vendor/bin/typo3 cache:flush' # allow
mkdir -p "$work/.project-migration/backups"
touch "$work/.project-migration/backups/pre-v14.sql.gz"
probe 'ddev exec vendor/bin/typo3 upgrade:run' # now allow
touch -t 202001010000 "$work/.project-migration/backups/pre-v14.sql.gz"
probe 'ddev exec vendor/bin/typo3 extension:setup' # deny again: dump too oldOne small detail that bit me while writing this: when the hook allows, it prints nothing, and jq on empty input prints nothing either. So the probe checks for empty output itself and says allow. The plugin’s self test runs exactly this kind of list, including negative cases like git commit -m "fix -n handling", which must not be mistaken for --no-verify.
What it did not cover
I want to be honest here, because this is the real lesson. On hoja.in the biggest database write was not a TYPO3 command at all. It was my import script, started with ddev exec php .project-migration/import/import-legacy.php, which truncates pages, tt_content and sys_file_reference before importing. The hook does not match that command. The dumps before each import (pre-v14-import.sql.gz, pre-reimport.sql.gz and friends in the backups folder) exist because a dump before every import was a written step of the plan, not because the hook forced it.
Also, the hook checks that a recent file exists. It does not check that the file is a valid dump of the right database. A determined agent could satisfy it with an empty file. I accept that, because the goal is to stop accidents, not to fight an attacker. If your threat model is different, check the size and run a zcat | head on it.
My takeaway: write down which commands in your project change data, including your own scripts, and put them in the pattern. A hook only guards what it knows.
Cheers, and take that dump!