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