Support for customizing saga storage table names through environment variables
Support for customizing saga storage table names through environment variables
Summary
This release adds support for customizing saga storage table names through environment variables, providing better flexibility for multi-tenant deployments and service-specific naming conventions.
Added
Saga Storage Configuration
-
Environment Variable Support for Saga Table Names: Added support for customizing saga execution and log table names via environment variables, similar to the existing outbox table configuration.
CQRS_SAGA_EXECUTION_TABLE_NAME: Environment variable to customize the saga execution table name (default:"saga_executions")CQRS_SAGA_LOG_TABLE_NAME: Environment variable to customize the saga log table name (default:"saga_logs")
This enhancement allows different services or deployments to use service-specific table names (e.g.,
service_manager_saga_executions,payment_service_saga_executions) while maintaining the same codebase.Usage:
# In .env file or environment variables export CQRS_SAGA_EXECUTION_TABLE_NAME=service_manager_saga_executions export CQRS_SAGA_LOG_TABLE_NAME=service_manager_saga_logs
The table names are automatically loaded from environment variables when the
cqrs.saga.storage.sqlalchemymodule is imported, usingpython-dotenvfor.envfile support.
Technical Details
Changes in cqrs.saga.storage.sqlalchemy
- Added
osanddotenvimports - Added
dotenv.load_dotenv()call to load environment variables from.envfiles - Modified
DEFAULT_SAGA_EXECUTION_TABLE_NAMEandDEFAULT_SAGA_LOG_TABLE_NAMEto be read from environment variables with fallback to default values - Updated
SagaExecutionModelandSagaLogModelto use the configurable table names - Updated
ForeignKeyreference inSagaLogModelto use the configurable execution table name
Backward Compatibility
- ✅ Fully backward compatible: If environment variables are not set, the default table names (
saga_executionsandsaga_logs) are used - ✅ No breaking changes: Existing code will continue to work without modifications
- ✅ Optional feature: The feature is opt-in via environment variables
Migration Guide
For Existing Users
No migration required. The feature is backward compatible and uses default table names if environment variables are not set.
For New Users or Custom Table Names
-
Set environment variables in your
.envfile or deployment configuration:CQRS_SAGA_EXECUTION_TABLE_NAME=your_custom_saga_executions CQRS_SAGA_LOG_TABLE_NAME=your_custom_saga_logs
-
Ensure your database migration creates tables with the custom names:
CREATE TABLE IF NOT EXISTS `your_custom_saga_executions` ( -- table definition ); CREATE TABLE IF NOT EXISTS `your_custom_saga_logs` ( -- table definition );
-
The
SqlAlchemySagaStoragewill automatically use the custom table names when initialized.
Examples
Example: Service-Specific Table Names
# .env file
CQRS_SAGA_EXECUTION_TABLE_NAME=service_manager_saga_executions
CQRS_SAGA_LOG_TABLE_NAME=service_manager_saga_logs
# Python code (no changes needed)
from cqrs.saga.storage.sqlalchemy import SqlAlchemySagaStorage
storage = SqlAlchemySagaStorage(session_factory)
# Automatically uses service_manager_saga_executions and service_manager_saga_logsRelated Issues
- Enables service-specific saga table naming for multi-service deployments
- Provides consistency with existing outbox table configuration pattern
- Improves flexibility for database schema customization