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
metadata
is truly in line with your currentdatabase
(i.e. ensure that runningalembic revision --autogenerate
creates a migration with zero operations). -
Create a new
temp_db
that is empty and point yoursqlalchemy.url
inalembic.ini
to 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_db
and re-pointsqlalchemy.url
to 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 head
it will begin from this migration.