Overview
Major refactoring of PostgreSQL storage implementation to improve code quality, security, maintainability, and add DID-based schema multi-tenancy support. Version: 2026.3.3Date: January 19, 2026
Author: Raahul Dutta
Breaking Changes
None - All public APIs remain backward compatible.New Features
✨ DID-Based Multi-Tenancy
- PostgresStorage now accepts optional
didparameter for schema isolation - Automatic schema creation and initialization per DID
- SQLAlchemy event listener sets search_path automatically
- Enables secure multi-tenant deployments with data isolation
🔒 Enhanced Security
- SQL injection prevention via
sanitize_identifier()validation - Improved password masking in database URLs for safe logging
- Centralized security utilities in
helpers/security.py
Improvements
📦 Code Organization
Createdhelpers/ module structure under storage with extracted helper modules for better separation of concerns:
| Module | Purpose |
|---|---|
validation.py | UUID and type validation |
serialization.py | JSONB serialization |
normalization.py | Message and UUID normalization |
security.py | Password masking, SQL injection prevention |
db_operations.py | Common database patterns |
__init__.py | Centralized exports |
🎯 Code Quality (DRY Principles)
- Reduced code duplication by 11% (1099 → 989 lines)
- UUID validation: 10+ instances → 1 helper function
- JSONB serialization: 15+ instances → 1 helper function
- Timestamp generation: 8+ instances → 1 helper function
- Message normalization: 60+ lines → 1 helper function
📖 Improved Readability
- Better error messages with context
- Clearer function signatures
- Reduced method complexity
- Improved documentation
Technical Details
Files Changed: 14 files- Added: 7 files (helpers + schema_manager + migration)
- Modified: 7 files
- Total: +726 insertions, -173 deletions
- New migration:
20260119_0001_add_schema_support.py - Schema manager utility for DID-based schema operations
Testing
All 23 unit tests passing
Backward compatibility verified
No breaking changes to existing functionality
Migration Guide
For existing deployments:- No action required - changes are backward compatible
- To enable DID-based multi-tenancy:
- Pass
didparameter when creating PostgresStorage instance - Schema will be automatically created and configured
- Pass
Example
Commit Details
Commit:022a47a3bc16f3da3bb3a011717e555826009674
Message: chore: refactor PostgreSQL storage to support DID-based schema multi-tenancy and improve code organization
Changes:
- Add DID parameter to PostgresStorage for schema-based multi-tenancy isolation
- Extract helper functions to separate modules
- Move timestamp utility to db_operations helper module
- Implement automatic schema initialization and search_path configuration
- Add SQLAlchemy event listener to set search_path on connection