Skip to content

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

@oefterdal

Description

@oefterdal

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:

  • OS: macOS 14.15
  • Python: 3.12
  • Alembic: 1.13.1
  • SQLAlchemy: 2.0.30
  • Database: SQLite
  • DBAPI: SQLModel 2.0.0

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!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions