From e95af4f377837b791b8b06809f26bb47658d214b Mon Sep 17 00:00:00 2001 From: Harry Tung Date: Thu, 20 Nov 2025 16:45:57 -0800 Subject: [PATCH] Catch database initialization errors --- app/core/database.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/app/core/database.py b/app/core/database.py index f3b60dbbe..f196cdb69 100644 --- a/app/core/database.py +++ b/app/core/database.py @@ -120,13 +120,21 @@ def get_inference_deployment_records(self, **kwargs) -> Sequence[InferenceDeploy def create_tables(self) -> None: """Create the database tables, if they don't already exist.""" - with self._engine.begin() as connection: - Base.metadata.create_all(connection) + try: + with self._engine.begin() as connection: + Base.metadata.create_all(connection, checkfirst=True) + except Exception as e: + # If tables already exist (race condition with another worker), that's fine + logger.debug(f"Error creating tables (likely they already exist): {e}") def drop_tables(self) -> None: """Drop all tables in the database.""" - with self._engine.begin() as connection: - Base.metadata.drop_all(connection) + try: + with self._engine.begin() as connection: + Base.metadata.drop_all(connection, checkfirst=True) + except Exception as e: + # If tables don't exist yet, that's fine - we're about to create them + logger.debug(f"Error dropping tables (likely they don't exist yet): {e}") def reset_database(self) -> None: """Reset the database by deleting all tables and then recreating them."""