This directory contains scripts for managing data synchronization between production and development environments.
A comprehensive script that exports book data from production, transforms user IDs to match development users, and imports the data into your local database.
- Read-only production access: Production database is never modified
- User ID transformation: Automatically remaps production owner IDs to development user IDs
- Safety features: Confirmation prompts and dry-run mode
- Error handling: Graceful handling of duplicates and other errors
- Progress tracking: Real-time import progress display
- Interactive mode: Select target user from available development users
-
Environment files must be configured:
.env.production- Must containDEWEY_DB_DATABASE_URLfor production database.env- Must containDEWEY_DB_DATABASE_URLfor development database
-
Development database must have at least one user
-
Dependencies installed:
npm install
Run the script and follow the prompts:
npm run sync-prod-dataThis will:
- Export all books from production
- Show available development users
- Ask you to select which user should own the imported books
- Ask for confirmation before deleting existing books
- Import the data
Preview what will happen without making any changes:
npm run sync-prod-data -- --dry-runUse this to:
- Verify the production database connection
- See how many books will be imported
- Confirm the target user ID
- Test the script before running it for real
Skip the user selection prompt by providing a user ID:
npm run sync-prod-data -- --user-id=1Use the --yes flag to skip all confirmation prompts (useful for automation):
npm run sync-prod-data -- --yes --user-id=1You can combine multiple flags:
# Dry run with specific user
npm run sync-prod-data -- --dry-run --user-id=1
# Auto-confirm with specific user
npm run sync-prod-data -- --yes --user-id=1-
Load Environment Variables
- Reads production database URL from
.env.production - Reads development database URL from
.env
- Reads production database URL from
-
Export from Production
- Connects to production database (read-only)
- Exports all books with their metadata
- Displays sample of exported books
-
Identify Development User
- Lists available users in development database
- Prompts for user selection (or uses
--user-idif provided)
-
Transform Data
- Remaps all
ownerIdfields to the selected development user ID - Preserves all other book metadata
- Remaps all
-
Import to Development (skipped in
--dry-runmode)- Warns about existing books that will be deleted
- Requests confirmation (unless
--yesflag is used) - Deletes existing books in a transaction
- Imports transformed books one by one
- Handles duplicate ISBN errors gracefully
- Displays progress and final statistics
- Production is read-only: The script only reads from production, never writes
- Confirmation required: Asks "yes" before deleting development data
- Dry run mode: Preview changes without making them
- Transaction safety: Uses database transactions to ensure consistency
- Error handling: Gracefully handles duplicates and other errors
- Progress tracking: Shows real-time import progress
🚀 Production to Development Data Sync
============================================================
📦 Loading environment configurations...
✅ Production DB: prisma+postgres://accelerate.prisma-data.net/?api_...
✅ Development DB: postgresql://postgres:postgres@localhost:5433/penumbra_local
📥 Exporting books from production...
✅ Exported 357 books from production
📚 Sample of exported books:
1. "Axiom's End" by Lindsay Ellis
2. "Thirst" by Marina Yuszczuk
3. "Recursion" by Blake Crouch
... and 354 more
👤 Identifying development user...
✅ Using specified user: moonejon+test1@gmail.com (ID: 1)
🔄 Transforming data...
Remapping 1 production owner ID(s) to dev user ID 1
✅ Transformed 357 books
⚠️ WARNING: Development database currently has 42 books
These will be DELETED and replaced with production data
Type "yes" to continue: yes
💾 Importing data to development database...
🗑️ Deleted 42 existing books
📚 Imported 357/357 books...
✅ Successfully imported 357 books
🔍 Verifying import...
✅ Development database now has 357 books for user ID 1
============================================================
✨ SYNC COMPLETE!
The script handles several types of errors:
- Missing environment files: Clear error message if
.envor.env.productionis missing - Database connection errors: Helpful error messages for connection issues
- No users in development: Error if no users exist (you must create a user first)
- Duplicate ISBNs: Gracefully skips duplicates and counts them
- Other errors: Displays error details and continues with remaining books
- Ensure
.env.productionexists withDEWEY_DB_DATABASE_URL - Ensure
.envexists withDEWEY_DB_DATABASE_URL
- Create a user in your development database first
- You can do this by signing up through the application
- Check available user IDs by running without
--user-idflag - Or run
npx prisma studioto view users
- Verify your database connection strings are correct
- Check that your local PostgreSQL is running (for development)
- For production, verify the Prisma Accelerate API key is valid
- Press Ctrl+C to cancel
- Use
--dry-runto test without making changes - Check database connectivity
To modify the script:
- Edit
/Users/jonathan/github/penumbra/scripts/sync-prod-data.ts - Test your changes with
--dry-run:npm run sync-prod-data -- --dry-run --user-id=1
- Run for real when satisfied:
npm run sync-prod-data -- --user-id=1
- Always run with
--dry-runfirst to verify what will happen - Backup your development database if you have important test data
- Use
--user-idto make the script non-interactive - Check the output to ensure the correct number of books were imported
- Run periodically to keep development data fresh with production updates
npm run sync-prod-data -- --dry-runThe script automatically uses the correct working directory.
Create a shell script or add to your development setup scripts:
#!/bin/bash
# sync-dev-data.sh
npm run sync-prod-data -- --yes --user-id=1You can use this script in CI/CD to populate test databases:
npm run sync-prod-data -- --yes --user-id=${TEST_USER_ID}- Never commit
.envfiles - They contain sensitive credentials - Production credentials are read-only - The script only reads from production
- Use Prisma Accelerate for production connections (as configured)
- Local PostgreSQL for development is recommended
Potential improvements for the future:
- Export to JSON file for offline sync
- Import from JSON file
- Selective sync (filter by date, author, etc.)
- Incremental sync (only new/updated books)
- Backup before sync
- Rollback capability