Fix Alembic issue

This commit is contained in:
Urtzi Alfaro
2025-10-01 11:24:06 +02:00
parent 7cc4b957a5
commit 2eeebfc1e0
62 changed files with 6114 additions and 3676 deletions

View File

@@ -36,13 +36,17 @@ class DatabaseInitManager:
service_name: str,
alembic_ini_path: Optional[str] = None,
models_module: Optional[str] = None,
force_recreate: bool = False
force_recreate: bool = False,
allow_create_all_fallback: bool = True,
environment: Optional[str] = None
):
self.database_manager = database_manager
self.service_name = service_name
self.alembic_ini_path = alembic_ini_path
self.models_module = models_module
self.force_recreate = force_recreate
self.allow_create_all_fallback = allow_create_all_fallback
self.environment = environment or os.getenv('ENVIRONMENT', 'development')
self.logger = logger.bind(service=service_name)
async def initialize_database(self) -> Dict[str, Any]:
@@ -68,12 +72,24 @@ class DatabaseInitManager:
if self.force_recreate:
result = await self._handle_force_recreate()
elif not db_state["has_migrations"]:
# No migration files found - use create_all() as fallback
self.logger.warning(
"No migration files found - using create_all() as fallback. "
"Consider generating proper migrations for production use."
)
result = await self._handle_no_migrations()
# No migration files found - check if fallback is allowed
if self.allow_create_all_fallback:
self.logger.warning(
"No migration files found - using create_all() as fallback. "
"Consider generating proper migrations for production use.",
environment=self.environment
)
result = await self._handle_no_migrations()
else:
# In production or when fallback is disabled, fail instead of using create_all
error_msg = (
f"No migration files found for {self.service_name} and "
f"create_all() fallback is disabled (environment: {self.environment}). "
f"Migration files must be generated before deployment. "
f"Run migration generation script to create initial migrations."
)
self.logger.error(error_msg)
raise Exception(error_msg)
else:
result = await self._handle_run_migrations()
@@ -253,7 +269,9 @@ def create_init_manager(
database_manager: DatabaseManager,
service_name: str,
service_path: Optional[str] = None,
force_recreate: bool = False
force_recreate: bool = False,
allow_create_all_fallback: Optional[bool] = None,
environment: Optional[str] = None
) -> DatabaseInitManager:
"""
Factory function to create a DatabaseInitManager with auto-detected paths
@@ -263,7 +281,20 @@ def create_init_manager(
service_name: Name of the service
service_path: Path to service directory (auto-detected if None)
force_recreate: Whether to force recreate tables (development mode)
allow_create_all_fallback: Allow create_all() if no migrations (auto-detect from env if None)
environment: Environment name (auto-detect from ENVIRONMENT env var if None)
"""
# Auto-detect environment
if environment is None:
environment = os.getenv('ENVIRONMENT', 'development')
# Auto-detect fallback setting based on environment
if allow_create_all_fallback is None:
# Only allow fallback in development/local environments
allow_create_all_fallback = environment.lower() in ['development', 'dev', 'local', 'test']
allow_create_all_fallback = False
# Auto-detect paths if not provided
if service_path is None:
# Try Docker container path first (service files at root level)
@@ -293,14 +324,18 @@ def create_init_manager(
service_name=service_name,
alembic_ini_path=alembic_ini_path,
models_module=models_module,
force_recreate=force_recreate
force_recreate=force_recreate,
allow_create_all_fallback=allow_create_all_fallback,
environment=environment
)
async def initialize_service_database(
database_manager: DatabaseManager,
service_name: str,
force_recreate: bool = False
force_recreate: bool = False,
allow_create_all_fallback: Optional[bool] = None,
environment: Optional[str] = None
) -> Dict[str, Any]:
"""
Convenience function for service database initialization
@@ -309,6 +344,8 @@ async def initialize_service_database(
database_manager: DatabaseManager instance
service_name: Name of the service
force_recreate: Whether to force recreate (development mode)
allow_create_all_fallback: Allow create_all() if no migrations (auto-detect from env if None)
environment: Environment name (auto-detect from ENVIRONMENT env var if None)
Returns:
Dict with initialization results
@@ -316,7 +353,9 @@ async def initialize_service_database(
init_manager = create_init_manager(
database_manager=database_manager,
service_name=service_name,
force_recreate=force_recreate
force_recreate=force_recreate,
allow_create_all_fallback=allow_create_all_fallback,
environment=environment
)
return await init_manager.initialize_database()