# Task: Implement Database Persistence ## Description Implement PostgreSQL database persistence for the IT Site Survey AI application. Currently, all survey data is stored in memory and lost when the application restarts. ## Requirements 1. Design a database schema for surveys and responses 2. Add PostgreSQL integration to the Flask application 3. Migrate existing in-memory storage to database storage 4. Ensure all existing API endpoints continue to work 5. Add database connection configuration 6. Update documentation with database setup instructions ## Technical Details - Application location: /home/jcbeasley/.openclaw/workspace/Projects/site-survey-ai/ - Main application file: app.py - Current in-memory storage: surveys_db and survey_responses_db dictionaries - Database: PostgreSQL (install if needed) - Connection should be configurable via environment variables ## Database Schema ### surveys - id (UUID, PK) - name (VARCHAR) - description (TEXT) - client_name (VARCHAR) - site_name (VARCHAR) - created_at (TIMESTAMP) - status (VARCHAR) ### survey_questions - id (UUID, PK) - survey_id (UUID, FK) - question_id (VARCHAR) - category (VARCHAR) - question_text (TEXT) - question_type (VARCHAR) - options (JSON) ### survey_responses - id (UUID, PK) - survey_id (UUID, FK) - submitted_by (VARCHAR) - submitted_at (TIMESTAMP) ### response_answers - id (UUID, PK) - response_id (UUID, FK) - question_id (VARCHAR) - answer_value (TEXT or JSON) ## Implementation Considerations ### Backward Compatibility - All existing API endpoints must continue working - No breaking changes to data structure - Maintain same JSON response formats ### Migration Strategy - Seamless transition from memory to database - No data loss during migration - Fallback to memory if database unavailable ### Configuration - Database connection via environment variables - Default to memory storage if no DB configured - Clear setup instructions for PostgreSQL ### Error Handling - Graceful degradation if database unavailable - Clear error messages for connectivity issues - Logging for debugging database issues ## Dependencies to Add - `psycopg2-binary` - PostgreSQL driver - `sqlalchemy` - ORM (optional but recommended) - Database connection pooling ## Acceptance Criteria - [ ] Survey data persists across application restarts - [ ] All existing API endpoints continue to function identically - [ ] Database schema documented - [ ] Setup instructions added to README - [ ] No data loss during migration - [ ] Error handling for database connectivity issues - [ ] Fallback to memory storage if database unavailable ## Priority High - Critical for production deployment ## Estimated Effort 16 hours