forked from maximusunc/icees-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
initdb.py
76 lines (60 loc) · 2.04 KB
/
initdb.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
import sys
import json
import os
import subprocess
import time
import psycopg2
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import tempfile
from features import features
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
ch = logging.StreamHandler()
ch.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s:%(name)s:%(levelname)s:%(message)s')
ch.setFormatter(formatter)
logger.addHandler(ch)
logger.info("waiting for database startup")
time.sleep(10)
os.environ["ICEES_HOST"]="localhost"
os.environ["ICEES_PORT"]="5432"
import dbutils
import sample
dbuser = os.environ["ICEES_DBUSER"]
dbpass = os.environ["ICEES_DBPASS"]
conn = psycopg2.connect(dbname='postgres', user="postgres", host='localhost', password="postgres")
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
cursor.execute("SELECT count(*) FROM pg_catalog.pg_user WHERE usename = '" + dbuser + "'")
p = cursor.fetchall()[0][0]
if p == 0:
logger.info("user not found initializing db")
cursor.execute("CREATE USER " + dbuser + " with password '" + dbpass + "'")
db = os.environ["ICEES_DATABASE"]
cursor.execute("CREATE DATABASE " + db)
cursor.execute("GRANT ALL ON DATABASE " + db + " to " + dbuser)
dbutils.create()
csvdir = "db/data/"
for t in features.features_dict.keys():
table_dir = csvdir + "/" + t
if os.path.isdir(table_dir):
logger.info(table_dir + " exists")
for f in os.listdir(table_dir):
table = table_dir + "/" + f
logger.info("loading " + table)
dbutils.insert(table, t)
else:
logger.info("generating data " + t)
temp = tempfile.NamedTemporaryFile()
try:
sample.generate_data(t, [2010], 1000, temp.name)
dbutils.insert(temp.name, t)
finally:
temp.close()
dbutils.create_indices()
logger.info("db initialized")
else:
logger.info("db already initialized")
cursor.close()
conn.close()