Skip to content

Instantly share code, notes, and snippets.

@kylefox
Last active October 23, 2020 08:42
Show Gist options
  • Save kylefox/7044491259b6b2586ca1 to your computer and use it in GitHub Desktop.
Save kylefox/7044491259b6b2586ca1 to your computer and use it in GitHub Desktop.
Run Django database migrations after deploy to Heroku. This file must live at `bin/post_compile` within your root project directory.
# !/usr/bin/env bash
# File path should be ./bin/post_compile
# (.sh extension added in Gist just to enable shell syntax highlighting.
# https://discussion.heroku.com/t/django-automaticlly-run-syncdb-and-migrations-after-heroku-deploy-with-a-buildpack-or-otherwise/466/7
echo "=> Performing database migrations..."
python manage.py migrate
@ebarojas
Copy link

This is great, works like a charm ->

I added a conditional env var in order to control whether you want this in all your environments:

if [ $AUTO_MIGRATE == True ]; then
  echo "=> Performing database migrations..."
  python 'app_name'/manage.py migrate
fi

@wooyek
Copy link

wooyek commented Dec 29, 2016

I may state the obvious, but to help anyone who gets here looking to solve No such file or directory error…

Make sure you have LF line endings!

@freezed
Copy link

freezed commented Oct 20, 2018

This solution may be outdated with the release option of the Procfile.

See StackOverflow answer or Heroku doc.

git push heroku ersatz-model:master 

(…)

remote: Compressing source files... done.
remote: Building source:
remote: 
remote: -----> Python app detected
remote:        Using supported version of Python 3.6 (python-3.6.6)
remote: -----> Installing requirements with pip
remote: 
remote: -----> $ python manage.py collectstatic --noinput
remote:        120 static files copied to '/tmp/…/staticfiles', 376 post-processed.
remote: 
remote: -----> Discovering process types
remote:        Procfile declares types -> release, web
remote: 
remote: -----> Compressing...
remote:        Done: 56.5M
remote: -----> Launching...
remote:  !     Release command declared: this new release will not be available until the command succeeds.
remote:        Released v41
remote:        https://….herokuapp.com/ deployed to Heroku
remote: 
remote: Verifying deploy... done.
remote: Running release command...
remote: 
remote: Operations to perform:
remote:   Apply all migrations: admin, auth, contenttypes, ersatz, sessions
remote: Running migrations:
remote:   Applying ersatz.0006_auto_20181020_0053... OK
remote: Waiting for release.... done.

@AbdouTlili
Copy link

i think you can just use the release command in your Procfile
for exemple to migrate :

release: python manage.py migrate
web: gunicorn django_proj.wsgi --log-file -

or runs a script :

release: ./ post_compile.sh
web: gunicorn django_proj.wsgi --log-file -

the command release runs after :

  • A successful app build
  • A change to the value of a config var (unless the config var is associated with an add-on)
  • A pipeline promotion
  • A rollback
  • A release via the platform API
  • Provisioning a new add-on

@segunisreal
Copy link

segunisreal commented Jun 10, 2020

Just do:

release: python manage.py migrate --no-input
web: gunicorn project_name.wsgi

and you will be fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment