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
safetyAllowUserFiles = true // What actually unblocks an existing src/
}
safetyAllowDangerousPaths alone will not get you there: the directory-name check only ever warns. A populated ./src is blocked by the count of files that look like user code, so you need safetyAllowUserFiles = true (or safetyMaxUserFiles raised above that count), plus safetyAllowProjectRoots = true if ./src happens to contain a tsconfig.json, README.md or .gitignore.
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"
// Report rather than block while hand-written files are still mixed in
safetyLevel = "permissive"
}
./src/generated needs no dangerous-path allowance — only the final path segment is checked, and generated is not on the list. permissive is here to stop the user-file count from blocking while you are still moving hand-written files out of the new output directory; drop it once the directory holds nothing but generated schemas.
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
Commit before running this.
# 1. Dry run: see which files would change (never recurse into node_modules)
grep -rln --include='*.ts' --exclude-dir=node_modules 'from "\./[A-Za-z0-9_]*\.schema"' src/
# 2. Rewrite, capturing the module name instead of re-inserting the whole match
find src -name '*.ts' -not -path '*/node_modules/*' \
-exec sed -i.bak -E 's|from "\./([A-Za-z0-9_]+\.schema)"|from "../generated/\1"|g' {} +
# 3. Review `git diff`, then delete the .bak files
find src -name '*.ts.bak' -delete
In a sed replacement, & inserts the entire match, so a naive from "../generated/&" produces nested, invalid import statements. Scope the find to src as well — a bare find . walks into node_modules and sed -i rewrites in place with no backup.
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: trueto 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 messages are debug-level, so warningsOnly: true on its own produces a completely silent run and it is easy to conclude there are no issues. Run DEBUG_PRISMA_ZOD=1 npx prisma generate (or DEBUG=prisma-zod npx prisma generate) for every step of this strategy.
Safety Configuration for Migration
Phase 1: Assessment
strict reports the largest set of issues (maxUserFiles: 0, so a single file that looks like user code is flagged) while warningsOnly keeps generation from aborting. Run it with DEBUG_PRISMA_ZOD=1 — otherwise nothing is printed. If the directory already holds a .prisma-zod-generator-manifest.json from an earlier run, the user-file check is skipped entirely; delete the manifest first to get a full picture.
{
"safety": {
"level": "strict",
"warningsOnly": true
}
}
Phase 2: Active Migration
permissive already implies allowDangerousPaths: true, allowUserFiles: true and warningsOnly: true; the two explicit lines below are there to document the intent, not to change behaviour.
{
"safety": {
"level": "permissive",
"allowDangerousPaths": true,
"allowUserFiles": true
}
}
Phase 3: Post-Migration
Back to normal safety.
{
"safety": {
"level": "standard"
}
}
Handling Specific Error Messages
The messages below are debug-log lines, not console output. Run DEBUG_PRISMA_ZOD=1 npx prisma generate (or DEBUG=prisma-zod) to see them. A blocking error is additionally printed to stderr — but it does not change the exit code, so prisma generate reports success while writing no schemas.
Error: "Output directory contains project file"
Output directory contains project file "package.json". This suggests it's a project root
directory that should not be cleaned automatically.
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. For safety,
automatic cleanup is disabled. Please use a dedicated directory for generated schemas.
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. Consider using a dedicated
subdirectory like "src/generated" instead.
Solutions:
- Use Subdirectory: Change to
./src/generated— only the last path segment is checked, so this silences the message outright - Allow Dangerous: Set
allowDangerousPaths: true, which appends "(Allowed by configuration)" to the message - Accept Warning: This check never blocks generation in either state, so ignoring it is safe
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.