Commit e4697cf
committed
Make cursor-based adapter compatible with psycopg v3 transactions and multi-statement
Running tests/integration/test_transactions.py against a real PostgreSQL
container with the psycopg3 adapter surfaced two driver-behavior
differences that need handling in the shared CursorBasedAdapter:
1. autocommit-aware execute_non_query
psycopg2 silently makes conn.commit() a no-op when conn.autocommit is
True; psycopg v3 actively COMMITs any open transaction. The adapter's
execute_non_query unconditionally called conn.commit(), which under
psycopg3 meant `executor.execute("BEGIN")` immediately ended the
transaction it just started — so the subsequent INSERT was committed
and ROLLBACK had nothing to undo. Skipping the commit when
conn.autocommit is True preserves the original intent on both
drivers.
2. nextset() walk in execute_query
psycopg v3 exposes each result set in a multi-statement string and
leaves the cursor positioned at the first (e.g. on BEGIN, which has
no columns). psycopg2 only ever surfaced the last set. Walking
cursor.nextset() until it returns falsy lands on the last set; the
loop is a no-op on drivers that don't implement nextset() (a number
raise NotSupportedError, hence the try/except).
The integration tests in tests/integration/test_transactions.py needed
three fixes that are independent of the driver swap — verified to fail
or hang on upstream main with psycopg2 as well:
- test_multi_statement_as_single_query_works
The "BEGIN; INSERT; SELECT" payload left an open transaction on the
executor's persistent connection; the test's subsequent DROP TABLE
(on a different connection) then blocked waiting for the lock. Added
reset_tui_executor() before the cleanup so the persistent connection
rolls back and releases the lock.
- test_atomic_execute_rolls_back_on_error
The test expected atomic_execute to raise on a failing statement.
Per its docstring and current implementation, atomic_execute reports
the failure via a MultiStatementResult with completed=False and the
failing index in error_index. Updated the assertions accordingly.
- test_atomic_execute_commits_on_success
Multi-statement atomic_execute is documented to return a
MultiStatementResult (one StatementResult per statement), not a
single QueryResult. Asserting on result.results[-1].result picks up
the trailing SELECT's QueryResult exactly as the test intended.
Result against the docker-compose postgres:16 fixture:
- tests/integration/test_transactions.py: 13 passed
- tests/integration/test_database_browsing_flow.py::TestPostgreSQLDatabaseBrowsing: 1 passed
- tests/integration/test_stale_connection_reconnect.py -k postgres: 5 passed, 1 idle-variant skipped (pre-existing)
- tests/unit: 842 passed1 parent a1758e1 commit e4697cf
3 files changed
Lines changed: 49 additions & 11 deletions
File tree
- sqlit/domains/connections/providers/adapters
- tests
- integration
- unit
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
492 | 492 | | |
493 | 493 | | |
494 | 494 | | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
495 | 508 | | |
496 | 509 | | |
497 | 510 | | |
| |||
511 | 524 | | |
512 | 525 | | |
513 | 526 | | |
514 | | - | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
515 | 535 | | |
516 | 536 | | |
517 | 537 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
300 | 300 | | |
301 | 301 | | |
302 | 302 | | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
303 | 308 | | |
304 | 309 | | |
305 | 310 | | |
| |||
509 | 514 | | |
510 | 515 | | |
511 | 516 | | |
512 | | - | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
513 | 522 | | |
514 | 523 | | |
515 | 524 | | |
516 | 525 | | |
517 | | - | |
518 | | - | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
519 | 530 | | |
520 | 531 | | |
521 | 532 | | |
| |||
556 | 567 | | |
557 | 568 | | |
558 | 569 | | |
| 570 | + | |
| 571 | + | |
559 | 572 | | |
560 | 573 | | |
561 | 574 | | |
562 | 575 | | |
563 | 576 | | |
564 | 577 | | |
565 | 578 | | |
566 | | - | |
567 | | - | |
568 | | - | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
569 | 587 | | |
570 | 588 | | |
571 | 589 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
48 | 48 | | |
49 | 49 | | |
50 | 50 | | |
51 | | - | |
52 | | - | |
| 51 | + | |
| 52 | + | |
53 | 53 | | |
54 | 54 | | |
55 | 55 | | |
| |||
65 | 65 | | |
66 | 66 | | |
67 | 67 | | |
68 | | - | |
| 68 | + | |
69 | 69 | | |
70 | | - | |
| 70 | + | |
71 | 71 | | |
72 | 72 | | |
73 | 73 | | |
| |||
0 commit comments