Skip to content

Instantly share code, notes, and snippets.

@lesywix
Last active June 1, 2018 13:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lesywix/92f17243708f6af2dace6eaf09bb6ce6 to your computer and use it in GitHub Desktop.
Save lesywix/92f17243708f6af2dace6eaf09bb6ce6 to your computer and use it in GitHub Desktop.
Check if django DB migrations is not synchronized. Import `exit_if_db_not_synchronized` in wsgi.py
# coding: utf-8
from __future__ import absolute_import
import functools
from django.db import close_old_connections, connections, DEFAULT_DB_ALIAS
from django.db.migrations.executor import MigrationExecutor
def close_connection_after_call(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
result = f(*args, **kwargs)
close_old_connections()
return result
return wrapper
def is_db_synchronized(database=DEFAULT_DB_ALIAS):
connection = connections[database]
connection.prepare_database()
executor = MigrationExecutor(connection)
targets = executor.loader.graph.leaf_nodes()
plan = executor.migration_plan(targets)
if plan:
print('has migration plan: {}'.format(plan))
return False
return True
def exit_if_db_not_synchronized():
if not is_db_synchronized():
raise SystemExit('exit because db migrations is not synchronized')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment