|
| 1 | +# Copyright 2024 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +from typing import Tuple |
| 17 | + |
| 18 | +import asyncpg |
| 19 | +import pytest |
| 20 | +import sqlalchemy |
| 21 | +import sqlalchemy.ext.asyncio |
| 22 | + |
| 23 | +from google.cloud.alloydb.connector import AsyncConnector |
| 24 | + |
| 25 | + |
| 26 | +async def create_sqlalchemy_engine( |
| 27 | + inst_uri: str, |
| 28 | + user: str, |
| 29 | + password: str, |
| 30 | + db: str, |
| 31 | +) -> Tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]: |
| 32 | + """Creates a connection pool for an AlloyDB instance and returns the pool |
| 33 | + and the connector. Callers are responsible for closing the pool and the |
| 34 | + connector. |
| 35 | +
|
| 36 | + A sample invocation looks like: |
| 37 | +
|
| 38 | + engine, connector = await create_sqlalchemy_engine( |
| 39 | + inst_uri, |
| 40 | + user, |
| 41 | + password, |
| 42 | + db, |
| 43 | + ) |
| 44 | + async with engine.connect() as conn: |
| 45 | + time = await conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() |
| 46 | + curr_time = time[0] |
| 47 | + # do something with query result |
| 48 | + await connector.close() |
| 49 | +
|
| 50 | + Args: |
| 51 | + instance_uri (str): |
| 52 | + The instance URI specifies the instance relative to the project, |
| 53 | + region, and cluster. For example: |
| 54 | + "projects/my-project/locations/us-central1/clusters/my-cluster/instances/my-instance" |
| 55 | + user (str): |
| 56 | + The database user name, e.g., postgres |
| 57 | + password (str): |
| 58 | + The database user's password, e.g., secret-password |
| 59 | + db_name (str): |
| 60 | + The name of the database, e.g., mydb |
| 61 | + """ |
| 62 | + connector = AsyncConnector() |
| 63 | + |
| 64 | + async def getconn() -> asyncpg.Connection: |
| 65 | + conn: asyncpg.Connection = await connector.connect( |
| 66 | + inst_uri, |
| 67 | + "asyncpg", |
| 68 | + user=user, |
| 69 | + password=password, |
| 70 | + db=db, |
| 71 | + ip_type="PSC", |
| 72 | + ) |
| 73 | + return conn |
| 74 | + |
| 75 | + # create SQLAlchemy connection pool |
| 76 | + engine = sqlalchemy.ext.asyncio.create_async_engine( |
| 77 | + "postgresql+asyncpg://", |
| 78 | + async_creator=getconn, |
| 79 | + execution_options={"isolation_level": "AUTOCOMMIT"}, |
| 80 | + ) |
| 81 | + return engine, connector |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.asyncio |
| 85 | +async def test_connection_with_asyncpg() -> None: |
| 86 | + """Basic test to get time from database.""" |
| 87 | + inst_uri = os.environ["ALLOYDB_PSC_INSTANCE_URI"] |
| 88 | + user = os.environ["ALLOYDB_USER"] |
| 89 | + password = os.environ["ALLOYDB_PASS"] |
| 90 | + db = os.environ["ALLOYDB_DB"] |
| 91 | + |
| 92 | + pool, connector = await create_sqlalchemy_engine(inst_uri, user, password, db) |
| 93 | + |
| 94 | + async with pool.connect() as conn: |
| 95 | + res = (await conn.execute(sqlalchemy.text("SELECT 1"))).fetchone() |
| 96 | + assert res[0] == 1 |
| 97 | + |
| 98 | + await connector.close() |
0 commit comments