Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run Django migrations in Google App Engine Flexible deployment step?

I have a Django app up and running in Google App Engine flexible. I know how to run migrations using the cloud proxy or by setting the DATABASES value but I would like to automate running migrations by doing it in the deployment step. However, there does not seem to be a way to run a custom script before or after the deployment.

The only way I've come up with is by doing it in the entrypoint command which you can set in the app.yaml:

entrypoint: bash -c 'python3 manage.py migrate --noinput && gunicorn -b :$PORT app.wsgi'  

This feels a lot like doing it wrong. A lot of Googling didn't provide a better answer.

like image 559
Alex Avatar asked Mar 21 '18 18:03

Alex


1 Answers

Defining the python3 manage.py migrate command in your app.yaml file will make it run every time a new instance is spawned and set up to serve traffic. Although technically this may not be an issue (no migration will happen if database schema hasn't changed) this isn't the right place to declare it.

You'd want this command to run once on every new version code push. This fits perfectly in a CI/CD approach. There are several tutorials on the Google Cloud online documentation using Bitbucket Pipelines or Travis CI for example but you can use many other CI/CD solutions.

like image 164
LundinCast Avatar answered Sep 25 '22 02:09

LundinCast