forked from EdwardBetts/FincLab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
executable file
·105 lines (87 loc) · 3.59 KB
/
setup.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
101
102
103
104
105
""" Create the default database for the FincLab project. If exist, the database will be deleted and re-created.
Date: 2015 Jan 06
Author: Peter Lee
"""
import MySQLdb as mdb
from lib.ReadConfig import ReadMySQLConfig
def resetDatabase():
""" Reset the entire database """
dbRoot = ReadMySQLConfig('MySQL_root')
dbUser = ReadMySQLConfig('MySQL_finclab')
try:
conn = mdb.Connection(**dbRoot)
cursor = conn.cursor()
query = """
# Drop user
DROP USER IF EXISTS {user}@{host};
FLUSH PRIVILEGES;
# Create a new database 'finclab'
DROP DATABASE IF EXISTS {db};
CREATE DATABASE {db};
USE {db};
# Create a new user and grant privileges
CREATE USER {user}@{host} IDENTIFIED BY '{passwd}';
GRANT ALL PRIVILEGES on {db}.* TO {user}@{host};
FLUSH PRIVILEGES;
# Create the 'exchange' table
CREATE TABLE exchange (
id int NOT NULL AUTO_INCREMENT,
abbrev varchar(32) NOT NULL,
name varchar(255) NOT NULL,
city varchar(255) NULL,
country varchar(255) NULL,
currency varchar(64) NULL,
timezone_offset time NULL,
created_date datetime NOT NULL,
last_updated_date datetime NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
# Create the 'data_vendor' table
CREATE TABLE data_vendor (
id int NOT NULL AUTO_INCREMENT,
name varchar(64) NOT NULL,
website_url varchar(255) NULL,
support_email varchar(255) NULL,
created_date datetime NOT NULL,
last_updated_date datetime NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
# Create the 'symbol' table
CREATE TABLE symbol (
id int NOT NULL AUTO_INCREMENT,
exchange_id int NULL,
ticker varchar(64) NOT NULL,
instrument varchar(64) NOT NULL,
name varchar(255) NULL,
sector varchar(255) NULL,
currency varchar(32) NULL,
created_date datetime NOT NULL,
last_updated_date datetime NOT NULL,
PRIMARY KEY (id), KEY index_exchange_id (exchange_id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
# Create the 'daily_price' table
CREATE TABLE daily_price (
id int NOT NULL AUTO_INCREMENT,
data_vendor_id int NOT NULL,
symbol_id int NOT NULL,
price_date datetime NOT NULL,
created_date datetime NOT NULL,
last_updated_date datetime NOT NULL,
open_price decimal(19,4) NULL,
high_price decimal(19,4) NULL,
low_price decimal(19,4) NULL,
close_price decimal(19,4) NULL,
adj_close_price decimal(19,4) NULL,
volume bigint NULL,
PRIMARY KEY (id), KEY index_data_vendor_id (data_vendor_id), KEY index_symbol_id (symbol_id)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
""".format(**dbUser)
cursor.execute(query)
except mdb.Error as e:
print(e)
finally:
cursor.close()
conn.close()
if __name__ == '__main__':
# Create / Reset the database
resetDatabase()