Search code examples
postgresqlazurealembic

Updating Azure Database for PostgreSQL flexible server using Alembic


I'm looking to amend some tables I have in a PostgreSQL instance on Azure, but I cannot work out how to perform the upgrades with Alembic.

I have been following the tutorial here, which includes a Heroku deployment around the 12:01:00 mark. In that case, once the changes have been defined, we can run heroku run "alembic upgrade head" to perform the upgrade. However, I cannot find the equivelant process for Azure.

My postgres instance is housed in a VNet and connected to a web app. Until now, I've made code changes to a server which is running in an attached web app. I push to GitHub which then deploys the changes in Azure. Obviously, if the table already exists in postgres, changes I make to the original schema are not reflected. I considered deleting the table and stating again, but this seems a very risky strategy.

A similar question was asked here, but has remained unanswered. I've also checked the documentation for Alembic and Azure but could not find anything.


Solution

  • What worked for me was:

    • Including Alembic in my virtual environment (requirements.txt file)
    • Ensuring the alembic.ini file is at the root of my project which gets deployed via GitHub to Azure
    • Updating alembic/env.py to parse the Azure environment set AZURE_POSTGRESQL_CONNECTIONSTRING to set the appropriate connection values for Alembic using:
    config.set_main_option('sqlalchemy.url', <connection_string>)
    
    alembic upgrade head; gunicorn --bind=0.0.0.0 --timeout 600 application:app