forked from dj-stripe/dj-stripe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makemigrations.py
113 lines (90 loc) · 2.78 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"""
.. module:: makemigrations
:synopsis: dj-stripe - Migrations creation/check tool
Based on: https://github.com/pinax/pinax-stripe/blob/master/makemigrations.py
.. moduleauthor:: Lee Skillen (@lskillen)
"""
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
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',
'jsonfield',
'djstripe',
],
MIDDLEWARE_CLASSES=[
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware'
],
ROOT_URLCONF='djstripe.urls',
SITE_ID=1,
TIME_ZONE='UTC',
USE_TZ=True,
)
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 dj-stripe migrations...")
for db in settings.DATABASES.keys():
try:
executor = MigrationExecutor(connections[db])
except OperationalError as ex:
sys.exit(
"Unable to check migrations due to database: {}".format(ex)
)
autodetector = MigrationAutodetector(
executor.loader.project_state(),
ProjectState.from_apps(apps),
)
changed.update(
autodetector.changes(graph=executor.loader.graph).keys()
)
if changed and 'djstripe' 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 dj-stripe 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)
try:
args = list(args)
args.pop(args.index('--check'))
is_check = True
except ValueError:
is_check = False
if is_check:
check_migrations()
else:
django.core.management.call_command(
'makemigrations', 'djstripe', *args
)
if __name__ == '__main__':
run(*sys.argv[1:])