Skip to main content

Alembic

How to useโ€‹

  • Modify models.py
  • alembic revision --autogenerate -m "<short description>"
  • To use specific ini/config file: alembic -c <ini file> revision --autogenerate -m "<short description" Example: alembic -c alembic_local.ini revision --autogenerate -m "sccp ecs mapping"
  • Check migrations/versions/
    • Need to use batch_op for altering/updating tables, both for upgrade and downgrade
    • No need when initially creating a table
  • Do alembic upgrade head to apply the changes specified in the migrations file to the database
  • If you used a specific ini/config file when autogenerating the revision, you also need to specify the config when upgrading the head: alembic -c alembic_local.ini upgrade head

Modifying table with no data yetโ€‹

  • Modify models.py
  • Downgrade: alembic downgrade -1
  • Delete relevant migration revision file
  • Make new revision: alembic revision --autogenerate -m "<short description>"
  • Do the upgrade: alembic upgrade head

Modifying table with data (Ex. production)โ€‹

  • Modify models.py
  • Make new revision: `alembic revision --autogenerate -m "short description"
  • Do the upgrade: alembic upgrade head

Counterpart in Flaskโ€‹

  • Modify models.py
  • flask db migrate -m "<short description>"
  • flask db upgrade