Skip to content

Commit

Permalink
Retry if can't recreate table due to deadlock
Browse files Browse the repository at this point in the history
  • Loading branch information
squaresmile committed Aug 21, 2024
1 parent 7c03e7c commit b7032be
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions app/db/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from typing import Any, Optional, Sequence, Union

import orjson
import sqlalchemy
from pydantic import DirectoryPath
from sqlalchemy import Table
from sqlalchemy.engine import Connection, Engine
Expand Down Expand Up @@ -54,8 +55,16 @@

def recreate_table(conn: Connection, table: Table) -> None: # pragma: no cover
logger.debug(f"Recreating table {table.name}")
table.drop(conn, checkfirst=True)
table.create(conn, checkfirst=True)
for _ in range(10):
try:
table.drop(conn, checkfirst=True)
table.create(conn, checkfirst=True)
return
except sqlalchemy.exc.OperationalError as e:
logger.exception(e)
time.sleep(15)

raise Exception(f"Failed to recreate table {table.name}")


def insert_db(conn: Connection, table: Table, db_data: Any) -> None: # pragma: no cover
Expand Down

0 comments on commit b7032be

Please # to comment.