fix(ci): filter cleanup tables to only those that exist
Tables like invites/user_tenants only exist on saas-tiers branch. Query pg_tables to skip missing tables in TRUNCATE. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -165,13 +165,24 @@ async def admin_session(admin_engine) -> AsyncGenerator[AsyncSession, None]:
|
|||||||
Cleanup deletes all rows from test tables after the test.
|
Cleanup deletes all rows from test tables after the test.
|
||||||
"""
|
"""
|
||||||
session = AsyncSession(admin_engine, expire_on_commit=False)
|
session = AsyncSession(admin_engine, expire_on_commit=False)
|
||||||
# TRUNCATE CASCADE reliably removes all data regardless of FK order
|
# TRUNCATE CASCADE reliably removes all data regardless of FK order.
|
||||||
tables_csv = ", ".join(_CLEANUP_TABLES)
|
# Filter to only tables that exist (some are saas-tiers only).
|
||||||
|
existing = await session.execute(
|
||||||
|
text(
|
||||||
|
"SELECT tablename FROM pg_tables "
|
||||||
|
"WHERE schemaname = 'public' AND tablename = ANY(:names)"
|
||||||
|
),
|
||||||
|
{"names": _CLEANUP_TABLES},
|
||||||
|
)
|
||||||
|
existing_tables = [row[0] for row in existing.fetchall()]
|
||||||
|
if existing_tables:
|
||||||
|
tables_csv = ", ".join(existing_tables)
|
||||||
await session.execute(text(f"TRUNCATE {tables_csv} CASCADE"))
|
await session.execute(text(f"TRUNCATE {tables_csv} CASCADE"))
|
||||||
await session.commit()
|
await session.commit()
|
||||||
try:
|
try:
|
||||||
yield session
|
yield session
|
||||||
finally:
|
finally:
|
||||||
|
if existing_tables:
|
||||||
await session.execute(text(f"TRUNCATE {tables_csv} CASCADE"))
|
await session.execute(text(f"TRUNCATE {tables_csv} CASCADE"))
|
||||||
await session.commit()
|
await session.commit()
|
||||||
await session.close()
|
await session.close()
|
||||||
|
|||||||
Reference in New Issue
Block a user