0

I would like Django to automatically apply migrations when server starts. How to do it?

I serve django with uwsgi - actually as one of emperor applications.

After publishing new version, I change wsgi.ini file and emperor restarts whole service. But database is not migrated.

But when running tests - locally - by manage.py the migrations are apllied to the database.

Currently the only idea I have is to call subprocess.check_output("manage.py migrate") from settings.py.

The problem here is that calling system command is dependent on system environment. Additionally wsgi is usually running on virtual env.

I could be missing something but is must be possible to run migrations from within the starting code of server.

Fantastory
  • 1,891
  • 21
  • 25

1 Answers1

4

In the file wsgi.py which is generated next to settings.py add:

from django.core.management import call_command

and after setup call migrate:

application = get_wsgi_application() call_command("migrate")

related answers: In django, how do I call the subcommand 'syncdb' from the initialization script?

Fantastory
  • 1,891
  • 21
  • 25