Database Tasks
A complete workflow for database management including migrations, seeding, backups, and performance analysis.
Complete Jakefile
Section titled “Complete Jakefile”# Database & Backend Jakefile# ===========================
@dotenv@require DATABASE_URL
# === Migrations ===
@group dbtask migrate: @description "Run pending migrations" @needs npx @pre echo "Running migrations..." npx prisma migrate deploy @post echo "Migrations complete"
@group dbtask migrate-create name: @description "Create a new migration" @needs npx npx prisma migrate dev --name {{name}} echo "Created migration: {{name}}"
@group dbtask migrate-reset: @description "Reset database and run all migrations" @confirm "This will DELETE all data. Continue?" @needs npx npx prisma migrate reset --force @post echo "Database reset complete"
@group dbtask migrate-status: @description "Show migration status" @needs npx npx prisma migrate status
# === Seeding ===
@group dbtask seed: @description "Seed database with sample data" @needs npx @pre echo "Seeding database..." npx prisma db seed @post echo "Database seeded"
@group dbtask seed-prod: @description "Seed production essentials only" @confirm "Seed production database?" @needs npx NODE_ENV=production npx prisma db seed -- --production echo "Production seed complete"
# === Backups ===
@group backuptask backup: @description "Create database backup" @needs pg_dump backup_file="backups/db-$(date +%Y%m%d-%H%M%S).sql" mkdir -p backups pg_dump $DATABASE_URL > $backup_file gzip $backup_file @post echo "Backup created: ${backup_file}.gz"
@group backuptask backup-list: @description "List available backups" @quiet ls -lah backups/*.sql.gz 2>/dev/null || echo "No backups found"
@group backuptask restore file: @description "Restore from backup file" @confirm "Restore from {{file}}? This will overwrite current data." @needs psql gunzip gunzip -c {{file}} | psql $DATABASE_URL @post echo "Database restored from {{file}}"
@group backuptask backup-s3: @description "Backup to S3" @require AWS_BUCKET @needs aws pg_dump backup_file="db-$(date +%Y%m%d-%H%M%S).sql.gz" pg_dump $DATABASE_URL | gzip | aws s3 cp - s3://$AWS_BUCKET/backups/$backup_file @post echo "Backup uploaded to s3://$AWS_BUCKET/backups/$backup_file"
# === Schema ===
@group schematask schema-push: @description "Push schema changes (dev only)" @needs npx npx prisma db push echo "Schema pushed"
@group schematask schema-pull: @description "Pull schema from database" @needs npx npx prisma db pull echo "Schema pulled"
@group schematask schema-generate: @description "Generate Prisma client" @needs npx npx prisma generate echo "Client generated"
@group schematask schema-studio: @description "Open Prisma Studio" @needs npx npx prisma studio
# === Performance ===
@group perftask analyze-queries: @description "Analyze slow queries" @needs psql psql $DATABASE_URL -c "SELECT query, calls, mean_time, total_time FROM pg_stat_statements ORDER BY mean_time DESC LIMIT 10;"
@group perftask vacuum: @description "Run VACUUM ANALYZE" @needs psql @pre echo "Running VACUUM ANALYZE..." psql $DATABASE_URL -c "VACUUM ANALYZE;" @post echo "Vacuum complete"
@group perftask table-sizes: @description "Show table sizes" @needs psql psql $DATABASE_URL -c "SELECT relname AS table, pg_size_pretty(pg_total_relation_size(relid)) AS size FROM pg_catalog.pg_statio_user_tables ORDER BY pg_total_relation_size(relid) DESC LIMIT 10;"
# === Setup ===
@defaulttask setup: [schema-generate, migrate, seed] @description "Full database setup" echo "Database setup complete!"
task reset: [migrate-reset, seed] @description "Reset and reseed database" echo "Database reset complete!"
# === Utilities ===
task shell: @description "Open database shell" @needs psql psql $DATABASE_URL
task exec query: @description "Execute SQL query" @needs psql psql $DATABASE_URL -c "{{query}}"jake setup # Full database setupjake migrate # Run migrationsjake migrate-create add-users # Create new migrationjake seed # Seed datajake backup # Create backupjake restore file=backups/db.sql.gz # Restore backupjake shell # Open psqlKey Features
Section titled “Key Features”Migration Workflow
Section titled “Migration Workflow”Complete migration lifecycle:
jake migrate-status # Check current statejake migrate-create add-users # Create migrationjake migrate # Apply migrationsjake migrate-reset # Start fresh (destructive)Automated Backups
Section titled “Automated Backups”Local and cloud backups:
task backup: backup_file="backups/db-$(date +%Y%m%d-%H%M%S).sql" pg_dump $DATABASE_URL > $backup_file gzip $backup_file
task backup-s3: pg_dump $DATABASE_URL | gzip | aws s3 cp - s3://$AWS_BUCKET/backups/$backup_fileSafe Destructive Operations
Section titled “Safe Destructive Operations”Confirmations prevent accidents:
task migrate-reset: @confirm "This will DELETE all data. Continue?" npx prisma migrate reset --forcePerformance Analysis
Section titled “Performance Analysis”Built-in performance tools:
jake analyze-queries # Show slow queriesjake vacuum # Run VACUUM ANALYZEjake table-sizes # Show table sizesCustomization
Section titled “Customization”For MySQL
Section titled “For MySQL”Adjust commands for MySQL:
task backup: @needs mysqldump backup_file="backups/db-$(date +%Y%m%d-%H%M%S).sql" mysqldump -u $DB_USER -p$DB_PASSWORD $DB_NAME > $backup_file gzip $backup_file
task shell: @needs mysql mysql -u $DB_USER -p$DB_PASSWORD $DB_NAMEFor Different ORMs
Section titled “For Different ORMs”Replace Prisma commands with your ORM:
# Knextask migrate: npx knex migrate:latest
# Drizzletask migrate: npx drizzle-kit push:pg
# TypeORMtask migrate: npx typeorm migration:runSee Also
Section titled “See Also”- Environment Validation - Secure credential handling
- Docker Workflows - Database containers