-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_setup.py
More file actions
73 lines (63 loc) · 2.39 KB
/
Copy pathdb_setup.py
File metadata and controls
73 lines (63 loc) · 2.39 KB
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
from typing import List
import asyncio
import pandas as pd
import sqlalchemy
import sqlalchemy.ext.asyncio
from sqlalchemy import MetaData
def python_type_to_sqlalchemy_type(python_type):
if python_type == int:
return sqlalchemy.Integer
elif python_type == float:
return sqlalchemy.Float
elif python_type == str or python_type == object:
return sqlalchemy.String
elif python_type == bool:
return sqlalchemy.Boolean
else:
raise ValueError(f'Unknown python type {python_type}')
class DBTableSetup(object):
def __init__(self, connection_uri):
self._sql_engine = sqlalchemy.ext.asyncio.create_async_engine(
connection_uri,
)
async def _create_table(self, table_name, table_columns):
'''
creates the table in the database
'''
async with self._sql_engine.begin() as conn:
metadata = MetaData(bind=conn)
_ = sqlalchemy.Table(table_name, metadata, *[
sqlalchemy.Column(c_name, c_type, primary_key=is_pk) for c_name, c_type, is_pk in table_columns
])
await conn.run_sync(lambda conn: metadata.create_all(conn))
async def _insert_data_in_table(self, table_name: str, data: pd.DataFrame):
'''
inserts rows in the table
'''
async with self._sql_engine.begin() as conn:
try:
await conn.run_sync(lambda conn: data.to_sql(table_name, conn, if_exists='append', index=False))
except sqlalchemy.exc.IntegrityError:
print(f"Skipping: Blob table is already populated with the blobs")
def insert_blob_data(self, table_name, blob_data: pd.DataFrame, primary_key_cols: List[str]):
'''
inserts the blob data in the blob table
'''
assert len(primary_key_cols) > 0, 'Primary key should be specified'
assert blob_data.shape[0] > 0, 'No blobs to insert in the blob table'
table_columns = []
for column in blob_data.columns:
dtype = python_type_to_sqlalchemy_type(blob_data[column].dtype)
if dtype == sqlalchemy.String:
c_type = sqlalchemy.Text()
else:
c_type = dtype
table_columns.append((column, c_type, column in primary_key_cols))
try:
loop = asyncio.get_event_loop()
except Exception as e:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(self._create_table(table_name, table_columns))
loop.run_until_complete(self._insert_data_in_table(table_name, blob_data))
loop.close()