Skip to content

Commit 6aba668

Browse files
chore: update tests and README to use lambda over getconn func
1 parent 9dac5b2 commit 6aba668

9 files changed

+62
-126
lines changed

README.md

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ import sqlalchemy
134134
# initialize Connector object
135135
connector = Connector()
136136

137-
# function to return the database connection
138-
def getconn():
139-
conn = connector.connect(
137+
# initialize SQLAlchemy connection pool with Connector
138+
pool = sqlalchemy.create_engine(
139+
"postgresql+pg8000://",
140+
creator=lambda: connector.connect(
140141
"projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>",
141142
"pg8000",
142143
user="my-user",
@@ -145,13 +146,7 @@ def getconn():
145146
# NOTE: this assumes private IP by default.
146147
# Add the following keyword arg to use public IP:
147148
# ip_type="PUBLIC"
148-
)
149-
return conn
150-
151-
# create connection pool
152-
pool = sqlalchemy.create_engine(
153-
"postgresql+pg8000://",
154-
creator=getconn,
149+
),
155150
)
156151
```
157152

@@ -196,30 +191,19 @@ Connector as a context manager:
196191
from google.cloud.alloydb.connector import Connector
197192
import sqlalchemy
198193

199-
# helper function to return SQLAlchemy connection pool
200-
def init_connection_pool(connector: Connector) -> sqlalchemy.engine.Engine:
201-
# function used to generate database connection
202-
def getconn():
203-
conn = connector.connect(
194+
# initialize Connector as context manager
195+
with Connector() as connector:
196+
# initialize SQLAlchemy connection pool with Connector
197+
pool = sqlalchemy.create_engine(
198+
"postgresql+pg8000://",
199+
creator=lambda: connector.connect(
204200
"projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>",
205201
"pg8000",
206202
user="my-user",
207203
password="my-password",
208204
db="my-db-name"
209-
)
210-
return conn
211-
212-
# create connection pool
213-
pool = sqlalchemy.create_engine(
214-
"postgresql+pg8000://",
215-
creator=getconn,
205+
),
216206
)
217-
return pool
218-
219-
# initialize Connector as context manager
220-
with Connector() as connector:
221-
# initialize connection pool
222-
pool = init_connection_pool(connector)
223207
# insert statement
224208
insert_stmt = sqlalchemy.text(
225209
"INSERT INTO my_table (id, title) VALUES (:id, :title)",
@@ -258,30 +242,26 @@ import asyncpg
258242
from google.cloud.alloydb.connector import AsyncConnector
259243

260244
async def main():
261-
# initialize Connector object for connections to AlloyDB
245+
# initialize AsyncConnector object for connections to AlloyDB
262246
connector = AsyncConnector()
263247

264-
# creation function to generate asyncpg connections as the 'connect' arg
265-
async def getconn(instance_connection_name, **kwargs) -> asyncpg.Connection:
266-
return await connector.connect(
248+
# initialize asyncpg connection pool with AsyncConnector
249+
pool = await asyncpg.create_pool(
250+
"projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>",
251+
connect=lambda: connector.connect(
267252
instance_connection_name,
268253
"asyncpg",
269254
user="my-user",
270255
password="my-password",
271256
db="my-db",
272-
)
273-
274-
# initialize connection pool
275-
pool = await asyncpg.create_pool(
276-
"projects/<YOUR_PROJECT>/locations/<YOUR_REGION>/clusters/<YOUR_CLUSTER>/instances/<YOUR_INSTANCE>",
277-
connect=getconn,
257+
),
278258
)
279259

280260
# acquire connection and query AlloyDB database
281261
async with pool.acquire() as conn:
282262
res = await conn.fetch("SELECT NOW()")
283263

284-
# close Connector
264+
# close AsyncConnector
285265
await connector.close()
286266
```
287267

tests/system/test_asyncpg_connection.py

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,16 @@ async def create_sqlalchemy_engine(
6666
"""
6767
connector = AsyncConnector(refresh_strategy=refresh_strategy)
6868

69-
async def getconn() -> asyncpg.Connection:
70-
conn: asyncpg.Connection = await connector.connect(
69+
# create SQLAlchemy connection pool
70+
engine = sqlalchemy.ext.asyncio.create_async_engine(
71+
"postgresql+asyncpg://",
72+
async_creator=lambda: connector.connect(
7173
inst_uri,
7274
"asyncpg",
7375
user=user,
7476
password=password,
7577
db=db,
76-
)
77-
return conn
78-
79-
# create SQLAlchemy connection pool
80-
engine = sqlalchemy.ext.asyncio.create_async_engine(
81-
"postgresql+asyncpg://",
82-
async_creator=getconn,
78+
),
8379
execution_options={"isolation_level": "AUTOCOMMIT"},
8480
)
8581
return engine, connector
@@ -129,20 +125,15 @@ async def create_asyncpg_pool(
129125
"""
130126
connector = AsyncConnector(refresh_strategy=refresh_strategy)
131127

132-
async def getconn(
133-
instance_connection_name: str, **kwargs: Any
134-
) -> asyncpg.Connection:
135-
conn: asyncpg.Connection = await connector.connect(
128+
# create native asyncpg pool (requires asyncpg version >=0.30.0)
129+
pool = await asyncpg.create_pool(instance_connection_name, connect=lambda: connector.connect(
136130
instance_connection_name,
137131
"asyncpg",
138132
user=user,
139133
password=password,
140134
db=db,
141135
)
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)
136+
)
146137
return pool, connector
147138

148139

tests/system/test_asyncpg_iam_authn.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import os
1717

1818
# [START alloydb_sqlalchemy_connect_async_connector_iam_authn]
19-
import asyncpg
2019
import sqlalchemy
2120
import sqlalchemy.ext.asyncio
2221

@@ -61,20 +60,16 @@ async def create_sqlalchemy_engine(
6160
"""
6261
connector = AsyncConnector(refresh_strategy=refresh_strategy)
6362

64-
async def getconn() -> asyncpg.Connection:
65-
conn: asyncpg.Connection = await connector.connect(
63+
# create async SQLAlchemy connection pool
64+
engine = sqlalchemy.ext.asyncio.create_async_engine(
65+
"postgresql+asyncpg://",
66+
async_creator=lambda: connector.connect(
6667
inst_uri,
6768
"asyncpg",
6869
user=user,
6970
db=db,
7071
enable_iam_auth=True,
71-
)
72-
return conn
73-
74-
# create async SQLAlchemy connection pool
75-
engine = sqlalchemy.ext.asyncio.create_async_engine(
76-
"postgresql+asyncpg://",
77-
async_creator=getconn,
72+
),
7873
execution_options={"isolation_level": "AUTOCOMMIT"},
7974
)
8075
return engine, connector

tests/system/test_asyncpg_psc.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414

1515
import os
1616

17-
import asyncpg
1817
import pytest
1918
import sqlalchemy
2019
import sqlalchemy.ext.asyncio
@@ -60,21 +59,17 @@ async def create_sqlalchemy_engine(
6059
"""
6160
connector = AsyncConnector()
6261

63-
async def getconn() -> asyncpg.Connection:
64-
conn: asyncpg.Connection = await connector.connect(
62+
# create SQLAlchemy connection pool
63+
engine = sqlalchemy.ext.asyncio.create_async_engine(
64+
"postgresql+asyncpg://",
65+
async_creator=lambda: connector.connect(
6566
inst_uri,
6667
"asyncpg",
6768
user=user,
6869
password=password,
6970
db=db,
7071
ip_type="PSC",
71-
)
72-
return conn
73-
74-
# create SQLAlchemy connection pool
75-
engine = sqlalchemy.ext.asyncio.create_async_engine(
76-
"postgresql+asyncpg://",
77-
async_creator=getconn,
72+
),
7873
execution_options={"isolation_level": "AUTOCOMMIT"},
7974
)
8075
return engine, connector

tests/system/test_asyncpg_public_ip.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
import os
1616

1717
# [START alloydb_sqlalchemy_connect_async_connector_public_ip]
18-
import asyncpg
1918
import pytest
2019
import sqlalchemy
2120
import sqlalchemy.ext.asyncio
@@ -61,21 +60,17 @@ async def create_sqlalchemy_engine(
6160
"""
6261
connector = AsyncConnector()
6362

64-
async def getconn() -> asyncpg.Connection:
65-
conn: asyncpg.Connection = await connector.connect(
63+
# create SQLAlchemy connection pool
64+
engine = sqlalchemy.ext.asyncio.create_async_engine(
65+
"postgresql+asyncpg://",
66+
async_creator=lambda: connector.connect(
6667
inst_uri,
6768
"asyncpg",
6869
user=user,
6970
password=password,
7071
db=db,
7172
ip_type="PUBLIC",
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,
73+
),
7974
execution_options={"isolation_level": "AUTOCOMMIT"},
8075
)
8176
return engine, connector

tests/system/test_pg8000_connection.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import os
1717

1818
# [START alloydb_sqlalchemy_connect_connector]
19-
import pg8000
2019
import sqlalchemy
2120

2221
from google.cloud.alloydb.connector import Connector
@@ -66,20 +65,16 @@ def create_sqlalchemy_engine(
6665
"""
6766
connector = Connector(refresh_strategy=refresh_strategy)
6867

69-
def getconn() -> pg8000.dbapi.Connection:
70-
conn: pg8000.dbapi.Connection = connector.connect(
68+
# create SQLAlchemy connection pool
69+
engine = sqlalchemy.create_engine(
70+
"postgresql+pg8000://",
71+
creator=lambda: connector.connect(
7172
inst_uri,
7273
"pg8000",
7374
user=user,
7475
password=password,
7576
db=db,
76-
)
77-
return conn
78-
79-
# create SQLAlchemy connection pool
80-
engine = sqlalchemy.create_engine(
81-
"postgresql+pg8000://",
82-
creator=getconn,
77+
),
8378
)
8479
engine.dialect.description_encoding = None
8580
return engine, connector

tests/system/test_pg8000_iam_authn.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import os
1717

1818
# [START alloydb_sqlalchemy_connect_connector_iam_authn]
19-
import pg8000
2019
import sqlalchemy
2120

2221
from google.cloud.alloydb.connector import Connector
@@ -60,20 +59,16 @@ def create_sqlalchemy_engine(
6059
"""
6160
connector = Connector(refresh_strategy=refresh_strategy)
6261

63-
def getconn() -> pg8000.dbapi.Connection:
64-
conn: pg8000.dbapi.Connection = connector.connect(
62+
# create SQLAlchemy connection pool
63+
engine = sqlalchemy.create_engine(
64+
"postgresql+pg8000://",
65+
creator=lambda: connector.connect(
6566
inst_uri,
6667
"pg8000",
6768
user=user,
6869
db=db,
6970
enable_iam_auth=True,
70-
)
71-
return conn
72-
73-
# create SQLAlchemy connection pool
74-
engine = sqlalchemy.create_engine(
75-
"postgresql+pg8000://",
76-
creator=getconn,
71+
),
7772
)
7873
return engine, connector
7974

tests/system/test_pg8000_psc.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from datetime import datetime
1616
import os
1717

18-
import pg8000
1918
import sqlalchemy
2019

2120
from google.cloud.alloydb.connector import Connector
@@ -60,21 +59,17 @@ def create_sqlalchemy_engine(
6059
"""
6160
connector = Connector()
6261

63-
def getconn() -> pg8000.dbapi.Connection:
64-
conn: pg8000.dbapi.Connection = connector.connect(
62+
# create SQLAlchemy connection pool
63+
engine = sqlalchemy.create_engine(
64+
"postgresql+pg8000://",
65+
creator=lambda: connector.connect(
6566
inst_uri,
6667
"pg8000",
6768
user=user,
6869
password=password,
6970
db=db,
7071
ip_type="PSC",
71-
)
72-
return conn
73-
74-
# create SQLAlchemy connection pool
75-
engine = sqlalchemy.create_engine(
76-
"postgresql+pg8000://",
77-
creator=getconn,
72+
),
7873
)
7974
return engine, connector
8075

tests/system/test_pg8000_public_ip.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
import os
1717

1818
# [START alloydb_sqlalchemy_connect_connector_public_ip]
19-
import pg8000
2019
import sqlalchemy
2120

2221
from google.cloud.alloydb.connector import Connector
@@ -61,21 +60,17 @@ def create_sqlalchemy_engine(
6160
"""
6261
connector = Connector()
6362

64-
def getconn() -> pg8000.dbapi.Connection:
65-
conn: pg8000.dbapi.Connection = connector.connect(
63+
# create SQLAlchemy connection pool
64+
engine = sqlalchemy.create_engine(
65+
"postgresql+pg8000://",
66+
creator=lambda: connector.connect(
6667
inst_uri,
6768
"pg8000",
6869
user=user,
6970
password=password,
7071
db=db,
7172
ip_type="PUBLIC",
72-
)
73-
return conn
74-
75-
# create SQLAlchemy connection pool
76-
engine = sqlalchemy.create_engine(
77-
"postgresql+pg8000://",
78-
creator=getconn,
73+
),
7974
)
8075
return engine, connector
8176

0 commit comments

Comments
 (0)