How to migrate db in django using CI/CD with Google CloudSQL?

Viewed 1531

I have a django repository setup in gitlab and I am trying to automate build and deploy on google cloud using gitlab CI/CD.

The app has to be deployed on App Engine and has to use CloudSQL for dynamic data storage.

Issue that I am facing is while executing migration on the db, before deploying my application.

I am supposed to run ./manage.py migrate which connects to cloudSQL.

I have read that we can use cloud proxy to connect to cloudSQL and migrate db. But it kind of seems like a hack. Is there a way to migrate my db via CI/CD pipeline script?

Any help is appreciated. Thanks.

2 Answers

When running Django in the App Engine Standard environment the recommended way of approaching database migration is to run ./manage.py migrate directly from the Console shell or from your local machine (which requires using the cloud sql proxy).

If you want the database migration to be decoupled from your application deployment and run it in Gitlab CI/CD you could do something along these lines:

  • Use as base image google/cloud-sdk:latest
  • Acquire credentials gcloud auth activate-service-account --key-file $GOOGLE_SERVICE_ACCOUNT_FILE
  • Download the cloudsqlproxy with wget https://dl.google.com/cloudsql/cloud_sql_proxy.linux.amd64 -O cloud_sql_proxy and make it executable chmod +x cloud_sql_proxy.
  • Start the proxy ./cloud_sql_proxy -instances="[YOUR_INSTANCE_CONNECTION_NAME]"=tcp:3306.
  • Finally, run the migration script.

You might also create a custom docker image that already does what's above behind the scenes, the result would be the same.

If you want to read further on the matter I suggest taking a look at the following articles link1 link2.

I'm also trying to find the correct way to do that. One other hacky way would be to just add a call to it in the settings file that is loaded with your app. something like the migrate.py file does:

from django.core.management import execute_from_command_line
execute_from_command_line(['./manage.py', 'migrate'])

so everytime after you'll deploy a new version of the app it will also run the migrate.

I want to beleive there are other ways, not involving the proxy, especially if you also want to work with a private ip for the sql - then this script must run in the same vpc.

Related