Overview
VoicePact uses SQLAlchemy as its ORM with support for both SQLite (development) and PostgreSQL (production). The database stores contracts, parties, signatures, payments, audit logs, and session data.Database Architecture
Core Tables
VoicePact’s database schema includes:- contracts - Voice-powered contract records
- contract_parties - Parties involved in contracts (buyer, seller, mediator, witness)
- contract_signatures - Digital signatures and confirmation status
- payments - Escrow payment transactions
- audit_logs - Immutable event trail for compliance
- voice_recordings - Audio file metadata and processing status
- ussd_sessions - USSD interaction state management
- sms_logs - SMS delivery tracking
Entity Relationships
SQLite Configuration (Development)
SQLite is the default database for development and small deployments.Advantages
- Zero configuration
- No separate server required
- Perfect for development and testing
- Single file database
Limitations
- Limited concurrency
- Not recommended for production with >10 concurrent users
- No built-in replication
Setup SQLite
- Configure environment variable:
- Initialize database:
- Verify database creation:
SQLite Optimizations
VoicePact automatically applies performance optimizations:app/core/database.py:46-52.
PostgreSQL Configuration (Production)
For production deployments, PostgreSQL is strongly recommended.Prerequisites
- PostgreSQL 13+ installed
- Database user with CREATE privileges
- Network connectivity from application server
Installation
- Ubuntu/Debian
- macOS (Homebrew)
- Docker
Database Setup
- Create database and user:
- Install Python PostgreSQL driver:
- Configure environment:
- Initialize database:
PostgreSQL Tuning
Editpostgresql.conf for production optimization:
Database Migrations
VoicePact uses SQLAlchemy’s declarative base for schema management. The database schema is automatically created on application startup.Manual Migration Process
For production environments where you want control over schema changes:- Install Alembic:
- Initialize Alembic:
- Configure Alembic:
alembic/env.py:
- Generate initial migration:
- Apply migration:
Schema Verification
Verify all tables are created: SQLite:Redis Configuration
Redis is required for caching and session management.Installation
- Ubuntu/Debian
- macOS (Homebrew)
- Docker
Redis Configuration
Editredis.conf for production:
Environment Configuration
Verify Redis Connection
Data Models Reference
Contract Model
Key fields fromapp/models/contract.py:65-140:
Payment Model
Key fields fromapp/models/contract.py:219-268:
Audit Log Model
Immutable event trail fromapp/models/contract.py:271-309:
Database Operations
Connection Management
TheDatabaseManager class (in app/core/database.py:25-143) handles:
- Async database engine initialization
- Connection pooling
- Redis connection management
- Automatic connection testing
- Graceful shutdown
Session Management
Use dependency injection for database sessions:Cache Management
Fromapp/core/database.py:166-240:
Backup and Recovery
SQLite Backup
PostgreSQL Backup
Automated Backup Script
Performance Monitoring
Database Health Check
VoicePact includes built-in health checks:PostgreSQL Query Monitoring
Identify slow queries:Redis Monitoring
Troubleshooting
Connection Errors
“Unable to open database file”- Check file permissions:
chmod 644 voicepact.db - Verify directory is writable:
chmod 755 . - Check disk space:
df -h
- Verify PostgreSQL is running:
sudo systemctl status postgresql - Check
pg_hba.conffor authentication rules - Verify connection string in
.env - Check firewall:
sudo ufw status
- Verify Redis is running:
sudo systemctl status redis - Check bind address in
redis.conf - Verify password if configured
Migration Issues
“Table already exists”Performance Issues
High CPU usage on SQLite:- Consider upgrading to PostgreSQL
- Check for missing indexes
- Review query patterns
- Enable
DATABASE_ECHO=trueto log queries
- Run
VACUUM ANALYZEregularly - Check indexes:
\diin psql - Review connection pool settings
- Adjust
work_memandshared_buffers
Next Steps
- Environment Setup - Configure environment variables
- Production Deployment - Deploy to production
- API Reference - Explore database-backed endpoints