alembic revision --autogenerate inspects the state of the connected database and the state of the target metadata and then creates a migration that brings the database in line with metadata.
If you are introducing alembic/sqlalchemy to an existing database, and you want a migration file that given an empty, fresh database would reproduce the current state- follow these steps.
-
Ensure that your
metadatais truly in line with your currentdatabase(i.e. ensure that runningalembic revision --autogeneratecreates a migration with zero operations). -
Create a new
temp_dbthat is empty and point yoursqlalchemy.urlinalembic.inito this newtemp_db. -
Run
alembic revision --autogenerate. This will create your desired bulk migration that brings a fresh db in line with the current one. -
Remove
temp_dband re-pointsqlalchemy.urlto your existing database. -
Run
alembic stamp head. This tells sqlalchemy that the current migration represents the state of the database- so next time you runalembic upgrade headit will begin from this migration.