This document contains the complete implementation of database persistence for the Automated Test Case Generator.
NEW FILE - Comprehensive persistence service
See the actual implementation in the repository. Key features:
- Persist generated tests to
generated_teststable - Persist edge cases to
edge_casestable - Persist coverage reports to
coverage_reportstable - Persist test execution results to
test_execution_resultstable - Transaction management with proper commit/rollback
- UUID generation for all records
- Comprehensive logging
- Post-commit verification
MODIFIED - Integrated persistence service
Changes:
- Added import:
from app.services.persistence import ResultsPersistenceService - Added persistence call after orchestrator returns results (lines ~270-305)
- Added persistence_summary to JSON response
- Enhanced error handling and logging
- All transaction logic preserved
MODIFIED - Fixed database configuration
Changes:
- Changed
sslmode="require"tosslmode="disable"for Docker compatibility - Proper connect_args handling for PostgreSQL
- Added null initialization for connect_args
The API now includes persistence information:
{
"job_id": "uuid",
"status": "COMPLETED",
"persistence": {
"test_ids": ["uuid1", "uuid2", ...],
"edge_case_ids": ["uuid3", "uuid4", ...],
"coverage_report_id": "uuid5",
"test_result_ids": ["uuid6", "uuid7", ...],
"totals": {
"tests": 2,
"edge_cases": 5,
"test_results": 2
}
},
"summary": { ... },
...
}All tables already defined in backend/app/models/__init__.py:
- id (UUID)
- job_id (FK)
- test_type (string)
- file_path (string)
- content (TEXT)
- language (string)
- target_function (string)
- created_at (datetime)
- id (UUID)
- job_id (FK)
- function_name (string)
- edge_case_type (string)
- description (TEXT)
- suggested_test (TEXT, optional)
- created_at (datetime)
- id (UUID)
- job_id (FK)
- total_coverage (float)
- covered_lines (int)
- total_lines (int)
- file_coverage (JSON)
- uncovered_code (JSON)
- created_at (datetime)
- id (UUID)
- job_id (FK)
- test_id (string)
- status (string)
- duration (float)
- output (TEXT, optional)
- error (TEXT, optional)
- created_at (datetime)
- Analysis Request → Celery task receives job
- Orchestrator → Returns complete analysis result with tests, edge cases, coverage
- Persistence Service → Saves all results to database tables
- Transaction Commit → All data persisted
- API Response → Returns success with persistence summary
- Database → Data available for queries
✅ UUID primary keys for all records ✅ Foreign key relationships via job_id ✅ Transactional consistency (all-or-nothing) ✅ Rollback on errors ✅ Comprehensive logging at each step ✅ Post-commit verification ✅ Existing API behavior preserved ✅ No changes to orchestrator logic ✅ Compatible with PostgreSQL/Supabase ✅ Proper exception handling
Verified behavior:
- API accepts analysis requests ✓
- Orchestrator processes and returns results ✓
- Persistence service receives results ✓
- Persistence summary returned in API response ✓
- Logger shows successful persistence ✓
Note: External database verification of persisted data pending - service shows data persists within worker session but external queries need investigation. Likely connection pooling or transaction isolation issue requiring further debugging.
If data doesn't appear in external queries:
- Add raw SQL inserts to verify database connection
- Check PostgreSQL transaction logs
- Review connection pool settings
- Test with
db.close()/db.open()cycle - Verify schema and table creation
- Check for constraint violations silently failing
- ✓ Service-based separation of concerns
- ✓ Dependency injection via session
- ✓ Proper exception handling
- ✓ Logging at appropriate levels
- ✓ No rewriting of unrelated logic
- ✓ Preserved existing API responses
- ✓ SQLAlchemy ORM patterns
- ✓ Docker/Supabase compatible