3

I'm using Alembic with SQLAlchemy to do a schema migration and a data migration.

Since I have stored migration-specific dependencies in their own module within my project's main application myapp (as per Michael Bayer's recommendation), I have run into ModuleNotFoundErrors when running alembic current and alembic history.

As suggested here, I have added the repository directory project to my system path by adding the following to env.py.

# Add project to path.
sys.path.append(os.getcwd())

This fixes the ModuleNotFoundError for alembic current, but not alembic history.

Why do these two commands give different results?


Repository structure:

project
│   alembic.ini
│   db.sqlite
│
├───alembic
│   │   env.py
│   │   README
│   │   script.py.mako
│   │
│   └───versions
│           2b939015022a_create_athlete_table.py
│           3649f2977ae1_migrate_athletes_to_star_athletes.py
│
└───myapp
    └───migrations
            star_athlete.py

Migration script:

"""migrate athletes to star_athletes

Revision ID: 3649f2977ae1
Revises: 2b939015022a
Create Date: 2019-06-12 09:46:16.173048

"""
from alembic import op
import sqlalchemy as sa

# This line causes ModuleNotFoundError
from myapp.migrations.star_athlete import StarAthlete


# revision identifiers, used by Alembic.
revision = '3649f2977ae1'
down_revision = '2b939015022a'
branch_labels = None
depends_on = None


def upgrade():
    print('upgrading...')


def downgrade():
    print('downgrading...')
Nathaniel Jones
  • 939
  • 1
  • 14
  • 25

1 Answers1

0

Yes, because the import path is modified by Alembic's env.py (even prior to your modifications), but by default env.py is not executed by alembic history. You can fix this by setting revision_environment = true in alembic.ini.

David Scarlett
  • 3,171
  • 2
  • 12
  • 28