Add base kubernetes support final fix 4
This commit is contained in:
@@ -150,12 +150,33 @@ class DatabaseManager:
|
||||
# ===== TABLE MANAGEMENT =====
|
||||
|
||||
async def create_tables(self, metadata=None):
|
||||
"""Create database tables"""
|
||||
"""Create database tables with enhanced error handling and transaction verification"""
|
||||
try:
|
||||
target_metadata = metadata or Base.metadata
|
||||
table_names = list(target_metadata.tables.keys())
|
||||
logger.info(f"Creating tables: {table_names}", service=self.service_name)
|
||||
|
||||
# Use explicit transaction with proper error handling
|
||||
async with self.async_engine.begin() as conn:
|
||||
await conn.run_sync(target_metadata.create_all, checkfirst=True)
|
||||
try:
|
||||
# Create tables within the transaction
|
||||
await conn.run_sync(target_metadata.create_all, checkfirst=True)
|
||||
|
||||
# Verify transaction is not in error state
|
||||
# Try a simple query to ensure connection is still valid
|
||||
await conn.execute(text("SELECT 1"))
|
||||
|
||||
logger.info("Database tables creation transaction completed successfully",
|
||||
service=self.service_name, tables=table_names)
|
||||
|
||||
except Exception as create_error:
|
||||
logger.error(f"Error during table creation within transaction: {create_error}",
|
||||
service=self.service_name)
|
||||
# Re-raise to trigger transaction rollback
|
||||
raise
|
||||
|
||||
logger.info("Database tables created successfully", service=self.service_name)
|
||||
|
||||
except Exception as e:
|
||||
# Check if it's a "relation already exists" error which can be safely ignored
|
||||
error_str = str(e).lower()
|
||||
@@ -164,6 +185,14 @@ class DatabaseManager:
|
||||
logger.info("Database tables creation completed (some already existed)", service=self.service_name)
|
||||
else:
|
||||
logger.error(f"Failed to create tables: {e}", service=self.service_name)
|
||||
|
||||
# Check for specific transaction error indicators
|
||||
if any(indicator in error_str for indicator in [
|
||||
"transaction", "rollback", "aborted", "failed sql transaction"
|
||||
]):
|
||||
logger.error("Transaction-related error detected during table creation",
|
||||
service=self.service_name)
|
||||
|
||||
raise DatabaseError(f"Table creation failed: {str(e)}")
|
||||
|
||||
async def drop_tables(self, metadata=None):
|
||||
|
||||
Reference in New Issue
Block a user