-
Notifications
You must be signed in to change notification settings - Fork 20
/
makemigrations.py
88 lines (69 loc) · 1.84 KB
/
makemigrations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import os
import sys
import django
from django.apps import apps
from django.conf import settings
from django.db import connections
from django.db.utils import OperationalError
APP_NAME = "djpaypal"
DEFAULT_SETTINGS = dict(
DATABASES={
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": ":memory:"
}
},
DEBUG=True,
INSTALLED_APPS=[
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.sites",
APP_NAME,
],
SITE_ID=1,
TIME_ZONE="UTC",
)
def check_migrations():
from django.db.migrations.autodetector import MigrationAutodetector
from django.db.migrations.executor import MigrationExecutor
from django.db.migrations.state import ProjectState
changed = set()
print("Checking {} migrations...".format(APP_NAME))
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError as e:
sys.exit(
"Unable to check migrations due to database: {}".format(e)
)
autodetector = MigrationAutodetector(
executor.loader.project_state(),
ProjectState.from_apps(apps),
)
changed.update(
autodetector.changes(graph=executor.loader.graph).keys()
)
if changed and APP_NAME in changed:
sys.exit(
"A migration file is missing. Please run "
"`python makemigrations.py` to generate it."
)
else:
print("All migration files present.")
def run(*args):
"""
Check and/or create Django migrations.
If --check is present in the arguments then migrations are checked only.
"""
if not settings.configured:
settings.configure(**DEFAULT_SETTINGS)
django.setup()
parent = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, parent)
if "--check" in args:
check_migrations()
else:
django.core.management.call_command("makemigrations", APP_NAME, *args)
if __name__ == "__main__":
run(*sys.argv[1:])