Skip to content

Commit fb3994f

Browse files
committed
DB Fix
1 parent 5e5bece commit fb3994f

4 files changed

Lines changed: 36 additions & 6 deletions

File tree

backend/src/platform/api/middleware.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import logging
44
import time
55

6+
from starlette.exceptions import HTTPException
67
from starlette.middleware.base import BaseHTTPMiddleware
78
from starlette.requests import Request
89
from starlette.responses import Response, JSONResponse
@@ -61,6 +62,8 @@ async def dispatch(self, request: Request, call_next) -> Response:
6162
{"detail": str(exc)},
6263
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
6364
)
65+
except HTTPException:
66+
raise # Let Starlette handle route-level HTTP errors (e.g. 404)
6467
except Exception:
6568
logger.exception("Unhandled exception in PlatformMiddleware")
6669
return JSONResponse(

backend/src/platform/api/routes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,11 @@ async def init_environment(request: Request) -> JSONResponse:
407407
logger.warning("Unauthorized template access in init_environment")
408408
return unauthorized()
409409
except ValueError as e:
410-
logger.warning(f"Template resolution failed in init_environment: {e}")
410+
logger.warning(
411+
f"Template resolution failed in init_environment: {e} "
412+
f"(service={body.templateService!r}, name={body.templateName!r}, "
413+
f"testId={body.testId!r}, schema={body.templateSchema!r})"
414+
)
411415
return bad_request(str(e))
412416

413417
if not body.testId and not body.impersonateUserId and not body.impersonateEmail:

backend/src/platform/isolationEngine/environment.py

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,35 @@ def migrate_schema(self, template_schema: str, target_schema: str) -> None:
5555
self._set_replica_identity(target_schema)
5656

5757
def _ensure_box_columns(self, schema: str) -> None:
58-
"""Add columns that may be missing from older template snapshots."""
58+
"""Add columns that may be missing from older template snapshots.
59+
60+
Only runs against schemas that actually contain Box tables. Each
61+
ALTER TABLE is wrapped in a SAVEPOINT so that a single failure does
62+
not poison the surrounding transaction (the same pattern used by
63+
``_copy_custom_indexes``).
64+
"""
5965
_columns = [
6066
# (table, column, SQL type, default)
6167
("box_folders", "path", "VARCHAR(500)", "'/'"),
6268
("box_files", "path", "VARCHAR(500)", "'/0/'"),
6369
]
6470
with self.session_manager.base_engine.begin() as conn:
71+
# Quick check: skip entirely when the schema has no Box tables.
72+
has_box = conn.execute(
73+
text(
74+
"SELECT EXISTS("
75+
" SELECT 1 FROM information_schema.tables"
76+
" WHERE table_schema = :schema"
77+
" AND table_name = 'box_folders'"
78+
")"
79+
),
80+
{"schema": schema},
81+
).scalar()
82+
if not has_box:
83+
return
84+
6585
for table, col, sql_type, default in _columns:
86+
nested = conn.begin_nested()
6687
try:
6788
conn.execute(
6889
text(
@@ -71,7 +92,9 @@ def _ensure_box_columns(self, schema: str) -> None:
7192
f"DEFAULT {default}"
7293
)
7394
)
95+
nested.commit()
7496
except Exception as exc:
97+
nested.rollback()
7598
logger.warning(
7699
f"Could not ensure column {schema}.{table}.{col}: {exc}"
77100
)

backend/src/services/calendar/database/operations.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2003,7 +2003,7 @@ def _extract_time(text_value: str) -> tuple[int, int] | None:
20032003
else:
20042004
parsed_dt = parsed_dt.astimezone(tzinfo)
20052005
start_dt = parsed_dt
2006-
except (ValueError, TypeError):
2006+
except ValueError, TypeError:
20072007
start_dt = now_local.replace(minute=0, second=0, microsecond=0) + timedelta(
20082008
hours=1
20092009
)
@@ -2570,7 +2570,7 @@ def query_free_busy(
25702570
if time_zone:
25712571
try:
25722572
target_tz = ZoneInfo(time_zone)
2573-
except (KeyError, ValueError):
2573+
except KeyError, ValueError:
25742574
# Invalid timezone - fall back to UTC
25752575
pass
25762576

@@ -2726,7 +2726,7 @@ def query_free_busy(
27262726
try:
27272727
event_tz = ZoneInfo(event_tz_name)
27282728
start_dt = start_dt.replace(tzinfo=event_tz)
2729-
except (KeyError, ValueError):
2729+
except KeyError, ValueError:
27302730
start_dt = start_dt.replace(tzinfo=dt_timezone.utc)
27312731
elif start_dt.tzinfo is None:
27322732
start_dt = start_dt.replace(tzinfo=dt_timezone.utc)
@@ -2739,7 +2739,7 @@ def query_free_busy(
27392739
try:
27402740
end_tz = ZoneInfo(end_tz_name)
27412741
end_dt = end_dt.replace(tzinfo=end_tz)
2742-
except (KeyError, ValueError):
2742+
except KeyError, ValueError:
27432743
end_dt = end_dt.replace(tzinfo=dt_timezone.utc)
27442744
elif end_dt.tzinfo is None:
27452745
end_dt = end_dt.replace(tzinfo=dt_timezone.utc)

0 commit comments

Comments
 (0)