Skip to content

Instantly share code, notes, and snippets.

@kylefox kylefox/post_compile.sh
Last active Aug 30, 2019

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

ebarojas commented Sep 22, 2016

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

This comment has been minimized.

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

This comment has been minimized.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.