forked from jpnewman/papers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.py
executable file
·54 lines (45 loc) · 1.52 KB
/
run.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
import inspect
import importlib
from rethinkdb import r
from termcolor import cprint
from flask_script import Manager
from api import create_app
app = create_app('development')
manager = Manager(app)
@manager.command
def migrate():
'''
Creates Database
'''
try:
db_name = app.config['DATABASE_NAME']
conn = r.connect()
# Create Tables
if db_name not in r.db_list().run(conn):
db = r.db_create(db_name).run(conn)
print("Created database '{0}'...".format(db_name))
# Create the application tables if they do not exist
lib = importlib.import_module('api.models')
for cls in inspect.getmembers(lib, inspect.isclass):
for base in cls[1].__bases__:
if base.__name__ == "RethinkDBModel":
table_name = getattr(cls[1], '_table')
r.db(db_name).table_create(table_name).run(conn)
print("Created table '{0}'...".format(table_name))
print("Running RethinkDB migration command")
except Exception as e:
cprint("An error occured --> {0}".format(e), 'red', attrs=['bold'])
@manager.command
def drop_db():
'''
Drops Database
'''
try:
db_name = app.config['DATABASE_NAME']
conn = r.connect()
if db_name in r.db_list().run(conn):
r.db_drop(db_name).run(conn)
except Exception as e:
cprint("An error occured --> {0}".format(e), 'red', attrs=['bold'])
if __name__ == '__main__':
manager.run()