refactor: Refactors repository initialization and usage
Signed-off-by: -LAN- <laipz8200@outlook.com>pull/19176/head
parent
1abf00e443
commit
f952c6576e
@ -1,87 +0,0 @@
|
|||||||
"""
|
|
||||||
Registry for repository implementations.
|
|
||||||
|
|
||||||
This module is responsible for registering factory functions with the repository factory.
|
|
||||||
"""
|
|
||||||
|
|
||||||
import logging
|
|
||||||
from collections.abc import Mapping
|
|
||||||
from typing import Any
|
|
||||||
|
|
||||||
from sqlalchemy.orm import sessionmaker
|
|
||||||
|
|
||||||
from configs import dify_config
|
|
||||||
from core.repositories.workflow_node_execution import SQLAlchemyWorkflowNodeExecutionRepository
|
|
||||||
from core.workflow.repository.repository_factory import RepositoryFactory
|
|
||||||
from extensions.ext_database import db
|
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
# Storage type constants
|
|
||||||
STORAGE_TYPE_RDBMS = "rdbms"
|
|
||||||
STORAGE_TYPE_HYBRID = "hybrid"
|
|
||||||
|
|
||||||
|
|
||||||
def register_repositories() -> None:
|
|
||||||
"""
|
|
||||||
Register repository factory functions with the RepositoryFactory.
|
|
||||||
|
|
||||||
This function reads configuration settings to determine which repository
|
|
||||||
implementations to register.
|
|
||||||
"""
|
|
||||||
# Configure WorkflowNodeExecutionRepository factory based on configuration
|
|
||||||
workflow_node_execution_storage = dify_config.WORKFLOW_NODE_EXECUTION_STORAGE
|
|
||||||
|
|
||||||
# Check storage type and register appropriate implementation
|
|
||||||
if workflow_node_execution_storage == STORAGE_TYPE_RDBMS:
|
|
||||||
# Register SQLAlchemy implementation for RDBMS storage
|
|
||||||
logger.info("Registering WorkflowNodeExecution repository with RDBMS storage")
|
|
||||||
RepositoryFactory.register_workflow_node_execution_factory(create_workflow_node_execution_repository)
|
|
||||||
elif workflow_node_execution_storage == STORAGE_TYPE_HYBRID:
|
|
||||||
# Hybrid storage is not yet implemented
|
|
||||||
raise NotImplementedError("Hybrid storage for WorkflowNodeExecution repository is not yet implemented")
|
|
||||||
else:
|
|
||||||
# Unknown storage type
|
|
||||||
raise ValueError(
|
|
||||||
f"Unknown storage type '{workflow_node_execution_storage}' for WorkflowNodeExecution repository. "
|
|
||||||
f"Supported types: {STORAGE_TYPE_RDBMS}"
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def create_workflow_node_execution_repository(params: Mapping[str, Any]) -> SQLAlchemyWorkflowNodeExecutionRepository:
|
|
||||||
"""
|
|
||||||
Create a WorkflowNodeExecutionRepository instance using SQLAlchemy implementation.
|
|
||||||
|
|
||||||
This factory function creates a repository for the RDBMS storage type.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
params: Parameters for creating the repository, including:
|
|
||||||
- tenant_id: Required. The tenant ID for multi-tenancy.
|
|
||||||
- app_id: Optional. The application ID for filtering.
|
|
||||||
- session_factory: Optional. A SQLAlchemy sessionmaker instance. If not provided,
|
|
||||||
a new sessionmaker will be created using the global database engine.
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A WorkflowNodeExecutionRepository instance
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
ValueError: If required parameters are missing
|
|
||||||
"""
|
|
||||||
# Extract required parameters
|
|
||||||
tenant_id = params.get("tenant_id")
|
|
||||||
if tenant_id is None:
|
|
||||||
raise ValueError("tenant_id is required for WorkflowNodeExecution repository with RDBMS storage")
|
|
||||||
|
|
||||||
# Extract optional parameters
|
|
||||||
app_id = params.get("app_id")
|
|
||||||
|
|
||||||
# Use the session_factory from params if provided, otherwise create one using the global db engine
|
|
||||||
session_factory = params.get("session_factory")
|
|
||||||
if session_factory is None:
|
|
||||||
# Create a sessionmaker using the same engine as the global db session
|
|
||||||
session_factory = sessionmaker(bind=db.engine)
|
|
||||||
|
|
||||||
# Create and return the repository
|
|
||||||
return SQLAlchemyWorkflowNodeExecutionRepository(
|
|
||||||
session_factory=session_factory, tenant_id=tenant_id, app_id=app_id
|
|
||||||
)
|
|
||||||
@ -1,9 +0,0 @@
|
|||||||
"""
|
|
||||||
WorkflowNodeExecution repository implementations.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from core.repositories.workflow_node_execution.sqlalchemy_repository import SQLAlchemyWorkflowNodeExecutionRepository
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
"SQLAlchemyWorkflowNodeExecutionRepository",
|
|
||||||
]
|
|
||||||
@ -1,97 +0,0 @@
|
|||||||
"""
|
|
||||||
Repository factory for creating repository instances.
|
|
||||||
|
|
||||||
This module provides a simple factory interface for creating repository instances.
|
|
||||||
It does not contain any implementation details or dependencies on specific repositories.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from collections.abc import Callable, Mapping
|
|
||||||
from typing import Any, Literal, Optional, cast
|
|
||||||
|
|
||||||
from core.workflow.repository.workflow_node_execution_repository import WorkflowNodeExecutionRepository
|
|
||||||
|
|
||||||
# Type for factory functions - takes a dict of parameters and returns any repository type
|
|
||||||
RepositoryFactoryFunc = Callable[[Mapping[str, Any]], Any]
|
|
||||||
|
|
||||||
# Type for workflow node execution factory function
|
|
||||||
WorkflowNodeExecutionFactoryFunc = Callable[[Mapping[str, Any]], WorkflowNodeExecutionRepository]
|
|
||||||
|
|
||||||
# Repository type literals
|
|
||||||
_RepositoryType = Literal["workflow_node_execution"]
|
|
||||||
|
|
||||||
|
|
||||||
class RepositoryFactory:
|
|
||||||
"""
|
|
||||||
Factory class for creating repository instances.
|
|
||||||
|
|
||||||
This factory delegates the actual repository creation to implementation-specific
|
|
||||||
factory functions that are registered with the factory at runtime.
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Dictionary to store factory functions
|
|
||||||
_factory_functions: dict[str, RepositoryFactoryFunc] = {}
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _register_factory(cls, repository_type: _RepositoryType, factory_func: RepositoryFactoryFunc) -> None:
|
|
||||||
"""
|
|
||||||
Register a factory function for a specific repository type.
|
|
||||||
This is a private method and should not be called directly.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repository_type: The type of repository (e.g., 'workflow_node_execution')
|
|
||||||
factory_func: A function that takes parameters and returns a repository instance
|
|
||||||
"""
|
|
||||||
cls._factory_functions[repository_type] = factory_func
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def _create_repository(cls, repository_type: _RepositoryType, params: Optional[Mapping[str, Any]] = None) -> Any:
|
|
||||||
"""
|
|
||||||
Create a new repository instance with the provided parameters.
|
|
||||||
This is a private method and should not be called directly.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
repository_type: The type of repository to create
|
|
||||||
params: A dictionary of parameters to pass to the factory function
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A new instance of the requested repository
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
ValueError: If no factory function is registered for the repository type
|
|
||||||
"""
|
|
||||||
if repository_type not in cls._factory_functions:
|
|
||||||
raise ValueError(f"No factory function registered for repository type '{repository_type}'")
|
|
||||||
|
|
||||||
# Use empty dict if params is None
|
|
||||||
params = params or {}
|
|
||||||
|
|
||||||
return cls._factory_functions[repository_type](params)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def register_workflow_node_execution_factory(cls, factory_func: WorkflowNodeExecutionFactoryFunc) -> None:
|
|
||||||
"""
|
|
||||||
Register a factory function for the workflow node execution repository.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
factory_func: A function that takes parameters and returns a WorkflowNodeExecutionRepository instance
|
|
||||||
"""
|
|
||||||
cls._register_factory("workflow_node_execution", factory_func)
|
|
||||||
|
|
||||||
@classmethod
|
|
||||||
def create_workflow_node_execution_repository(
|
|
||||||
cls, params: Optional[Mapping[str, Any]] = None
|
|
||||||
) -> WorkflowNodeExecutionRepository:
|
|
||||||
"""
|
|
||||||
Create a new WorkflowNodeExecutionRepository instance with the provided parameters.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
params: A dictionary of parameters to pass to the factory function
|
|
||||||
|
|
||||||
Returns:
|
|
||||||
A new instance of the WorkflowNodeExecutionRepository
|
|
||||||
|
|
||||||
Raises:
|
|
||||||
ValueError: If no factory function is registered for the workflow_node_execution repository type
|
|
||||||
"""
|
|
||||||
# We can safely cast here because we've registered a WorkflowNodeExecutionFactoryFunc
|
|
||||||
return cast(WorkflowNodeExecutionRepository, cls._create_repository("workflow_node_execution", params))
|
|
||||||
@ -1,18 +0,0 @@
|
|||||||
"""
|
|
||||||
Extension for initializing repositories.
|
|
||||||
|
|
||||||
This extension registers repository implementations with the RepositoryFactory.
|
|
||||||
"""
|
|
||||||
|
|
||||||
from core.repositories.repository_registry import register_repositories
|
|
||||||
from dify_app import DifyApp
|
|
||||||
|
|
||||||
|
|
||||||
def init_app(_app: DifyApp) -> None:
|
|
||||||
"""
|
|
||||||
Initialize repository implementations.
|
|
||||||
|
|
||||||
Args:
|
|
||||||
_app: The Flask application instance (unused)
|
|
||||||
"""
|
|
||||||
register_repositories()
|
|
||||||
Loading…
Reference in New Issue