Skip to content

Support for customizing saga storage table names through environment variables

Choose a tag to compare

@vadikko2 vadikko2 released this 27 Jan 15:24
· 22 commits to master since this release
88a410d

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.sqlalchemy module is imported, using python-dotenv for .env file support.

Technical Details

Changes in cqrs.saga.storage.sqlalchemy

  • Added os and dotenv imports
  • Added dotenv.load_dotenv() call to load environment variables from .env files
  • Modified DEFAULT_SAGA_EXECUTION_TABLE_NAME and DEFAULT_SAGA_LOG_TABLE_NAME to be read from environment variables with fallback to default values
  • Updated SagaExecutionModel and SagaLogModel to use the configurable table names
  • Updated ForeignKey reference in SagaLogModel to use the configurable execution table name

Backward Compatibility

  • Fully backward compatible: If environment variables are not set, the default table names (saga_executions and saga_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

  1. Set environment variables in your .env file or deployment configuration:

    CQRS_SAGA_EXECUTION_TABLE_NAME=your_custom_saga_executions
    CQRS_SAGA_LOG_TABLE_NAME=your_custom_saga_logs
  2. 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
    );
  3. The SqlAlchemySagaStorage will 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_logs

Related 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