Skip to content

Importing airflow.utils.db eagerly imports Alembic and emits noisy alembic.runtime.plugins INFO logs #65652

@hkc-8010

Description

@hkc-8010

Apache Airflow version

3.2.0

If "Other Airflow 2 version" selected, which one?

No response

What happened?

Importing Airflow's DB manager path emits non-actionable Alembic plugin setup logs at INFO level:

setup plugin alembic.autogenerate.schemas
setup plugin alembic.autogenerate.tables
setup plugin alembic.autogenerate.types
setup plugin alembic.autogenerate.constraints
setup plugin alembic.autogenerate.defaults
setup plugin alembic.autogenerate.comments

These logs appear during import, before any database migration action is explicitly requested.

This is reproducible in OSS Airflow, not just downstream distributions.

What you think should happen instead?

Importing airflow.utils.db or airflow.utils.db_manager should not eagerly import Alembic migration machinery or emit Alembic plugin setup logs.

Alembic should only be imported when a DB migration operation is actually invoked, such as stamp() or upgrade().

How to reproduce

I reproduced this in OSS Airflow using Breeze.

Reproduced on Python 3.10

breeze shell --python 3.10 --backend sqlite --skip-image-upgrade-check --quiet --tty disabled -- python - <<'PY'
import airflow.utils.db
print("IMPORTED airflow.utils.db")
PY

Observed output included:

setup plugin alembic.autogenerate.schemas
setup plugin alembic.autogenerate.tables
setup plugin alembic.autogenerate.types
setup plugin alembic.autogenerate.constraints
setup plugin alembic.autogenerate.defaults
setup plugin alembic.autogenerate.comments
IMPORTED airflow.utils.db

Reproduced on Python 3.13

Using a Breeze environment started with:

breeze start-airflow --python 3.13 --backend postgres

Then inside the running Airflow container:

python -c 'import airflow.utils.db; print("IMPORTED airflow.utils.db")'

Observed output again included the same Alembic setup plugin ... lines before the import completed.

Minimal comparison

Plain import airflow did not reproduce the issue in OSS Airflow 3.2.0 during my tests.

The noisy import path was specifically:

  • import airflow.utils.db
  • import airflow.utils.db_manager
  • from airflow.utils.db_manager import RunDBManager

Root cause analysis

The eager import appears to come from a top-level import in:

airflow-core/src/airflow/utils/db_manager.py

That module currently imports:

from alembic import command

at module import time.

Because airflow.utils.db imports RunDBManager, this causes Alembic to be imported during a normal module import path, which in turn emits the alembic.runtime.plugins INFO logs.

Proposed fix

Lazy-import Alembic only inside the code paths that actually need it, rather than at module import time.

For example, defer importing alembic.command until methods like create_db_from_orm() / upgrade() need to call stamp() or upgrade().

This avoids suppressing logs globally and preserves the existing Alembic behavior when migration operations are actually executed.

Are you willing to submit PR?

Yes

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions