Project Migration Safety Guide
This guide helps you migrate existing projects that may have unsafe generator configurations to use the new safety system.
Common Migration Scenarios
Scenario 1: Generator Points to Source Directory
Before (Unsafe):
generator zod {
provider = "prisma-zod-generator"
output = "./src" // Dangerous - points to entire src directory
}
Migration Options:
Option A: Move to Dedicated Directory (Recommended)
generator zod {
provider = "prisma-zod-generator"
output = "./generated" // Safe dedicated directory
}
Steps:
- Update schema.prisma
- Run generator to create new location
- Update all imports in your code
- Delete old generated files from src/
- Test thoroughly
Option B: Use Subdirectory in Source
generator zod {
provider = "prisma-zod-generator"
output = "./src/generated" // Safer subdirectory
}
Option C: Allow Dangerous Path Temporarily
generator zod {
provider = "prisma-zod-generator"
output = "./src"
safetyAllowDangerousPaths = true # Temporary workaround
}
Option C is a temporary solution. Plan to migrate to Option A or B.
Scenario 2: Generator in Project Root
Before (Very Unsafe):
generator zod {
provider = "prisma-zod-generator"
output = "." // Extremely dangerous - project root!
}
This configuration is now blocked by default. You must migrate:
Migration (Required):
generator zod {
provider = "prisma-zod-generator"
output = "./prisma/generated" // Safe location
}
Scenario 3: Mixed Generated and Source Files
Before: Generated files mixed with your code in src/
Migration Strategy:
- Identify Generated Files: Look for files that match typical generated patterns
- Create Manifest: Use the manifest system to track future generations
- Separate Gradually: Move generated files to dedicated directories
# Look for common patterns
find src/ -name "*.schema.ts"
find src/ -name "*CreateInput.ts"
find src/ -name "*WhereInput.ts"
generator zod {
provider = "prisma-zod-generator"
output = "./src/generated"
# Allow the dangerous src path temporarily during migration
safetyLevel = "permissive"
}
Migration Strategies
Strategy 1: Big Bang Migration
Move everything at once:
- Backup Project: Commit all changes
- Update Configuration: Change output path
- Run Generator: Generate in new location
- Update Imports: Use find-and-replace for import paths
- Clean Up: Delete old files
- Test: Verify everything works
# Example: Update imports from src/ to generated/
find . -name "*.ts" -exec sed -i 's|from "\.\/.*\.schema"|from "../generated/&"|g' {} \;
Strategy 2: Gradual Migration
Migrate module by module:
- Dual Configuration: Run generator in both old and new locations temporarily
- Migrate Modules: Update imports module by module
- Clean Up Gradually: Remove old files as you migrate imports
- Final Switch: Once all imports updated, switch to new location only
Strategy 3: Safety-First Migration
Use safety system to guide migration:
- Enable Warnings: Use
warningsOnly: true
to see issues without blocking - Analyze Warnings: Understand what files would be affected
- Create Migration Plan: Based on warning analysis
- Execute Plan: Make changes guided by safety feedback
Safety Configuration for Migration
Phase 1: Assessment
{
"safety": {
"level": "strict",
"warningsOnly": true // See all issues without blocking
}
}
Phase 2: Active Migration
{
"safety": {
"level": "permissive",
"allowDangerousPaths": true,
"allowUserFiles": true
}
}
Phase 3: Post-Migration
{
"safety": {
"level": "standard" // Return to normal safety
}
}
Handling Specific Error Messages
Error: "Output directory contains project file"
❌ Output directory contains project file "package.json"
Solutions:
- Change Output: Use a subdirectory instead
- Override Temporarily: Set
allowProjectRoots: true
- Environment Override:
PRISMA_ZOD_SAFETY_ALLOW_PROJECT_ROOTS=true
Error: "Too many potentially user-generated files"
❌ Too many potentially user-generated files (15) found. Maximum allowed: 5.
Solutions:
- Increase Limit: Set
maxUserFiles: 20
- Allow User Files: Set
allowUserFiles: true
- Clean Directory: Remove non-generated files first
- Use Manifest: Let the system learn what's generated
Warning: "Common source code directory name"
⚠️ Output directory "src" is a common source code directory name
Solutions:
- Use Subdirectory: Change to
./src/generated
- Allow Dangerous: Set
allowDangerousPaths: true
- Accept Warning: Warnings don't block generation
Import Update Strategies
Automated Import Updates
const fs = require('fs');
const path = require('path');
function updateImports(directory, oldPath, newPath) {
const files = fs.readdirSync(directory);
files.forEach(file => {
if (file.endsWith('.ts') || file.endsWith('.tsx')) {
const filePath = path.join(directory, file);
let content = fs.readFileSync(filePath, 'utf8');
// Update relative imports
content = content.replace(
new RegExp(`from ['"]${oldPath}`, 'g'),
`from "${newPath}`
);
fs.writeFileSync(filePath, content);
}
});
}
// Usage
updateImports('./src', './schemas/', '../generated/schemas/');
VSCode Find and Replace
- Open Find and Replace (Ctrl/Cmd + Shift + H)
- Enable regex mode
- Find:
from ['"]\.\/schemas\/
- Replace:
from "../generated/schemas/
TypeScript-Aware Refactoring
If using VSCode or WebStorm:
- Rename the generated directory
- Let the IDE update imports automatically
- Run TypeScript compiler to catch any missed imports
Validation After Migration
Check 1: No TypeScript Errors
npx tsc --noEmit
Check 2: All Imports Resolved
npm run build
Check 3: Tests Pass
npm test
Check 4: No Old Generated Files
# Look for old generated files in dangerous locations
find src/ -name "*.schema.ts" -not -path "*/generated/*"
Rollback Plan
Always have a rollback plan:
- Git Branch: Create a migration branch
- Backup Configuration: Save old generator config
- Document Changes: Keep notes of what imports were changed
- Test Rollback: Verify you can revert changes
git checkout main
git reset --hard HEAD~1 # If committed
# OR restore specific files
git checkout HEAD~1 -- schema.prisma src/
Team Coordination
For team projects:
- Announce Migration: Warn team about upcoming changes
- Create PR: Use pull requests for review
- Document Process: Share migration steps with team
- Coordinate Timing: Choose low-activity periods
- Support Team: Be available for migration questions
Migration can be complex, but the safety system is designed to help guide you through the process. Start with permissive settings and gradually tighten them as you clean up your project structure.