|
1
|
|
|
import sqlalchemy as sa |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
# Define a version number for the database generated by these writers |
|
5
|
|
|
# Increment this version number any time a change is made to the schema of the |
|
6
|
|
|
# assets database |
|
7
|
|
|
ASSET_DB_VERSION = 0 |
|
8
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
def generate_asset_db_metadata(bind=None): |
|
11
|
|
|
# NOTE: When modifying this schema, update the ASSET_DB_VERSION value |
|
12
|
|
|
metadata = sa.MetaData(bind=bind) |
|
13
|
|
|
_version_table_schema(metadata) |
|
14
|
|
|
_equities_table_schema(metadata) |
|
15
|
|
|
_futures_exchanges_schema(metadata) |
|
16
|
|
|
_futures_root_symbols_schema(metadata) |
|
17
|
|
|
_futures_contracts_schema(metadata) |
|
18
|
|
|
_asset_router_schema(metadata) |
|
19
|
|
|
return metadata |
|
20
|
|
|
|
|
21
|
|
|
|
|
22
|
|
|
# A list of the names of all tables in the assets db |
|
23
|
|
|
# NOTE: When modifying this schema, update the ASSET_DB_VERSION value |
|
24
|
|
|
asset_db_table_names = ['version_info', 'equities', 'futures_exchanges', |
|
25
|
|
|
'futures_root_symbols', 'futures_contracts', |
|
26
|
|
|
'asset_router'] |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def _equities_table_schema(metadata): |
|
30
|
|
|
# NOTE: When modifying this schema, update the ASSET_DB_VERSION value |
|
31
|
|
|
return sa.Table( |
|
32
|
|
|
'equities', |
|
33
|
|
|
metadata, |
|
34
|
|
|
sa.Column( |
|
35
|
|
|
'sid', |
|
36
|
|
|
sa.Integer, |
|
37
|
|
|
unique=True, |
|
38
|
|
|
nullable=False, |
|
39
|
|
|
primary_key=True, |
|
40
|
|
|
), |
|
41
|
|
|
sa.Column('symbol', sa.Text), |
|
42
|
|
|
sa.Column('company_symbol', sa.Text, index=True), |
|
43
|
|
|
sa.Column('share_class_symbol', sa.Text), |
|
44
|
|
|
sa.Column('fuzzy_symbol', sa.Text, index=True), |
|
45
|
|
|
sa.Column('asset_name', sa.Text), |
|
46
|
|
|
sa.Column('start_date', sa.Integer, default=0, nullable=False), |
|
47
|
|
|
sa.Column('end_date', sa.Integer, nullable=False), |
|
48
|
|
|
sa.Column('first_traded', sa.Integer, nullable=False), |
|
49
|
|
|
sa.Column('exchange', sa.Text), |
|
50
|
|
|
) |
|
51
|
|
|
|
|
52
|
|
|
|
|
53
|
|
|
def _futures_exchanges_schema(metadata): |
|
54
|
|
|
# NOTE: When modifying this schema, update the ASSET_DB_VERSION value |
|
55
|
|
|
return sa.Table( |
|
56
|
|
|
'futures_exchanges', |
|
57
|
|
|
metadata, |
|
58
|
|
|
sa.Column( |
|
59
|
|
|
'exchange', |
|
60
|
|
|
sa.Text, |
|
61
|
|
|
unique=True, |
|
62
|
|
|
nullable=False, |
|
63
|
|
|
primary_key=True, |
|
64
|
|
|
), |
|
65
|
|
|
sa.Column('timezone', sa.Text), |
|
66
|
|
|
) |
|
67
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
def _futures_root_symbols_schema(metadata): |
|
70
|
|
|
# NOTE: When modifying this schema, update the ASSET_DB_VERSION value |
|
71
|
|
|
return sa.Table( |
|
72
|
|
|
'futures_root_symbols', |
|
73
|
|
|
metadata, |
|
74
|
|
|
sa.Column( |
|
75
|
|
|
'root_symbol', |
|
76
|
|
|
sa.Text, |
|
77
|
|
|
unique=True, |
|
78
|
|
|
nullable=False, |
|
79
|
|
|
primary_key=True, |
|
80
|
|
|
), |
|
81
|
|
|
sa.Column('root_symbol_id', sa.Integer), |
|
82
|
|
|
sa.Column('sector', sa.Text), |
|
83
|
|
|
sa.Column('description', sa.Text), |
|
84
|
|
|
sa.Column( |
|
85
|
|
|
'exchange', |
|
86
|
|
|
sa.Text, |
|
87
|
|
|
sa.ForeignKey('futures_exchanges.exchange'), |
|
88
|
|
|
), |
|
89
|
|
|
) |
|
90
|
|
|
|
|
91
|
|
|
|
|
92
|
|
|
def _futures_contracts_schema(metadata): |
|
93
|
|
|
# NOTE: When modifying this schema, update the ASSET_DB_VERSION value |
|
94
|
|
|
return sa.Table( |
|
95
|
|
|
'futures_contracts', |
|
96
|
|
|
metadata, |
|
97
|
|
|
sa.Column( |
|
98
|
|
|
'sid', |
|
99
|
|
|
sa.Integer, |
|
100
|
|
|
unique=True, |
|
101
|
|
|
nullable=False, |
|
102
|
|
|
primary_key=True, |
|
103
|
|
|
), |
|
104
|
|
|
sa.Column('symbol', sa.Text, unique=True, index=True), |
|
105
|
|
|
sa.Column( |
|
106
|
|
|
'root_symbol', |
|
107
|
|
|
sa.Text, |
|
108
|
|
|
sa.ForeignKey('futures_root_symbols.root_symbol'), |
|
109
|
|
|
index=True |
|
110
|
|
|
), |
|
111
|
|
|
sa.Column('asset_name', sa.Text), |
|
112
|
|
|
sa.Column('start_date', sa.Integer, default=0, nullable=False), |
|
113
|
|
|
sa.Column('end_date', sa.Integer, nullable=False), |
|
114
|
|
|
sa.Column('first_traded', sa.Integer, nullable=False), |
|
115
|
|
|
sa.Column( |
|
116
|
|
|
'exchange', |
|
117
|
|
|
sa.Text, |
|
118
|
|
|
sa.ForeignKey('futures_exchanges.exchange'), |
|
119
|
|
|
), |
|
120
|
|
|
sa.Column('notice_date', sa.Integer, nullable=False), |
|
121
|
|
|
sa.Column('expiration_date', sa.Integer, nullable=False), |
|
122
|
|
|
sa.Column('auto_close_date', sa.Integer, nullable=False), |
|
123
|
|
|
sa.Column('contract_multiplier', sa.Float), |
|
124
|
|
|
) |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
def _asset_router_schema(metadata): |
|
128
|
|
|
# NOTE: When modifying this schema, update the ASSET_DB_VERSION value |
|
129
|
|
|
return sa.Table( |
|
130
|
|
|
'asset_router', |
|
131
|
|
|
metadata, |
|
132
|
|
|
sa.Column( |
|
133
|
|
|
'sid', |
|
134
|
|
|
sa.Integer, |
|
135
|
|
|
unique=True, |
|
136
|
|
|
nullable=False, |
|
137
|
|
|
primary_key=True), |
|
138
|
|
|
sa.Column('asset_type', sa.Text), |
|
139
|
|
|
) |
|
140
|
|
|
|
|
141
|
|
|
|
|
142
|
|
|
def _version_table_schema(metadata): |
|
143
|
|
|
# NOTE: When modifying this schema, update the ASSET_DB_VERSION value |
|
144
|
|
|
return sa.Table( |
|
145
|
|
|
'version_info', |
|
146
|
|
|
metadata, |
|
147
|
|
|
sa.Column( |
|
148
|
|
|
'id', |
|
149
|
|
|
sa.Integer, |
|
150
|
|
|
unique=True, |
|
151
|
|
|
nullable=False, |
|
152
|
|
|
primary_key=True, |
|
153
|
|
|
), |
|
154
|
|
|
sa.Column( |
|
155
|
|
|
'version', |
|
156
|
|
|
sa.Integer, |
|
157
|
|
|
unique=True, |
|
158
|
|
|
nullable=False, |
|
159
|
|
|
), |
|
160
|
|
|
# This constraint ensures a single entry in this table |
|
161
|
|
|
sa.CheckConstraint('id <= 1'), |
|
162
|
|
|
) |
|
163
|
|
|
|