Commit fd86688
fix(cleanup): rewrite log-cleanup UNION as function-form for SQLAlchemy 2.x
Sentry has been firing this nightly for weeks (alert
OPENSENTRY-COMMAND-1, surfaced in this morning's email digest):
AttributeError: 'CompoundSelect' object has no attribute 'union'
File "app/main.py", line 244, in _log_cleanup_loop
.union(select([AuditLog.org_id]).distinct())
Root cause: the cleanup loop in ``app.main._log_cleanup_loop`` chained
``.union(...)`` calls across the five log tables to collect distinct
org_ids:
select(StreamAccessLog.org_id).distinct()
.union(select(McpActivityLog.org_id).distinct())
.union(select(AuditLog.org_id).distinct()) # <- raises here
...
In SQLAlchemy 2.x (``pyproject.toml`` pins ``>=2.0.48``) the FIRST
``.union()`` returns a ``CompoundSelect``, which doesn't expose a
``.union()`` method — only ``Select`` does. The 1.x-era pattern of
chaining unions was deprecated in 1.4 and removed in 2.0. The chained
form had been silently broken since Tigris was removed and this loop
landed; the cleanup body sat inside a ``try/except`` that logged
"Cleanup failed" and moved on, so per-org log retention (Free 30d /
Pro 90d / Pro Plus 365d) wasn't actually getting applied — the
backend's audit / motion / notification / MCP-activity / stream-access
log tables were growing unbounded.
The fix uses the function form ``union(a, b, c, ...)`` which is the
SQLAlchemy 2.x-compatible composition (and also reads more cleanly
than a chain). The ``union`` symbol was already imported at line 301,
so the change is a one-block rewrite.
Tests
-----
``tests/test_log_cleanup_union.py`` (3 cases):
- ``test_union_query_executes_without_attributeerror`` — pins the
failure mode by building + executing the query against an empty
DB. If a future refactor reintroduces the chained form, this
fails at execute time, not silently inside the loop's try/except.
- ``test_union_query_returns_distinct_org_ids_across_log_tables``
seeds rows for two orgs across multiple log tables and asserts
the deduped set comes back as expected (smoke for the cleanup's
first pass).
- ``test_union_query_handles_empty_tables`` covers fresh-install
day-one.
Suite: 235 → 238 passing.
Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>1 parent 6fb5fcb commit fd86688
2 files changed
Lines changed: 145 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
298 | 298 | | |
299 | 299 | | |
300 | 300 | | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
301 | 309 | | |
302 | 310 | | |
303 | 311 | | |
304 | | - | |
305 | | - | |
306 | | - | |
307 | | - | |
308 | | - | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
309 | 319 | | |
310 | 320 | | |
311 | 321 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
0 commit comments