>>> with psycopg.connect(autocommit=True) as conn:
... with conn.pipeline() as p, conn.cursor() as cur:
- ... cur.execute("INSERT INTO mytable (data) VALUES (%s)", ["one"])
- ... cur.execute("INSERT INTO no_such_table (data) VALUES (%s)", ["two"])
- ... conn.execute("INSERT INTO mytable (data) VALUES (%s)", ["three"])
- ... p.sync()
+ ... try:
+ ... cur.execute("INSERT INTO mytable (data) VALUES (%s)", ["one"])
+ ... cur.execute("INSERT INTO no_such_table (data) VALUES (%s)", ["two"])
+ ... conn.execute("INSERT INTO mytable (data) VALUES (%s)", ["three"])
+ ... p.sync()
+ ... except psycopg.errors.UndefinedTable:
+ ... pass
... cur.execute("INSERT INTO mytable (data) VALUES (%s)", ["four"])
fails with the error ``relation "no_such_table" does not exist`` and, at the
def _sync_gen(self) -> PQGen[None]:
self._enqueue_sync()
yield from self._communicate_gen()
+ yield from self._fetch_gen(flush=False)
def _exit_gen(self) -> PQGen[None]:
"""Exit current pipeline by sending a Sync and, unless within a nested
pipeline, also fetch back all remaining results.
"""
try:
- yield from self._sync_gen()
+ self._enqueue_sync()
+ yield from self._communicate_gen()
finally:
if self.level == 1:
# No need to force flush since we emitted a sync just before.
conn.commit()
+def test_sync_syncs_results(conn):
+ with conn.pipeline() as p:
+ cur = conn.execute("select 1")
+ assert cur.statusmessage is None
+ p.sync()
+ assert cur.statusmessage == "SELECT 1"
+
+
+def test_sync_syncs_errors(conn):
+ conn.autocommit = True
+ with conn.pipeline() as p:
+ conn.execute("select 1 from nosuchtable")
+ with pytest.raises(e.UndefinedTable):
+ p.sync()
+
+
def test_fetch_no_result(conn):
with conn.pipeline():
cur = conn.cursor()
await aconn.commit()
+async def test_sync_syncs_results(aconn):
+ async with aconn.pipeline() as p:
+ cur = await aconn.execute("select 1")
+ assert cur.statusmessage is None
+ await p.sync()
+ assert cur.statusmessage == "SELECT 1"
+
+
+async def test_sync_syncs_errors(aconn):
+ await aconn.set_autocommit(True)
+ async with aconn.pipeline() as p:
+ await aconn.execute("select 1 from nosuchtable")
+ with pytest.raises(e.UndefinedTable):
+ await p.sync()
+
+
async def test_fetch_no_result(aconn):
async with aconn.pipeline():
cur = aconn.cursor()