GoogleCloudPlatform / alloydb-python-connector

A Python library for connecting securely to your AlloyDB instances.
Apache License 2.0
25 stars 6 forks source link

ValueError: Driver 'asyncpg' is not a supported database driver #305

Closed wolfthom closed 2 months ago

wolfthom commented 2 months ago

Bug Description

When I try to connect to alloydb using the python connector and asyncpg as database driver, I get the following error:

ValueError: Driver 'asyncpg' is not a supported database driver

Command used to install alloydb-python-connector: pip install alloydb-python-connector["asyncpg"]

Example code (or command)

import asyncio

from google.cloud.alloydb.connector import asyncpg, connector, IPTypes
import asyncpg

async def getconn() -> asyncpg.Connection:
    conn: asyncpg.Connection = await connector.Connector(
        ip_type=IPTypes.PUBLIC,enable_iam_auth=True).connect(
        "my-connection-url",
        "asyncpg",
        user=user,
        password=password,
        db="postgres",

        # use enable_iam_auth to enable IAM authentication
        enable_iam_auth=True
    )
    return conn

asyncio.run(getconn())

Stacktrace

File "/Users/xy/Library/Caches/pypoetry/virtualenvs/alloydb-importer-vP5KE6S_-py3.11/lib/python3.11/site-packages/google/cloud/alloydb/connector/connector.py", line 168, in connect_async
    raise ValueError(f"Driver '{driver}' is not a supported database driver.")
ValueError: Driver 'asyncpg' is not a supported database driver.

Steps to reproduce?

  1. ?
  2. ?
  3. ? ...

Environment

  1. OS type and version: MacOS X, M2
  2. Python version: 3.11
  3. AlloyDB Python Connector version: 1.0.0

Additional Details

No response

jackwotherspoon commented 2 months ago

The async interface of the Cloud SQL Python Connector is through the AsyncConnector class as outlined in our README's Async Driver Usage section. Using the synchronous Connector interface on the async driver will result in the error you are seeing.


import asyncio
import asyncpg

import sqlalchemy
from sqlalchemy.ext.asyncio import AsyncEngine, create_async_engine

from google.cloud.alloydb.connector import AsyncConnector

async def init_connection_pool(connector: AsyncConnector) -> AsyncEngine:
    # initialize Connector object for connections to AlloyDB
    async def getconn() -> asyncpg.Connection:
            conn: asyncpg.Connection = await connector.connect(
                "projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>",
                "asyncpg",
                user="my-user",
                password="my-password",
                db="my-db-name"
                # ... additional database driver args
            )
            return conn

    # The AlloyDB Python Connector can be used along with SQLAlchemy using the
    # 'async_creator' argument to 'create_async_engine'
    pool = create_async_engine(
        "postgresql+asyncpg://",
        async_creator=getconn,
    )
    return pool

async def main():
    # initialize Connector object for connections to AlloyDB
    async with AsyncConnector() as connector:
        # initialize connection pool
        pool = await init_connection_pool(connector)

        # example query
        async with pool.connect() as conn:
            await conn.execute(sqlalchemy.text("SELECT NOW()"))

        # dispose of connection pool
        await pool.dispose()```