Install command
Provided
PostToolUse hook that detects writes to migration, schema, and SQL files then runs framework-specific status checks — runs knex migrate:status for Node.js projects and python manage.py showmigrations for Django projects.
Open the source and read safety notes before installing.
Source-backed facts for citing this resource, derived directly from the registry — also available as plain text for AI assistants.
Decision playbook
Signals are present but mixed. Use the checklist below to confirm the source and operational safety for your environment.
0
78
—
No baseline selected
No major trust-signal divergence detected in the current selection.
Confirm ownership and provenance before trusting install instructions.
Source link availableRequired
Open the canonical repository and verify ownership.
Source provenance statusRequired
Marked as source-backed.
Metadata reviewed
Registry metadata indicates a reviewed listing.
Validate risk disclosures before installation or API wiring.
Safety notes presentRequired
Review the listed safety guidance before running commands.
Privacy notes presentRequired
Review data handling notes before connecting accounts or secrets.
Trust level risk gateRequired
Trust level does not block evaluation.
Check package metadata and artifact integrity signals.
Install payload available
Install or copy payload is available for review.
Package verification flag
No package verification flag provided.
Checksum metadata
No checksum provided for downloaded artifact.
Use compare context to validate trade-offs before adoption.
Compare tray has multiple entries
Add at least one more entry to compare trust differences.
Baseline comparison available
No baseline peer selected yet.
Diverging trust signals identified
No major trust-signal divergence found.
Setup at a glance
Copy-ready — paste the snippet to get started.
Install command
Provided
Config snippet
Provided
Copy snippet
Provided
Prerequisites
None
Platforms
1 listed
Difficulty
0/100
Adoption plan
Current risk score 16/100. Use staged verification before broader rollout.
Validate source and review signals before any execution.
Confirm source provenanceRequired
Source URL/provenance metadata is present.
Confirm metadata review state
Listing has review metadata.
Verify install payload
Install/config payload exists and can be inspected.
Confirm safety, privacy, and package integrity signals.
Review safety notesRequired
Safety notes are present.
Review privacy notesRequired
Privacy notes are present.
Verify package integrity metadata
No package verification/checksum metadata.
Adopt in controlled steps based on the selected plan.
Run in isolated sandbox firstRequired
Use a constrained sandbox and observe behavior across multiple tasks.
Roll out graduallyRequired
Roll out to a small cohort before wider usage.
Set monitoring and fallback
Define rollback path and monitor errors after adoption.
Evidence readiness
Required evidence gates are covered (5/6 signals complete).
Source repository/provenance is listed.
Required in this preset
Review metadata is present.
Required in this preset
Safety notes are present.
Required in this preset
Privacy notes are present.
Optional in this preset
Package integrity metadata is missing.
Optional in this preset
Install payload is available.
Required in this preset
Required evidence gates are covered for this preset.
Decision timeline
5/6 steps complete with no blocking gaps for this preset.
triage
Source/provenance metadata is available.
triage
Review metadata is available.
verify
Safety notes are available.
verify
Privacy notes are available.
verify
Package integrity metadata is missing.
rollout
Install payload is available.
No required blockers for this timeline preset.
Safety & privacy surface
3 safety and 2 privacy notes across 3 risk areas.
#!/usr/bin/env bash
# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Check if it's a migration-related file
if [[ "$FILE_PATH" == *migration* ]] || [[ "$FILE_PATH" == *schema* ]] || [[ "$FILE_PATH" == *.sql ]]; then
echo "🗃️ Database migration file detected: $FILE_PATH" >&2
# Check for Knex migrations
if [ -f "package.json" ] && grep -q "knex" package.json; then
echo "📦 Knex migration framework detected" >&2
# Knex migrations
if command -v npx &> /dev/null && npx knex --version &> /dev/null 2>&1; then
echo "🔧 Running Knex migration status check..." >&2
MIGRATION_STATUS=$(npx knex migrate:status 2>/dev/null || echo "No pending migrations")
echo "📊 Migration Status: $MIGRATION_STATUS" >&2
# Check for pending migrations
if echo "$MIGRATION_STATUS" | grep -q "pending"; then
echo "⚠️ Pending migrations detected. Run 'npx knex migrate:latest' to apply them" >&2
else
echo "✅ All migrations are up to date" >&2
fi
fi
# Django migrations
elif [ -f "manage.py" ]; then
echo "🐍 Django project detected" >&2
if command -v python &> /dev/null; then
echo "🔧 Checking Django migration status..." >&2
python manage.py showmigrations --plan 2>/dev/null | tail -5 | head -3 || echo "💡 Run 'python manage.py showmigrations' to check status" >&2
fi
# Raw SQL files
elif [[ "$FILE_PATH" == *.sql ]]; then
echo "📜 Raw SQL migration file detected" >&2
# Check file size and complexity
if [ -f "$FILE_PATH" ]; then
LINE_COUNT=$(wc -l < "$FILE_PATH" 2>/dev/null || echo "0")
echo "📊 SQL file contains $LINE_COUNT lines" >&2
# Check for potentially destructive operations
if grep -i "DROP\|DELETE\|TRUNCATE" "$FILE_PATH" >/dev/null 2>&1; then
echo "⚠️ WARNING: Potentially destructive SQL operations detected (DROP/DELETE/TRUNCATE)" >&2
echo "💡 Consider creating a backup before executing this migration" >&2
fi
# Check for common patterns
if grep -i "CREATE TABLE\|ALTER TABLE\|CREATE INDEX" "$FILE_PATH" >/dev/null 2>&1; then
echo "🏗️ Schema modification statements detected" >&2
fi
if grep -i "INSERT\|UPDATE" "$FILE_PATH" >/dev/null 2>&1; then
echo "📝 Data modification statements detected" >&2
fi
fi
fi
# General migration best practices reminder
echo "📋 Migration Best Practices:" >&2
echo " • Always backup database before running migrations" >&2
echo " • Test migrations on development/staging first" >&2
echo " • Ensure migrations are reversible when possible" >&2
echo " • Use transactions for atomic operations" >&2
else
echo "File $FILE_PATH is not a migration file, skipping analysis" >&2
fi
exit 0{
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/database-migration-runner.sh",
"matchers": [
"write",
"edit"
]
}
}
}.sql files via PostToolUse and triggers status checks automaticallynpx knex migrate:status for Node.js projects that include Knex in package.jsonpython manage.py showmigrations --plan for Django projects when manage.py is presentnpx knex migrate:latest.claude/settings.local.json~/.claude/settings.json.claude/settings.json{
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/database-migration-runner.sh",
"matchers": ["write", "edit"]
}
}
}
#!/usr/bin/env bash
# Read the tool input from stdin
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
# Check if it's a migration-related file
if [[ "$FILE_PATH" == *migration* ]] || [[ "$FILE_PATH" == *schema* ]] || [[ "$FILE_PATH" == *.sql ]]; then
echo "🗃️ Database migration file detected: $FILE_PATH" >&2
# Check for Knex migrations
if [ -f "package.json" ] && grep -q "knex" package.json; then
echo "📦 Knex migration framework detected" >&2
# Knex migrations
if command -v npx &> /dev/null && npx knex --version &> /dev/null 2>&1; then
echo "🔧 Running Knex migration status check..." >&2
MIGRATION_STATUS=$(npx knex migrate:status 2>/dev/null || echo "No pending migrations")
echo "📊 Migration Status: $MIGRATION_STATUS" >&2
# Check for pending migrations
if echo "$MIGRATION_STATUS" | grep -q "pending"; then
echo "⚠️ Pending migrations detected. Run 'npx knex migrate:latest' to apply them" >&2
else
echo "✅ All migrations are up to date" >&2
fi
fi
# Django migrations
elif [ -f "manage.py" ]; then
echo "🐍 Django project detected" >&2
if command -v python &> /dev/null; then
echo "🔧 Checking Django migration status..." >&2
python manage.py showmigrations --plan 2>/dev/null | tail -5 | head -3 || echo "💡 Run 'python manage.py showmigrations' to check status" >&2
fi
# Raw SQL files
elif [[ "$FILE_PATH" == *.sql ]]; then
echo "📜 Raw SQL migration file detected" >&2
# Check file size and complexity
if [ -f "$FILE_PATH" ]; then
LINE_COUNT=$(wc -l < "$FILE_PATH" 2>/dev/null || echo "0")
echo "📊 SQL file contains $LINE_COUNT lines" >&2
# Check for potentially destructive operations
if grep -i "DROP\|DELETE\|TRUNCATE" "$FILE_PATH" >/dev/null 2>&1; then
echo "⚠️ WARNING: Potentially destructive SQL operations detected (DROP/DELETE/TRUNCATE)" >&2
echo "💡 Consider creating a backup before executing this migration" >&2
fi
# Check for common patterns
if grep -i "CREATE TABLE\|ALTER TABLE\|CREATE INDEX" "$FILE_PATH" >/dev/null 2>&1; then
echo "🏗️ Schema modification statements detected" >&2
fi
if grep -i "INSERT\|UPDATE" "$FILE_PATH" >/dev/null 2>&1; then
echo "📝 Data modification statements detected" >&2
fi
fi
fi
# General migration best practices reminder
echo "📋 Migration Best Practices:" >&2
echo " • Always backup database before running migrations" >&2
echo " • Test migrations on development/staging first" >&2
echo " • Ensure migrations are reversible when possible" >&2
echo " • Use transactions for atomic operations" >&2
else
echo "File $FILE_PATH is not a migration file, skipping analysis" >&2
fi
exit 0
Complete hook script that detects migration files and checks migration status
#!/usr/bin/env bash
INPUT=$(cat)
TOOL_NAME=$(echo "$INPUT" | jq -r '.tool_name')
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [ -z "$FILE_PATH" ]; then
exit 0
fi
if [[ "$FILE_PATH" == *migration* ]] || [[ "$FILE_PATH" == *schema* ]] || [[ "$FILE_PATH" == *.sql ]]; then
echo "Database migration file detected: $FILE_PATH" >&2
if [ -f "package.json" ] && grep -q "knex" package.json; then
if command -v npx &> /dev/null && npx knex --version &> /dev/null 2>&1; then
echo "Running Knex migration status check..." >&2
MIGRATION_STATUS=$(npx knex migrate:status 2>/dev/null || echo "No pending migrations")
echo "Migration Status: $MIGRATION_STATUS" >&2
fi
fi
fi
exit 0
Complete hook configuration for .claude/settings.json to enable database migration detection on file write/edit
{
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/database-migration-runner.sh",
"matchers": ["write", "edit"]
}
}
}
Enhanced hook script that analyzes SQL migration files for destructive operations and schema changes
#!/usr/bin/env bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [[ "$FILE_PATH" == *.sql ]]; then
if [ -f "$FILE_PATH" ]; then
LINE_COUNT=$(wc -l < "$FILE_PATH" 2>/dev/null || echo "0")
echo "SQL file contains $LINE_COUNT lines" >&2
if grep -i "DROP\|DELETE\|TRUNCATE" "$FILE_PATH" >/dev/null 2>&1; then
echo "WARNING: Potentially destructive SQL operations detected (DROP/DELETE/TRUNCATE)" >&2
echo "Consider creating a backup before executing this migration" >&2
fi
if grep -i "CREATE TABLE\|ALTER TABLE\|CREATE INDEX" "$FILE_PATH" >/dev/null 2>&1; then
echo "Schema modification statements detected" >&2
fi
fi
fi
exit 0
Enhanced hook script for Django migration detection and status checking
#!/usr/bin/env bash
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | jq -r '.tool_input.file_path // .tool_input.path // ""')
if [[ "$FILE_PATH" == *migration* ]] && [ -f "manage.py" ]; then
echo "Django project detected" >&2
if command -v python &> /dev/null; then
echo "Checking Django migration status..." >&2
python manage.py showmigrations --plan 2>/dev/null | tail -5 | head -3 || echo "Run 'python manage.py showmigrations' to check status" >&2
fi
fi
exit 0
Verify package.json contains a knex dependency and run npm install. Check that npx knex --version succeeds. Ensure knexfile.js is present and points to the correct migrations directory.
Run npx knex migrate:list to verify migration discovery. Check that migration filenames follow the timestamp pattern Knex expects (e.g. 20240101_create_users.js). Confirm knexfile.js migrations path is correct.
Tighten path matching by adding a directory check: [[ "$FILE_PATH" == *migrations/* ]]. Adjust or remove the *.sql pattern if raw SQL files in other directories are causing false positives.
The hook warns on DROP/DELETE/TRUNCATE in any SQL context. Split destructive down-migrations into separately reviewed files, or add a comment like # rollback-safe and update the grep pattern to skip flagged files.
Use the absolute Python path from which python or which python3 instead of relying on the shell PATH inside the hook. Ensure manage.py is present in the project root and the virtualenv is activated before Claude Code starts.
Show that Database Migration Runner - Hooks is listed on HeyClaude. Paste this Markdown into your README — it renders the badge and links back to this page.
[](https://heyclau.de/entry/hooks/database-migration-runner)Database Migration Runner - Hooks side by side with 3 alternatives on trust, install, platform support, and disclosed safety notes — all from reviewed registry metadata.
Next steps differ across entries — use the actions in the table below to copy install commands and source links per resource.
| Field | PostToolUse hook that detects writes to migration, schema, and SQL files then runs framework-specific status checks — runs knex migrate:status for Node.js projects and python manage.py showmigrations for Django projects. Open dossier | A PostToolUse hook that runs the matching test runner for a changed file's language — Jest or Vitest for JS/TS, pytest for Python, go test, or Maven/Gradle for Java — scoped to the edited file. Open dossier | A Stop hook that terminates lingering database connections when a Claude Code session ends — via PostgreSQL pg_terminate_backend, MySQL KILL, Redis CLIENT KILL, and MongoDB connection cleanup. Open dossier | A PostToolUse hook that reminds you to enable native query logging for PostgreSQL, Prisma, Sequelize, and TypeORM, and flags possible N+1 patterns when you edit SQL or data-access files. Open dossier |
|---|---|---|---|---|
| Next stepsDiffers | ||||
| Trust | ||||
| Review status | ReviewedMaintainer reviewed | ReviewedMaintainer reviewed | ReviewedMaintainer reviewed | ReviewedMaintainer reviewed |
| Package trust | Package not verified | Package not verified | Package not verified | Package not verified |
| Source provenance | Source-backed | Source-backed | Source-backed | Source-backed |
| Submitter | — | — | — | — |
| Install risk | Review first | Review first | Review first | Review first |
| Notes | Safety ✓ Privacy ✓ | Safety ✓ Privacy ✓ | Safety ✓ Privacy ✓ | Safety ✓ Privacy ✓ |
| Brand | — | — | — | — |
| Category | hooks | hooks | hooks | hooks |
| Source | source-backed | source-backed | source-backed | source-backed |
| Author | JSONbored | JSONbored | JSONbored | JSONbored |
| Added | 2025-09-16 | 2025-09-16 | 2025-09-19 | 2025-10-19 |
| Platforms | Claude Code | Claude Code | Claude Code | Claude Code |
| Source repo | — | — | — | — |
| Safety notes | ✓Runs automatically after write or edit activity on migration, schema, and SQL files. Invokes knex migrate:status for Knex projects and python manage.py showmigrations for Django projects when the respective framework CLI is available. Scans raw SQL for destructive statements and reports warnings but does not apply migrations itself. | ✓Runs your project's test command automatically after each edit; tests execute arbitrary project code, so enable this only in a trusted repo and expect it to run frequently. The script invokes whichever runner it detects (npm test, npx vitest, pytest, go test, mvn/gradle); make sure the right one is installed so it does not run an unexpected command. | ✓Runs at session end and forcibly terminates database backends/clients (pg_terminate_backend, KILL, CLIENT KILL); pointed at a shared or production database it can drop other users' connections — scope it to local/dev databases and confirm the target before enabling. | ✓Runs automatically after bash, write, or edit activity and inspects files that look like query, model, repository, DAO, or SQL files. Creates .claude/logs/query-performance.log and appends database command or file-analysis events. Uses grep-based heuristics for query warnings and should not be treated as proof of a performance defect. |
| Privacy notes | ✓Reads migration, schema, SQL, package, and framework configuration files to infer migration state. Command output may reveal database names, migration names, table names, or schema details in the local hook output. | ✓Runs locally and prints test output to the hook; that output can include file paths, test names, and any data your tests log. | ✓Uses locally configured database credentials and connection details to issue termination commands; keep those credentials in environment variables, not in the hook. | ✓Reads query-related source files and may print file paths, query patterns, and database command strings to local hook output. Stores analyzed file paths and database command text in .claude/logs/query-performance.log. Database command text may include connection names, database names, or other operational details if typed directly into the command. |
| Prerequisites | — none listed | — none listed | — none listed | — none listed |
| Install | | | | — |
| Config | | | | |
| Citations | ||||
| Claim | Unclaimed | Unclaimed | Unclaimed | Unclaimed |
Source-backed guides for putting this to work.
Migrate or refactor a big codebase in safe, reviewable stages with Claude Code.
Loading live community signals…
A short, calm digest of reviewed Claude resources. Unsubscribe any time.