diff --git a/README.md b/README.md index 5f0e332..9ea63ac 100644 --- a/README.md +++ b/README.md @@ -161,6 +161,46 @@ DATABASE_URL="postgresql://username:password@localhost:5432/euchre_camp" DATABASE_SHADOW_URL="postgresql://username:password@localhost:5432/euchre_camp_shadow" ``` +## Development Database + +For development and testing, use a separate development database to avoid conflicts with production data. + +**Setup Development Database:** + +```bash +# Create and setup the development database +npm run db:setup-dev + +# Reset the development database (drops and recreates) +npm run db:reset-dev + +# Clean development database (drop and recreate with migrations) +npm run db:setup-dev:clean +``` + +**Environment Configuration:** + +The development database uses `.env.development` which is automatically configured for: +- Database: `euchre_camp_dev` +- NODE_ENV: `development` + +**Testing with Development Database:** + +```bash +# Run unit tests (uses test database automatically) +npm run test + +# Run acceptance tests (uses development database) +npm run test:acceptance +``` + +**Test Data Cleanup:** + +All tests automatically clean up their created records: +- Global setup/teardown handles Playwright tests +- Test utilities provide `cleanupTestRecords()` for manual cleanup +- Tests use `beforeEach` and `afterEach` hooks for automatic cleanup + ## Usage ### Creating a Tournament diff --git a/package.json b/package.json index efb1376..0d6d3fb 100644 --- a/package.json +++ b/package.json @@ -15,6 +15,7 @@ "db:setup-postgres": "node scripts/setup-postgres.js", "db:setup-dev": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js", "db:setup-dev:clean": "DATABASE_URL=\"postgresql://euchre_camp:LINGO5row_hiding@dhg.lol:5432/euchre_camp_dev\" node scripts/setup-postgres.js --drop", + "db:reset-dev": "node scripts/reset-dev-db.js", "db:use-dev": "node scripts/use-dev-db.js", "db:seed": "node scripts/seed.js", "docker:up": "docker-compose up -d", diff --git a/scripts/reset-dev-db.js b/scripts/reset-dev-db.js new file mode 100755 index 0000000..c3a1540 --- /dev/null +++ b/scripts/reset-dev-db.js @@ -0,0 +1,65 @@ +#!/usr/bin/env node + +/** + * Reset Development Database + * + * Drops and recreates the development database with a clean state. + * Useful for development and testing. + */ + +const { execSync } = require('child_process'); +const fs = require('fs'); +const path = require('path'); + +// Load environment variables +const envPath = path.resolve(__dirname, '..', '.env.development'); +if (fs.existsSync(envPath)) { + require('dotenv').config({ path: envPath }); +} + +// Check if DATABASE_URL is set +if (!process.env.DATABASE_URL) { + console.error('āŒ DATABASE_URL is not set'); + console.error('Please set it in .env.development'); + process.exit(1); +} + +// Extract database name from URL +const dbUrl = process.env.DATABASE_URL; +const dbName = dbUrl.split('/').pop(); + +console.log('šŸ”„ Resetting development database...\n'); +console.log(`Database: ${dbName}`); +console.log(`URL: ${dbUrl}\n`); + +try { + // Drop existing connections + console.log('Dropping existing connections...'); + execSync( + `psql "${dbUrl}" -c "SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE datname = '${dbName}' AND pid <> pg_backend_pid();" 2>/dev/null || true`, + { stdio: 'pipe' } + ); + + // Drop the database + console.log('Dropping database...'); + execSync( + `psql "${dbUrl}" -c "DROP DATABASE IF EXISTS ${dbName};" 2>/dev/null || true`, + { stdio: 'pipe' } + ); + + // Recreate the database + console.log('Creating database...'); + execSync(`psql "${dbUrl}" -c "CREATE DATABASE ${dbName};"`, { stdio: 'pipe' }); + + // Run migrations + console.log('Running migrations...'); + execSync('npx prisma migrate deploy', { stdio: 'inherit' }); + + console.log('\nāœ… Development database reset successfully!'); + console.log('\nNext steps:'); + console.log('1. Seed the database (optional): npm run db:seed'); + console.log('2. Start development server: npm run dev'); +} catch (error) { + console.error('\nāŒ Failed to reset database:', error.message); + process.exit(1); +}