- Set correct table ID: mx149yctebfwvys (ai_data_Memory) - Updated type column with SingleSelect options - Added all required columns for corrections, preferences, episodes, decisions, validation - Fixed column options for type, severity, status fields
2.6 KiB
2.6 KiB
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
- Design a database schema for surveys and responses
- Add PostgreSQL integration to the Flask application
- Migrate existing in-memory storage to database storage
- Ensure all existing API endpoints continue to work
- Add database connection configuration
- 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 driversqlalchemy- 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