-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_db.py
94 lines (75 loc) · 2.41 KB
/
create_db.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
"""A helper utility to automatically create a database for the ACM-HSCC app
Author: Logan Gore
This file is responsible for (at the bare minimum) creating the database and
all associated tables for the ACM-HSCC app. It will import all models and
ensure that a table for each model exists.
"""
import argparse
import os
import sys
from hscc import db
from hscc.models import PasswordReset
from hscc.models import School
from hscc.models import User
PARSER = argparse.ArgumentParser(description='ACM-HSCC DB Creation Tool')
PARSER.add_argument(
'-d', '--drop', action='store_true',
help='Drop existing DB tables before recreation'
)
PARSER.add_argument(
'-p', '--populate', action='store_true',
help='Populate the DB with default values after creation'
)
PARSER.add_argument(
'-v', '--verbose', action='store_true',
help='Show extra output about which stage the script is executing'
)
ARGS = PARSER.parse_args()
def vprint(s='', endl='\n'):
"""Print a string if verbose mode is enabled"""
if ARGS.verbose:
sys.stderr.write('{s}{endl}'.format(s=s, endl=endl))
def populate_db_users():
"""Populate the database User model"""
users = [
User(
name='ACM-HSCC Admin',
email='[email protected]',
password=os.environ['ACM_HSCC_ADMIN_PASSWORD'],
school=None,
team=None,
grade=None,
shirt_size=None,
allergies=None,
is_admin=True,
language=None,
),
]
db.session.add_all(users)
db.session.commit()
def populate_db_all():
"""Completely populate a basic db for ACM-HSCC"""
if 'ACM_HSCC_ADMIN_PASSWORD' not in os.environ:
print('Please set env variable ACM_HSCC_ADMIN_PASSWORD first.')
return False
vprint('Starting DB population script...')
populate_db_users()
vprint('User model populated.')
vprint('\nDB population script complete.')
return True
if __name__ == '__main__':
vprint('CreateDB script loaded.')
if ARGS.drop:
vprint('Dropping all existing data first!')
db.session.close()
db.drop_all()
vprint('DB dropped.')
db.create_all()
vprint('All database models created.')
res = True
if ARGS.populate:
res = populate_db_all()
if res:
vprint('CreateDB script exiting successfully.')
else:
vprint('CreateDB script exited with failure!')