Alembic is a lightweight Python library that helps manage database migrations.
The following files are present in this directory OR related to it:
script.py.mako: This is a Mako template file which is used to generate new migration scripts. Whatever is here is used to generate new files withinversions/. This is scriptable so that the structure of each migration file can be controlled, including standard imports to be within each, as well as changes to the structure of theupgrade()anddowngrade()functionsenv.py: The main script that sets up the migration environment.alembic.ini: Thealembicconfiguration file. Located in the root of the repository/versions: The directory which contains the migration scriptsapply_migrations.py: A Python script, located in the root directory, which applies any outstanding migrations to the databaseexecute.sh: A shell script in the root directory which runs theapply_migrations.pyscript. Called by DigitalOcean when deploying the application.
To generate a new migration, run the following command from the root directory:
alembic revision --autogenerate -m "Description for migration"Then, locate the new revision script in /versions and modify the update and downgrade functions as needed
Once you have generated a new migration, you can upgrade and downgrade the database using the alembic command line tool.
Finally, make sure to commit your changes to the repository.
As long as new migrations are generated and stored in the /versions directory, Alembic will apply them, in the order they were made, to the production database.