Set up a database migration
If your project includes a database, you need to set up an initial migrations for it.
To do so, Upsun recommends using Flask-Migrate. If you’ve generated your Flask app using Cookiecutter, your app uses Flask-Migrate by default to handle database migrations.
Otherwise, to install Flask-Migrate, run the following command:
pip install Flask-MigrateAfter you’ve installed Flask-Migrate, you need to set up a temporary local environment and allow it to access your database,
so you can run the migrate command successfully.
Follow these steps:
-
To set up a local environment where your project can run, run the following command:
Terminalpython3 -m venv env && source venv/bin/activate -
To upgrade
pipand install your requirements, run the following commands:Terminalpip install βupgrade pip pip install -r requirements.txt -
Your database was automatically created and deployed when you first pushed your changes to Upsun. To allow your virtual environment to communicate with your database, run the following command:
Terminalplatform tunnel:open -yAn SSH tunnel to all the services for your app is open.
-
To successfully establish communication between your local environment and your services, you need to set more environment variables. To do so, add the following line to your
.upsun/config.yamlfile:.upsun/config.yamlexport PLATFORM_RELATIONSHIPS="$(platform tunnel:info --encode)"The Upsun $PLATFORM_RELATIONSHIPS variable allows you to retrieve information about services and their credentials, while
platform tunnelgenerates that same data locally. -
To set other required variables, add the following lines:
.upsun/config.yamlexport PLATFORM_ENVIRONMENT_TYPE=production export PORT=8888 export PLATFORM_PROJECT_ENTROPY=$(openssl rand -base64 32) -
To complete the setup of all the required environmental variables in your current shell, run the following command:
Terminalsource ./.environment -
To instruct Flask-migrate to initiate the migration directory, prepare for the
migratecommand, and generate your migrations, run the following commands:Terminalflask db init flask db migrate -
To commit your migrations, run the following commands:
Terminalgit add migrations/* git commit -m "Adds migrations" -
If you want future migration changes to be automatically applied, instruct Upsun to run the Flask-migrate
upgradecommand whenever your app is deployed. To do so, customize your deploy hook again. Locate the section dedicated to it:.upsun/config.yaml# The deploy hook is run after the app container has been started, but before it has started accepting requests. # More information: https://docs.upsun.com/create-apps/hooks/hooks-comparison.html#deploy-hook deploy: | set -eux npm run buildAdd
flask db upgrade:.upsun/config.yamldeploy: | set -eux npm run build flask db upgrade -
To commit and push your changes, run the following commands:
Terminalgit add .platform.app.yaml git commit -m "Adds flask db upgrade to deploy hook" platform environment:push -yYour Flask app is now fully deployed on Upsun.