Loading...
Automatically generates Prisma client and creates migrations when schema.prisma is modified
{
"hookConfig": {
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/prisma-schema-sync.sh",
"matchers": [
"write",
"edit"
]
}
}
},
"scriptContent": "#!/bin/bash\n\n# Read the tool input from stdin\nINPUT=$(cat)\nTOOL_NAME=$(echo \"$INPUT\" | jq -r '.tool_name')\nFILE_PATH=$(echo \"$INPUT\" | jq -r '.tool_input.file_path // .tool_input.path // \"\"')\n\nif [ -z \"$FILE_PATH\" ]; then\n exit 0\nfi\n\n# Check if this is a Prisma schema file\nif [[ \"$FILE_PATH\" == *schema.prisma ]]; then\n echo \"đ Prisma Schema Sync - Processing schema changes...\"\n echo \"đ Schema file: $FILE_PATH\"\n \n # Check if Prisma is available\n if ! command -v npx >/dev/null 2>&1; then\n echo \"â ī¸ npx not found. Please install Node.js and npm\"\n exit 1\n fi\n \n # Step 1: Validate schema\n echo \"đ Validating Prisma schema...\"\n if npx prisma validate; then\n echo \"â
Schema validation passed\"\n else\n echo \"â Schema validation failed - Please fix errors before proceeding\"\n exit 1\n fi\n \n # Step 2: Format schema\n echo \"đ Formatting Prisma schema...\"\n if npx prisma format; then\n echo \"â
Schema formatted successfully\"\n else\n echo \"â ī¸ Schema formatting failed\"\n fi\n \n # Step 3: Generate Prisma client\n echo \"đī¸ Generating Prisma client...\"\n if npx prisma generate; then\n echo \"â
Prisma client generated successfully\"\n else\n echo \"â Prisma client generation failed\"\n exit 1\n fi\n \n # Step 4: Create migration (dev mode only)\n if [ \"$NODE_ENV\" != \"production\" ]; then\n echo \"đī¸ Creating database migration...\"\n MIGRATION_NAME=\"auto_migration_$(date +%Y%m%d_%H%M%S)\"\n \n if npx prisma migrate dev --name \"$MIGRATION_NAME\" --create-only; then\n echo \"â
Migration created: $MIGRATION_NAME\"\n echo \"â ī¸ Please review the migration before applying it to your database\"\n echo \"đĄ Apply with: npx prisma migrate dev\"\n else\n echo \"â ī¸ Migration creation skipped or failed\"\n fi\n else\n echo \"âšī¸ Production environment - skipping migration creation\"\n fi\n \n echo \"\"\n echo \"đĄ Prisma Sync Tips:\"\n echo \" âĸ Review generated migrations before applying\"\n echo \" âĸ Use 'npx prisma studio' to explore your database\"\n echo \" âĸ Run 'npx prisma db push' for prototyping\"\n echo \" âĸ Use 'npx prisma migrate reset' to reset development database\"\n \n echo \"\"\n echo \"đ¯ Prisma schema sync complete!\"\n \nelse\n echo \"âšī¸ File is not a Prisma schema file: $FILE_PATH\"\nfi\n\nexit 0"
}.claude/hooks/~/.claude/hooks/{
"hooks": {
"postToolUse": {
"script": "./.claude/hooks/prisma-schema-sync.sh",
"matchers": [
"write",
"edit"
]
}
}
}#!/bin/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 this is a Prisma schema file
if [[ "$FILE_PATH" == *schema.prisma ]]; then
echo "đ Prisma Schema Sync - Processing schema changes..."
echo "đ Schema file: $FILE_PATH"
# Check if Prisma is available
if ! command -v npx >/dev/null 2>&1; then
echo "â ī¸ npx not found. Please install Node.js and npm"
exit 1
fi
# Step 1: Validate schema
echo "đ Validating Prisma schema..."
if npx prisma validate; then
echo "â
Schema validation passed"
else
echo "â Schema validation failed - Please fix errors before proceeding"
exit 1
fi
# Step 2: Format schema
echo "đ Formatting Prisma schema..."
if npx prisma format; then
echo "â
Schema formatted successfully"
else
echo "â ī¸ Schema formatting failed"
fi
# Step 3: Generate Prisma client
echo "đī¸ Generating Prisma client..."
if npx prisma generate; then
echo "â
Prisma client generated successfully"
else
echo "â Prisma client generation failed"
exit 1
fi
# Step 4: Create migration (dev mode only)
if [ "$NODE_ENV" != "production" ]; then
echo "đī¸ Creating database migration..."
MIGRATION_NAME="auto_migration_$(date +%Y%m%d_%H%M%S)"
if npx prisma migrate dev --name "$MIGRATION_NAME" --create-only; then
echo "â
Migration created: $MIGRATION_NAME"
echo "â ī¸ Please review the migration before applying it to your database"
echo "đĄ Apply with: npx prisma migrate dev"
else
echo "â ī¸ Migration creation skipped or failed"
fi
else
echo "âšī¸ Production environment - skipping migration creation"
fi
echo ""
echo "đĄ Prisma Sync Tips:"
echo " âĸ Review generated migrations before applying"
echo " âĸ Use 'npx prisma studio' to explore your database"
echo " âĸ Run 'npx prisma db push' for prototyping"
echo " âĸ Use 'npx prisma migrate reset' to reset development database"
echo ""
echo "đ¯ Prisma schema sync complete!"
else
echo "âšī¸ File is not a Prisma schema file: $FILE_PATH"
fi
exit 0npx prisma generate fails with 'generator not found' error
Prisma client not installed or wrong version. Run: 'npm install @prisma/client' matching schema generator. Verify: 'npx prisma version' showing versions. Re-generate with full schema path.
Migration creation hangs waiting for database connection that fails
DATABASE_URL missing or incorrect in .env. Verify: 'echo $DATABASE_URL' showing connection. Test: 'npx prisma db pull' for connectivity. Or use '--skip-generate' flag bypassing DB.
Auto-migration creates duplicate migrations for identical schema changes
Timestamp-based naming always creates new migration. Add check: 'git diff prisma/schema.prisma | grep "^+model"' detecting real changes. Or use 'npx prisma migrate diff' to compare first.
prisma format changes schema but hook shows no modifications
Formatting occurs after file write completing hook execution. Move format before validation: reorder script or use: 'npx prisma format && npx prisma validate' ensuring formatted state checked.
Hook runs in production despite NODE_ENV check skipping migrations
NODE_ENV not set in deployment defaulting to undefined. Export: 'export NODE_ENV=production' in shell profile. Or check: 'if [ "$NODE_ENV" = "production" ] || [ -z "$NODE_ENV" ]'.
Loading reviews...
Join our community of Claude power users. No spam, unsubscribe anytime.
Automated accessibility testing and compliance checking for web applications following WCAG guidelines
Automatically generates or updates API documentation when endpoint files are modified
Automatically formats code files after Claude writes or edits them using Prettier, Black, or other formatters