This Laravel application uses the RefreshDatabase trait in tests, which COMPLETELY DROPS AND RECREATES ALL DATABASE TABLES. Running tests incorrectly WILL DELETE ALL YOUR DATA.
| Database | Purpose | Data Status |
|---|---|---|
learning_app |
Development work | MUST BE PROTECTED |
learning_app_test |
Testing only | Wiped on each test run |
./scripts/safe-test.sh # Run all tests
./scripts/safe-test.sh --filter TestName # Run specific test
./scripts/safe-test.sh --coverage # Run with coveragemake test-unit # Runs safe-test.sh internally
make test-unit-coverage # Safe coverage testing# ALL OF THESE CAN WIPE YOUR DEVELOPMENT DATABASE:
php artisan test # NO DATABASE PROTECTION
artisan test # NO DATABASE PROTECTION
vendor/bin/phpunit # NO DATABASE PROTECTION
phpunit # NO DATABASE PROTECTION
# Even with APP_ENV - still risky if misconfigured:
APP_ENV=testing php artisan test # RISKY - might not load .env.testing- Validates database name isn't
learning_app - Forces
DB_DATABASE=learning_app_test - Sets all required environment variables
- Creates test database if it doesn't exist
- Displays configuration before running
// Throws exception if wrong database detected
if ($database !== 'learning_app_test') {
throw new \Exception("CRITICAL ERROR: Tests attempting to run on non-test database!");
}<!-- Forces test database with force="true" attribute -->
<env name="DB_DATABASE" value="learning_app_test" force="true"/>All make test* commands use safe-test.sh automatically.
The structure can be restored, but data may be lost unless you have backups.
# Recreate all tables
php artisan migrate
# If you have seeders, run them
php artisan db:seed
# Or use fresh command
php artisan migrate:fresh --seed- Check if you have database backups
- Check if you have SQL dumps
- Manually recreate critical data
# Create a database backup before any risky operations
pg_dump -U laravel -h localhost learning_app > backup_$(date +%Y%m%d_%H%M%S).sql
# Set up automated backups
# Add to crontab: 0 */6 * * * pg_dump -U laravel learning_app > ~/backups/learning_app_$(date +\%Y\%m\%d_\%H\%M\%S).sql- Always use
./scripts/safe-test.shfor running tests - Never modify phpunit.xml database settings
- Create regular database backups
- Use separate PostgreSQL users for dev and test databases (advanced)
- Review test output - it shows which database is being used
# GitHub Actions example
- name: Run tests safely
env:
DB_DATABASE: learning_app_test # Explicitly set test database
APP_ENV: testing
run: |
./scripts/safe-test.sh✅ SAFE: ./scripts/safe-test.sh
✅ SAFE: make test-unit
❌ DANGER: php artisan test
❌ DANGER: vendor/bin/phpunit
If you accidentally wipe your database:
- Stop all running processes
- Check
storage/logs/laravel.logfor any errors - Run recovery steps above
- Consider implementing database replication for instant recovery
Remember: When in doubt, use ./scripts/safe-test.sh. It's better to be safe than sorry!