Skip to main content

Overview

Major refactoring of PostgreSQL storage implementation to improve code quality, security, maintainability, and add DID-based schema multi-tenancy support. Version: 2026.3.3
Date: 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 did parameter 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

Created helpers/ module structure under storage with extracted helper modules for better separation of concerns:
ModulePurpose
validation.pyUUID and type validation
serialization.pyJSONB serialization
normalization.pyMessage and UUID normalization
security.pyPassword masking, SQL injection prevention
db_operations.pyCommon database patterns
__init__.pyCentralized 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 Dependencies: None (uses existing SQLAlchemy and asyncpg) Database Changes:
  • 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:
  1. No action required - changes are backward compatible
  2. To enable DID-based multi-tenancy:
    • Pass did parameter when creating PostgresStorage instance
    • Schema will be automatically created and configured

Example

storage = PostgresStorage(did="did:example:123")
await storage.connect()

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