Skip to content

Commit c93db0e

Browse files
fix(test): execute migration scripts via raw asyncpg connection
test_forward_then_rollback_restores_baseline reached a live Postgres for the first time after the MissingGreenlet fix, surfacing a second real bug: SQLAlchemy's asyncpg dialect always executes through prepared statements, and Postgres's extended query protocol rejects multiple SQL commands in a single prepared statement -- every migration file here is multi-statement DDL. conn.execute(text(sql)) failed with "PostgresSyntaxError: cannot insert multiple commands into a prepared statement" for every migration, then cascaded into unrelated test failures (event-loop-closed noise) later in the same CI run. Added _exec_script(), which runs the script through conn.get_raw_connection().driver_connection.execute(sql) -- the raw asyncpg connection's simple query protocol, which has no such restriction. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
1 parent 7516eb0 commit c93db0e

1 file changed

Lines changed: 14 additions & 5 deletions

File tree

‎db/tests/test_schema.py‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -342,18 +342,27 @@ async def test_forward_then_rollback_restores_baseline() -> None:
342342
``asyncpg`` being the project's only Postgres driver dependency), so this
343343
uses ``create_async_engine`` rather than the sync ``create_engine`` -
344344
mixing a sync engine with an async-only driver raises
345-
``sqlalchemy.exc.MissingGreenlet``.
345+
``sqlalchemy.exc.MissingGreenlet``. Each migration script is applied via
346+
the raw asyncpg connection rather than ``conn.execute(text(...))``:
347+
SQLAlchemy's asyncpg dialect always prepares statements, and Postgres's
348+
extended query protocol rejects multiple commands in one prepared
349+
statement, which every multi-statement migration file here is.
346350
"""
347-
from sqlalchemy import inspect, text
348-
from sqlalchemy.ext.asyncio import create_async_engine
351+
from sqlalchemy import inspect
352+
from sqlalchemy.ext.asyncio import AsyncConnection, create_async_engine
353+
354+
async def _exec_script(conn: AsyncConnection, sql: str) -> None:
355+
"""Run a (possibly multi-statement) SQL script via the raw driver connection."""
356+
raw_connection = await conn.get_raw_connection()
357+
await raw_connection.driver_connection.execute(sql)
349358

350359
engine = create_async_engine(os.environ["DATABASE_URL"])
351360
try:
352361
# Apply all migrations in order (001 -> 002 -> 003 -> 004).
353362
for migration_path in _ALL_FORWARD_MIGRATIONS:
354363
migration_sql = _read(migration_path)
355364
async with engine.begin() as conn:
356-
await conn.execute(text(migration_sql))
365+
await _exec_script(conn, migration_sql)
357366
async with engine.connect() as conn:
358367
has_documents = await conn.run_sync(
359368
lambda sync_conn: inspect(sync_conn).has_table("documents")
@@ -366,7 +375,7 @@ async def test_forward_then_rollback_restores_baseline() -> None:
366375
# Roll back 001 (which cascades to drop all dependent objects).
367376
down = _read(_DOWN_SQL)
368377
async with engine.begin() as conn:
369-
await conn.execute(text(down))
378+
await _exec_script(conn, down)
370379
async with engine.connect() as conn:
371380
sections_remain = await conn.run_sync(
372381
lambda sync_conn: inspect(sync_conn).has_table("sections")

0 commit comments

Comments
 (0)