-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_tables.py
53 lines (46 loc) · 1.63 KB
/
create_tables.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
# WARNING: NOT FOR USE IN PRODUCTION AFTER REAL DATA EXISTS!!!!!!!!!!!!!!!!!!!!!!
'''
This script creates the database tables in the SQLite file.
Update this file as you update your database.
'''
import os, sys
import importlib
import datetime
here = os.path.dirname(__file__)
# Don't forget to import your own models!
from app.models.models import *
conf = load_config(os.path.join(here,'app/secret_config.yaml'))
#onf = load_config('app/config.yaml')
sqlite_dbs = [ conf['databases']['dev'],
# conf['databases']['test'],
# add more here if multiple DBs
]
print(sqlite_dbs)
# Remove DBs
for fname in sqlite_dbs:
try:
print ("Removing {0}.".format(fname))
os.remove(fname)
except OSError:
pass
# Creates DBs
for fname in sqlite_dbs:
if os.path.isfile(fname):
print ("Database {0} should not exist at this point!".format(fname))
print ("Creating empty SQLite file: {0}.".format(fname))
open(fname, 'a').close()
def class_from_name (module_name, class_name):
# load the module, will raise ImportError if module cannot be loaded
# m = __import__(module_name, globals(), locals(), class_name)
# get the class, will raise AttributeError if class cannot be found
c = getattr(module_name, class_name)
return c
"""This file creates the database and fills it with some dummy run it after you have made changes to the models pages."""
def get_classes (db):
classes = []
for str in conf['models'][db]:
print ("\tCreating model for '{0}'".format(str))
c = class_from_name(sys.modules[__name__], str)
classes.append(c)
return classes
mainDB.create_tables(get_classes('mainDB'))