Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Dialect: Add support forasyncpg andpsycopg3 drivers#11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Draft
amotl wants to merge1 commit intomain
base:main
Choose a base branch
Loading
fromamo/postgresql-async

Conversation

@amotl
Copy link
Member

@amotlamotl commentedDec 23, 2023
edited
Loading

About

This patch adds support forasyncpg andpsycopg3 drivers, by introducing thecrate+asyncpg:// andcrate+psycopg:// dialect identifiers.

It also adds thecrate+urllib3:// dialect identifier, for explicitly addressing theurllib3-based transport, in case there might be other HTTP-based transport adapters in the future, using libraries likeaiohttp,httpx, orniquests.1

Notes

  • The asynchronous variant ofpsycopg is also supported and will be automatically selected when usingcreate_async_engine() instead ofcreate_engine(), so it doesn't have a dedicated dialect identifier.

  • All of this will only work with SQLAlchemy 2.x.

Installation

pip install 'sqlalchemy-cratedb[all] @ git+https://github.com/crate/sqlalchemy-cratedb@amo/postgresql-async'

References

Backlog

Footnotes

  1. Picked up from another discussion athttps://github.com/panodata/grafana-client/issues/134.

@amotlamotlforce-pushed theamo/postgresql-async branch 2 times, most recently from20ab16e toe1929cbCompareDecember 23, 2023 01:36
Comment on lines 24 to 43
deftest_engine_sync_vanilla():
"""
crate:// -- Verify connectivity and data transport with vanilla HTTP-based driver.
"""
engine=sa.create_engine("crate://crate@localhost:4200/",echo=True)
assertisinstance(engine,sa.engine.Engine)
withengine.connect()asconnection:
result=connection.execute(QUERY)
assertresult.mappings().fetchone()== {'mountain':'Acherkogel','coordinates': [10.95667,47.18917]}


deftest_engine_sync_urllib3():
"""
crate+urllib3:// -- Verify connectivity and data transport *explicitly* selecting the HTTP driver.
"""
engine=sa.create_engine("crate+urllib3://crate@localhost:4200/",isolation_level="AUTOCOMMIT",echo=True)
assertisinstance(engine,sa.engine.Engine)
withengine.connect()asconnection:
result=connection.execute(QUERY)
assertresult.mappings().fetchone()== {'mountain':'Acherkogel','coordinates': [10.95667,47.18917]}
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is the reference record representation.

{'mountain':'Acherkogel','coordinates': [10.95667,47.18917]}

Comment on lines 46 to 67
deftest_engine_sync_psycopg():
"""
crate+psycopg:// -- Verify connectivity and data transport using the psycopg driver (version 3).
"""
engine=sa.create_engine("crate+psycopg://crate@localhost:5432/",isolation_level="AUTOCOMMIT",echo=True)
assertisinstance(engine,sa.engine.Engine)
withengine.connect()asconnection:
result=connection.execute(QUERY)
assertresult.mappings().fetchone()== {'mountain':'Acherkogel','coordinates':'(10.95667,47.18917)'}


@pytest.mark.asyncio
asyncdeftest_engine_async_psycopg():
"""
crate+psycopg:// -- Verify connectivity and data transport using the psycopg driver (version 3).
This time, in asynchronous mode.
"""
engine=create_async_engine("crate+psycopg://crate@localhost:5432/",isolation_level="AUTOCOMMIT",echo=True)
assertisinstance(engine,AsyncEngine)
asyncwithengine.begin()asconn:
result=awaitconn.execute(QUERY)
assertresult.mappings().fetchone()== {'mountain':'Acherkogel','coordinates':'(10.95667,47.18917)'}
Copy link
MemberAuthor

@amotlamotlDec 23, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

When usingpsycopg, there are deviations, which clearly need to be addressed. By chance, you might have seen this already,@SStorm? Or did you only use asyncpg?

{'mountain':'Acherkogel','coordinates':'(10.95667,47.18917)'}

Comment on lines 70 to 81
@pytest.mark.asyncio
asyncdeftest_engine_async_asyncpg():
"""
crate+asyncpg:// -- Verify connectivity and data transport using the asyncpg driver.
This exclusively uses asynchronous mode.
"""
fromasyncpg.pgproto.typesimportPoint
engine=create_async_engine("crate+asyncpg://crate@localhost:5432/",isolation_level="AUTOCOMMIT",echo=True)
assertisinstance(engine,AsyncEngine)
asyncwithengine.begin()asconn:
result=awaitconn.execute(QUERY)
assertresult.mappings().fetchone()== {'mountain':'Acherkogel','coordinates':Point(10.95667,47.18917)}
Copy link
MemberAuthor

@amotlamotlDec 23, 2023
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

When usingasyncpg, the response record is not wrong, but its shape is different, as it provides a dedicatedPoint type for representing coordinates.

{'mountain': 'Acherkogel', 'coordinates': Point(10.95667, 47.18917)}

@amotlamotl changed the titleAdd support forpsycopg andasyncpg driversAdd support forasyncpg andpsycopg driversDec 23, 2023
@amotlamotl changed the titleAdd support forasyncpg andpsycopg driversAdd support forasyncpg andpsycopg3 driversDec 23, 2023
@amotlamotl requested a review frommatrivDecember 23, 2023 23:33
@amotlamotl mentioned this pull requestJan 12, 2024
2 tasks
@amotlamotl changed the titleAdd support forasyncpg andpsycopg3 driversDialect: Add support forasyncpg andpsycopg3 driversJan 15, 2024
@amotlamotlforce-pushed theamo/fix-inspector branch 5 times, most recently from6ac0a22 tod2c7613CompareJune 13, 2024 14:33
Base automatically changed fromamo/fix-inspector tomainJune 13, 2024 14:38
@amotlamotlforce-pushed theamo/postgresql-async branch 3 times, most recently from5c906ad to474f658CompareJune 14, 2024 02:20
@amotlamotlforce-pushed theamo/postgresql-async branch from474f658 todcb6708CompareJune 24, 2024 14:34
Comment on lines +403 to +436
When using the PostgreSQL protocol with drivers `psycopg` or `asyncpg`,
the paramstyle is not `qmark`, but `pyformat`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I am not quite following. I assume we would want to use server side binding (i.e. qmark)?

Copy link
MemberAuthor

@amotlamotlJun 25, 2024
edited
Loading

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This is probably the reason for needing the workaround at all: Because PostgreSQL drivers psycopg and asyncpg, or the SA dialect, usepyformat, but CrateDB usesqmark, we may need to adjust, iirc.

At least, the patch in its current shape needs it. Maybe there are alternatives to implement it, possibly even easier ones. We will be happy to learn about them.

@amotlamotlforce-pushed theamo/postgresql-async branch fromdcb6708 toe8bfd77CompareJune 25, 2024 14:50
This introduces the `crate+psycopg://`, `crate+asyncpg://`, and`crate+urllib3://` dialect identifiers. The asynchronous variant of`psycopg` is also supported.
@amotlamotlforce-pushed theamo/postgresql-async branch from5953f14 to48e6cacCompareMarch 30, 2025 01:50
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@SStormSStormAwaiting requested review from SStorm

@suristersuristerAwaiting requested review from surister

@matrivmatrivAwaiting requested review from matriv

1 more reviewer

@proddataproddataproddata left review comments

Reviewers whose approvals may not affect merge requirements

Assignees

No one assigned

Labels

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@amotl@proddata

[8]ページ先頭

©2009-2025 Movatter.jp