Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unhandled error #2446

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions packages/api/egapro/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,33 +88,49 @@ class table:

@classmethod
async def fetch(cls, sql: str, *params):
conn: asyncpg.connection.Connection
async with cls.pool.acquire() as conn:
return await conn.fetch(sql, *params, record_class=cls.record_class)
try:
conn: asyncpg.connection.Connection
async with cls.pool.acquire() as conn:
return await conn.fetch(sql, *params, record_class=cls.record_class)
except AttributeError:
logger.warning("Database connection not initialized")
return []

@classmethod
async def fetchrow(cls, sql: str, *params):
conn: asyncpg.connection.Connection
async with cls.pool.acquire() as conn:
row = await conn.fetchrow(sql, *params, record_class=cls.record_class)
if not row:
try:
conn: asyncpg.connection.Connection
async with cls.pool.acquire() as conn:
row = await conn.fetchrow(sql, *params, record_class=cls.record_class)
if not row:
raise NoData
return row
except AttributeError:
logger.warning("Database connection not initialized")
raise NoData
return row

@classmethod
async def fetchval(cls, sql: str, *params):
conn: asyncpg.connection.Connection
async with cls.pool.acquire() as conn:
row = await conn.fetchval(sql, *params)
if row is None:
try:
conn: asyncpg.connection.Connection
async with cls.pool.acquire() as conn:
row = await conn.fetchval(sql, *params)
if row is None:
raise NoData
return row
except AttributeError:
logger.warning("Database connection not initialized")
raise NoData
return row

@classmethod
async def execute(cls, sql: str, *params):
conn: asyncpg.connection.Connection
async with cls.pool.acquire() as conn:
return await conn.execute(sql, *params)
try:
conn: asyncpg.connection.Connection
async with cls.pool.acquire() as conn:
return await conn.execute(sql, *params)
except AttributeError:
logger.warning("Database connection not initialized")
return None

class referent(table):
record_class = ReferentRecord
Expand Down Expand Up @@ -719,7 +735,9 @@ async def create():

async def terminate():
try:
await table.pool.close()
print("Closing DB pool.")
if table.pool is not None:
await table.pool.close()
table.pool = None
logger.info("Database connection pool closed")
except AttributeError:
print("DB not initialized, nothing to do.")
logger.warning("Database connection was not initialized")
Loading