π‘οΈ Critical: Implement database protection system for testing#1
Merged
Conversation
Critical fix to prevent development database from being wiped during testing. ## Problem - Tests with RefreshDatabase trait were wiping the development database - Running `php artisan test` without proper environment setup destroyed all data - This happened multiple times causing significant data loss ## Solution - Multi-layer Protection System ### 1. Safe Test Runner Script (scripts/safe-test.sh) - Forces DB_DATABASE=learning_app_test explicitly - Validates database name isn't 'learning_app' - Creates test database if it doesn't exist - Shows configuration before running tests - Exit immediately if wrong database detected ### 2. TestCase.php Runtime Protection - Added database validation in setUp() method - Throws exception if tests attempt to run on non-test database - Provides clear error message with recovery instructions ### 3. phpunit.xml Configuration Hardening - Added force="true" to DB_DATABASE environment variable - Ensures test database is always used regardless of .env - Added warning comments about RefreshDatabase dangers ### 4. Makefile Integration - Updated test-unit and test-unit-coverage commands - Both now use safe-test.sh instead of direct artisan test - Added clear warnings about database safety ### 5. Documentation Updates - Updated CLAUDE.md with safe testing instructions - Listed dangerous commands to avoid - Provided safe alternatives and examples - Created TESTING_SAFETY.md with comprehensive guide - Includes recovery procedures if database gets wiped ## Testing - Verified safe-test.sh prevents connection to development database - Confirmed TestCase.php throws exception on wrong database - Tested Makefile commands work with new safety wrapper ## Impact - Developers can now run tests without fear of data loss - Clear documentation prevents future incidents - Multiple failsafes ensure database protection - Recovery procedures documented if issues occur π€ Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
The CI pipeline uses 'homeschoolai_test' and 'homeschoolai_e2e' as database names, but our protection was only allowing 'learning_app_test'. This commit updates the TestCase.php to allow all valid test database names while still protecting the development database from being wiped.
- Set max failures to 3 in CI (stop after 3 test failures) - Disable retries in CI for faster feedback - Add 10-minute timeout for e2e job - Reduce individual test timeouts in CI (30s test, 5s expect, 5s action) - Use BASE_URL environment variable properly - Add simpler 'line' reporter for CI output
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Problem Statement
The Laravel test suite uses the
RefreshDatabasetrait which drops and recreates all database tables. When tests were run without proper environment configuration, they connected to the development database (learning_app) instead of the test database (learning_app_test), resulting in complete data loss.This has happened multiple times and caused significant disruption to development work.
Solution Overview
Implemented a multi-layer protection system with the following safeguards:
1. β Safe Test Runner Script (
scripts/safe-test.sh)DB_DATABASE=learning_app_testexplicitly2. β TestCase.php Runtime Protection
setUp()method3. β phpunit.xml Configuration Hardening
force="true"to DB_DATABASE environment variable4. β Makefile Integration
test-unitandtest-unit-coveragecommands5. β Comprehensive Documentation
Testing Done
Impact
Files Changed
scripts/safe-test.sh- New safety wrapper scripttests/TestCase.php- Added runtime database validationphpunit.xml- Hardened configuration with force attributesMakefile- Updated test commands to use safe wrapperCLAUDE.md- Added safe testing instructionsTESTING_SAFETY.md- New comprehensive safety documentationChecklist
Related Issues
π€ Generated with Claude Code