From e16c749019affbe7eeea666bd749b19536b38e04 Mon Sep 17 00:00:00 2001 From: mohamad Date: Sun, 1 Jun 2025 17:29:48 +0200 Subject: [PATCH] refactor: Enhance Alembic migration functions to support direct execution and improve error handling for database URL configuration --- be/alembic/env.py | 19 +++++++++++++------ be/app/main.py | 21 ++++++--------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/be/alembic/env.py b/be/alembic/env.py index de66480..eead375 100644 --- a/be/alembic/env.py +++ b/be/alembic/env.py @@ -53,8 +53,13 @@ def do_run_migrations(connection): with context.begin_transaction(): context.run_migrations() -async def run_migrations_online_async(): +async def run_migrations_online_async(alembic_cfg=None): """Run migrations in 'online' mode asynchronously.""" + # If running from Alembic directly + if alembic_cfg: + if alembic_cfg.config_file_name is not None: + fileConfig(alembic_cfg.config_file_name) + connectable = create_async_engine( settings.DATABASE_URL, poolclass=pool.NullPool, @@ -77,9 +82,11 @@ def run_migrations_offline(): script output. """ - url = config.get_main_option("sqlalchemy.url") + if not settings.DATABASE_URL: + raise ValueError("DATABASE_URL not found in settings for Alembic.") + context.configure( - url=url, + url=settings.DATABASE_URL, target_metadata=target_metadata, literal_binds=True, dialect_opts={"paramstyle": "named"}, @@ -88,8 +95,8 @@ def run_migrations_offline(): with context.begin_transaction(): context.run_migrations() +# This section only runs when executing alembic commands directly (not when imported) if context.is_offline_mode(): run_migrations_offline() -else: - # Don't run migrations here - they will be run from FastAPI - pass +elif 'ALEMBIC_CONFIG' in os.environ: # Only run if called from alembic command + asyncio.run(run_migrations_online_async(context.config)) diff --git a/be/app/main.py b/be/app/main.py index e8aea86..d4d1230 100644 --- a/be/app/main.py +++ b/be/app/main.py @@ -219,25 +219,16 @@ async def run_migrations(): """Run database migrations.""" try: logger.info("Running database migrations...") - # Get the absolute path to the alembic.ini file + # Get the absolute path to the alembic directory base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) - alembic_ini_path = os.path.join(base_path, 'alembic.ini') - alembic_cfg = Config(alembic_ini_path) + alembic_path = os.path.join(base_path, 'alembic') - # Set the script_location to the absolute path - script_location = os.path.join(base_path, 'alembic') - alembic_cfg.set_main_option('script_location', script_location) + # Add alembic directory to Python path + if alembic_path not in sys.path: + sys.path.insert(0, alembic_path) - # Set the sqlalchemy.url - if not settings.DATABASE_URL: - raise ValueError("DATABASE_URL is not configured in settings.") - alembic_cfg.set_main_option('sqlalchemy.url', settings.DATABASE_URL) - - # Import the async migration function from env.py - sys.path.insert(0, script_location) + # Import and run migrations from env import run_migrations_online_async - - # Run the migration asynchronously await run_migrations_online_async() logger.info("Database migrations completed successfully.") -- 2.45.2