-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_migrate.py
81 lines (67 loc) · 1.96 KB
/
db_migrate.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
import argparse
import os
from subprocess import run, PIPE, STDOUT
argparser = argparse.ArgumentParser(
description="migrate/update the spectacles database schema"
)
argparser.add_argument("-i", action="store_true", help="Setup new migration directory")
argparser.add_argument("-m", action="store_true", help="Migrate the database")
argparser.add_argument("-u", action="store_true", help="Update the database")
args = argparser.parse_args()
current_dir = os.path.dirname(os.path.realpath(__file__))
if __name__ == "__main__":
os.environ["FLASK_APP"] = "docker_run.py"
init = "flask db init"
migrate = "flask db migrate"
update = "flask db upgrade"
if args.i:
result = run(
init, # nosec
stdout=PIPE,
stderr=STDOUT,
universal_newlines=True,
shell=True,
cwd=current_dir,
)
print(
{
"code": result.returncode,
"stdout": str(result.stdout)
.replace("\x1b[37m", "")
.replace("\x1b[0m\n", ""),
}
)
if args.m:
result = run(
migrate, # nosec
stdout=PIPE,
stderr=STDOUT,
universal_newlines=True,
shell=True,
cwd=current_dir,
)
print(
{
"code": result.returncode,
"stdout": str(result.stdout)
.replace("\x1b[37m", "")
.replace("\x1b[0m\n", ""),
}
)
if args.u:
result = run(
update, # nosec
stdout=PIPE,
stderr=STDOUT,
universal_newlines=True,
shell=True,
cwd=current_dir,
)
print(
{
"code": result.returncode,
"stdout": str(result.stdout)
.replace("\x1b[37m", "")
.replace("\x1b[0m\n", ""),
}
)