Better Ergonomics For Psycopg2#36
Merged
Merged
Conversation
Coverage Report Results
1 empty file skipped. |
Prior to this change, this package was relying on the default behaviour
of Psycopg for cursor instantiation.
The problem is that there are differences between Psycopg 2 and Psycopg
3 that make this unfeasible for this library, which supports both
versions.
For example, when a cursor is instantiated from the connection context
manager, Psycopg 2 starts a transaction automatically [1][2], whereas
Psycopg 3 doesn't.
with connection.cursor() as cur:
# There is already an implicit "BEGIN;" here on Psycopg 2.
# But not on Psycopg 3
...
This problem came up when testing Psycopack locally using Psycopg 2.
The following ABORT call had to be added or else the procedure would
fail when creating indexes CONCURRENTLY, as that can't happen inside a
transaction.
from psycopack import psycopack, get_db_connection
with get_db_connection("postgresql://user:password@host:port/db") as conn:
with conn.cursor() as cur:
cur.execute("ABORT;")
psycopack = Psycopack(
batch_size=50_000,
conn=conn,
cur=cur,
schema="public",
table="to_psycopack",
# Set this argument and Psycopack will do the conversion
# automatically.
convert_pk_to_bigint=True,
)
psycopack.sync_schemas()
In any case, Psycopack handles its own transactions and does not need to
rely on arbitrary library designs.
- [1] psycopg/psycopg2#941 (comment)
- [2] psycopg/psycopg2#1305 (comment)
3465b07 to
26b812a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Prior to this change, this package was relying on the default behaviour of Psycopg for cursor instantiation.
The problem is that there are differences between Psycopg 2 and Psycopg 3 that make this unfeasible for this library, which supports both versions.
For example, when a cursor is instantiated from the connection context manager, Psycopg 2 starts a transaction automatically [1][2], whereas Psycopg 3 doesn't.
This problem came up when testing Psycopack locally using Psycopg 2.
The following ABORT call had to be added or else the procedure would fail when creating indexes CONCURRENTLY, as that can't happen inside a transaction.
In any case, Psycopack handles its own transactions and does not need to rely on arbitrary library designs.