1313# limitations under the License.
1414
1515import os
16- from typing import Tuple
16+ from typing import Any , Tuple
1717
1818# [START alloydb_sqlalchemy_connect_async_connector]
1919import asyncpg
@@ -88,7 +88,65 @@ async def getconn() -> asyncpg.Connection:
8888# [END alloydb_sqlalchemy_connect_async_connector]
8989
9090
91- async def test_connection_with_asyncpg () -> None :
91+ async def create_asyncpg_pool (
92+ instance_connection_name : str ,
93+ user : str ,
94+ password : str ,
95+ db : str ,
96+ refresh_strategy : str = "background" ,
97+ ) -> tuple [asyncpg .Pool , AsyncConnector ]:
98+ """Creates a native asyncpg connection pool for an AlloyDB instance and
99+ returns the pool and the connector. Callers are responsible for closing the
100+ pool and the connector.
101+
102+ A sample invocation looks like:
103+
104+ pool, connector = await create_asyncpg_pool(
105+ inst_conn_name,
106+ user,
107+ password,
108+ db,
109+ )
110+ async with pool.acquire() as conn:
111+ hello = await conn.fetch("SELECT 'Hello World!'")
112+ # do something with query result
113+ await connector.close()
114+
115+ Args:
116+ instance_connection_name (str):
117+ The instance connection name specifies the instance relative to the
118+ project and region. For example: "my-project:my-region:my-instance"
119+ user (str):
120+ The database user name, e.g., postgres
121+ password (str):
122+ The database user's password, e.g., secret-password
123+ db (str):
124+ The name of the database, e.g., mydb
125+ refresh_strategy (Optional[str]):
126+ Refresh strategy for the Cloud SQL Connector. Can be one of "lazy"
127+ or "background". For serverless environments use "lazy" to avoid
128+ errors resulting from CPU being throttled.
129+ """
130+ connector = AsyncConnector (refresh_strategy = refresh_strategy )
131+
132+ async def getconn (
133+ instance_connection_name : str , ** kwargs : Any
134+ ) -> asyncpg .Connection :
135+ conn : asyncpg .Connection = await connector .connect (
136+ instance_connection_name ,
137+ "asyncpg" ,
138+ user = user ,
139+ password = password ,
140+ db = db ,
141+ )
142+ return conn
143+
144+ # create native asyncpg pool (requires asyncpg version >=0.30.0)
145+ pool = await asyncpg .create_pool (instance_connection_name , connect = getconn )
146+ return pool , connector
147+
148+
149+ async def test_sqlalchemy_connection_with_asyncpg () -> None :
92150 """Basic test to get time from database."""
93151 inst_uri = os .environ ["ALLOYDB_INSTANCE_URI" ]
94152 user = os .environ ["ALLOYDB_USER" ]
@@ -104,7 +162,7 @@ async def test_connection_with_asyncpg() -> None:
104162 await connector .close ()
105163
106164
107- async def test_lazy_connection_with_asyncpg () -> None :
165+ async def test_lazy_sqlalchemy_connection_with_asyncpg () -> None :
108166 """Basic test to get time from database."""
109167 inst_uri = os .environ ["ALLOYDB_INSTANCE_URI" ]
110168 user = os .environ ["ALLOYDB_USER" ]
@@ -120,3 +178,35 @@ async def test_lazy_connection_with_asyncpg() -> None:
120178 assert res [0 ] == 1
121179
122180 await connector .close ()
181+
182+
183+ async def test_connection_with_asyncpg () -> None :
184+ """Basic test to get time from database."""
185+ inst_uri = os .environ ["ALLOYDB_INSTANCE_URI" ]
186+ user = os .environ ["ALLOYDB_USER" ]
187+ password = os .environ ["ALLOYDB_PASS" ]
188+ db = os .environ ["ALLOYDB_DB" ]
189+
190+ pool , connector = await create_asyncpg_pool (inst_uri , user , password , db )
191+
192+ async with pool .acquire () as conn :
193+ res = await conn .fetch ("SELECT 1" )
194+ assert res [0 ][0 ] == 1
195+
196+ await connector .close ()
197+
198+
199+ async def test_lazy_connection_with_asyncpg () -> None :
200+ """Basic test to get time from database."""
201+ inst_uri = os .environ ["ALLOYDB_INSTANCE_URI" ]
202+ user = os .environ ["ALLOYDB_USER" ]
203+ password = os .environ ["ALLOYDB_PASS" ]
204+ db = os .environ ["ALLOYDB_DB" ]
205+
206+ pool , connector = await create_asyncpg_pool (inst_uri , user , password , db , "lazy" )
207+
208+ async with pool .acquire () as conn :
209+ res = await conn .fetch ("SELECT 1" )
210+ assert res [0 ][0 ] == 1
211+
212+ await connector .close ()
0 commit comments