Last active
June 1, 2018 13:47
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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