Refactor database metadata initialization and update JSON download logic for improved clarity and efficiency
This commit is contained in:
+2
-2
@@ -123,9 +123,9 @@ class settype(object):
|
||||
cursor.execute(text(sql))
|
||||
updatesBase.prepare(engine, reflect=True, schema="updates")
|
||||
dboBase.prepare(engine, reflect=True, schema="dbo")
|
||||
metadata = sqlalchemy.MetaData("updates")
|
||||
metadata = sqlalchemy.MetaData(schema="updates")
|
||||
metadata.reflect(bind=engine)
|
||||
dboMetadata = sqlalchemy.MetaData("dbo")
|
||||
dboMetadata = sqlalchemy.MetaData(schema="dbo")
|
||||
dboMetadata.reflect(bind=engine)
|
||||
session = Session(engine)
|
||||
self.dbengine["engine"] = engine
|
||||
|
||||
@@ -12,27 +12,32 @@ class Dbexec():
|
||||
def update_tvupdates(self, engine, available_updates, tablename):
|
||||
print('Creating update transaction.')
|
||||
|
||||
# Build batch insert data
|
||||
updatelist = [
|
||||
{
|
||||
'seriesid': seriesid,
|
||||
'seriesid': str(seriesid),
|
||||
'timestamp': available_updates[seriesid]
|
||||
}
|
||||
for seriesid in tqdm(
|
||||
available_updates,
|
||||
desc='Updating tvupdates table',
|
||||
desc='Preparing tvupdates records',
|
||||
unit='record'
|
||||
)
|
||||
]
|
||||
|
||||
# Commented out code for applying updates for now. Will re-enable later after downloads are verified.
|
||||
print('Applying updates to table.')
|
||||
with engine.connect() as conn:
|
||||
conn.execute(text(f"truncate table {tablename}"))
|
||||
for key, value in available_updates.items():
|
||||
seriesid = str(key)
|
||||
timestamp = value
|
||||
conn.execute(text(f"INSERT INTO {tablename} (seriesid, timestamp) VALUES (:seriesid, :timestamp)"), {'seriesid': seriesid, 'timestamp': timestamp})
|
||||
conn.commit()
|
||||
# Use a transactional context; SQLAlchemy Connection.execute accepts
|
||||
# a list of parameter mappings for bulk inserts (it will use the DBAPI
|
||||
# executemany under the hood).
|
||||
with engine.begin() as conn:
|
||||
print(f'Truncating table {tablename} before insert.')
|
||||
conn.execute(text(f"TRUNCATE TABLE {tablename}"))
|
||||
print(f'Inserting {len(updatelist)} records into {tablename}.')
|
||||
if updatelist:
|
||||
conn.execute(
|
||||
text(f"INSERT INTO {tablename} (seriesid, timestamp) VALUES (:seriesid, :timestamp)"),
|
||||
updatelist,
|
||||
)
|
||||
print('Update of tvupdates table is complete.')
|
||||
|
||||
def rawsql_select(self, engine, sqlquery, lprint):
|
||||
|
||||
Reference in New Issue
Block a user