Skip to content

feat: add support for asyncpg driver - #390

Merged
jackwotherspoon merged 43 commits into
mainfrom
asyncpg-support
Jul 25, 2022
Merged

feat: add support for asyncpg driver#390
jackwotherspoon merged 43 commits into
mainfrom
asyncpg-support

Conversation

@jackwotherspoon

@jackwotherspoonjackwotherspoon commented Jul 4, 2022

Copy link
Copy Markdown
Collaborator

Todo:

  • Wait for 0.26.0 release of asyncpg
  • Determine how to use creator argument or similar for SQLAlchemycreate_async_engine
  • Update README with asyncpg connection example

Closes#218

Comment threadgoogle/cloud/sql/connector/asyncpg.py Outdated
@jackwotherspoonjackwotherspoon changed the title WIP: add support for asyncpg driverfeat: add support for asyncpg driverJul 13, 2022
Comment threadREADME.md Outdated
Comment threadREADME.md Outdated
Comment threadREADME.md Outdated
Comment threadREADME.md
connector = await create_async_connector()

# create connection to Cloud SQL database
conn: asyncpg.Connection = await connector.connect_async(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: this connection should probably use an async with

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

the connector or connection itself? async with create_async_connector() or async with connector.connect_async(...) ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The connection itself.

Do we support async enters and exit with the Connector?

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

not currently, we don't have an __aenter__ or __aexit__ implemented.

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

I added the ability to create an async context manager with Connector and added example to README. I can't use async with for the connection because the asyncpg.Connection class does not have an __aenter__ or __aexit__. We could potentially create a Connection wrapper class of our own to implement an async context manager for the database connection. (Example). However, if in a future PR we are going to support connection pools (either through SQLAlchemy or native asyncpg.Pool then they will have context manager built in so not sure it is worth it to create the wrapper class.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, this is surprising to me that they don't support those two functions. I think it's fine to leave for now (maybe worth asking why they haven't?)

Copy link
Copy Markdown
CollaboratorAuthor

Choose a reason for hiding this comment

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

Seems like there is an open issue for it actually, if I have some spare time I will try and put up another PR to asyncpg and add the two functions.

Comment threadtests/unit/mocks.py Outdated
Comment threadtests/conftest.py
Comment threadtests/conftest.py
Comment threadREADME.md
connector = await create_async_connector()

# create connection to Cloud SQL database
conn: asyncpg.Connection = await connector.connect_async(

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ok, this is surprising to me that they don't support those two functions. I think it's fine to leave for now (maybe worth asking why they haven't?)

Comment threadtests/conftest.py Outdated
Comment threadtests/conftest.py
@jackwotherspoon
jackwotherspoon merged commit 3170b1f into mainJul 25, 2022
@jackwotherspoon
jackwotherspoon deleted the asyncpg-support branch July 25, 2022 14:36
@github-actionsgithub-actionsBot mentioned this pull request Jul 25, 2022
Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for asyncpg driver.

2 participants

@jackwotherspoon@kurtisvg