sqlalchemy / alembic

A database migrations tool for SQLAlchemy.
MIT License
2.61k stars 234 forks source link

command.upgrade(alembic_cfg, "head") never returns and blocks the process indefinitely. #1482

Closed oefterdal closed 1 month ago

oefterdal commented 1 month ago

Bug Description: Running command.upgrade(alembic_cfg, "head") never returns and blocks the process indefinitely.

Expected Behavior: The process should complete and return, either successfully or with a failure.

To Reproduce:

def run_migrations():
    try:
        logger.info("Starting database migrations")
        alembic_cfg = Config("alembic.ini")
        command.upgrade(alembic_cfg, "head")
        logger.info("Database migrations completed successfully")
    except Exception as e:
        logger.error(f"Migration failed: {e}")
        raise

Error:

# Copy error here. Please include the full stack trace.

Environment:

Additional Context: The following workaround using subprocess works:

def run_migrations():
    try:
        logger.info("Starting database migrations")

        # Run the Alembic upgrade command using subprocess
        result = subprocess.run(["alembic", "upgrade", "head"], capture_output=True, text=True)
        if result.returncode != 0:
            logger.error(f"Alembic upgrade failed: {result.stderr}")
            raise RuntimeError(f"Alembic upgrade failed: {result.stderr}")

        logger.info("Database migrations completed successfully")
    except Exception as e:
        logger.error(f"Migration failed: {e}")
        raise
    finally:
        # Ensure the engine is disposed
        engine.dispose()
        logger.info("Disposed of the engine to close all connections.")

Have a nice day!