forked from Durian-Inc/mingle-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmanage.py
100 lines (83 loc) · 2.58 KB
/
manage.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
"""Helper program to make OS level operations on PostgreSQL tables"""
import argparse
import time
from app.serve import db
from app.models import tables
def create_all():
"""Initialize the tables for each model defined in the app"""
with db:
db.create_tables(tables)
def create_some(table_names):
"""Initialize the table for one specific model input by the user"""
targets = []
for table in tables:
for table_name in table_names:
if table_name == type(table).__name__:
targets.append(table)
if targets:
with db:
db.create_tables(targets)
else:
print("Enter valid tables")
def drop_all():
"""Drop the tables for each model defined in the app"""
with db:
db.drop_tables(tables)
def drop_some(table_names):
"""Initialize the table for one specific model input by the user"""
targets = []
for table in tables:
for table_name in table_names:
if table_name == type(table).__name__:
targets.append(table)
if targets:
with db:
db.drop_tables(targets)
else:
print("Enter valid tables")
def parse_args():
"""
Handles command line arguments and returns the passed parameters as a
namespace
"""
parser = argparse.ArgumentParser(
description="Manage the application database")
parser.add_argument(
"-c",
"--create",
help=
"Create a table, or typing \"all\" will create all tables. May be repeated.",
action='append')
parser.add_argument(
"-d",
"--drop",
help=
"Drop a table, or typing \"all\" will drop all tables. May be repeated.",
action='append')
args = parser.parse_args()
if args.create is None and args.drop is None:
parser.print_help()
return None
return args
def handle_args(args):
"""Takes passed namespace(args) and decides which operations to perform"""
if args is None:
return
if args.create and "all" in args.create:
print("CREATING ALL TABLES IN 4 SECONDS. CANCEL WITH CONTROL+C.")
time.sleep(4)
create_all()
return
if args.drop and "all" in args.drop:
print("DROPPING ALL TABLES IN 4 SECONDS. CANCEL WITH CONTROL+C.")
time.sleep(4)
drop_all()
return
if args.create:
print("Creating:", args.create)
create_some(args.create)
if args.drop:
print("Dropping:", args.drop)
drop_some(args.drop)
if __name__ == "__main__":
handle_args(parse_args())