I have an existing app which uses SQLAlchemy for DB access. It works well.
Now I want to introduce DB migrations, and I read alembic is the recommended way. Basically I want to start with the "current DB state" (not empty DB!) as revision 0, and then track further revisions going forward.
So I installed alembic (version 1.7.3) and put
from my_project.db_tables import Base
target_metadata = Base.metadata
into my env.py
. The Base
is just standard SQLAlchemy Base = sqlalchemy.ext.declarative.declarative_base()
within my project (again, which works fine).
Then I ran alembic revision --autogenerate -m "revision0"
, expecting to see an upgrade()
method that gets me to the current DB state from an empty DB. Or maybe an empty upgrade()
, since it's the first revision, I don't know.
Instead, the upgrade()
method is full of op.drop_index
and op.drop_table
calls, while downgrade()
is all op.create_index
and op.create_table
. Basically the opposite of what I expected.
Any idea what's wrong? What's the recommended way to "initialize" migrations from an existing DB state?