-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d845ac3
commit 9698431
Showing
12 changed files
with
300 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
from typing import Tuple | ||
|
||
import asyncpg | ||
import pytest | ||
import sqlalchemy | ||
import sqlalchemy.ext.asyncio | ||
|
||
from google.cloud.alloydb.connector import AsyncConnector | ||
|
||
|
||
async def create_sqlalchemy_engine( | ||
inst_uri: str, | ||
user: str, | ||
password: str, | ||
db: str, | ||
) -> Tuple[sqlalchemy.ext.asyncio.engine.AsyncEngine, AsyncConnector]: | ||
"""Creates a connection pool for an AlloyDB instance and returns the pool | ||
and the connector. Callers are responsible for closing the pool and the | ||
connector. | ||
A sample invocation looks like: | ||
engine, connector = await create_sqlalchemy_engine( | ||
inst_uri, | ||
user, | ||
password, | ||
db, | ||
) | ||
async with engine.connect() as conn: | ||
time = await conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() | ||
curr_time = time[0] | ||
# do something with query result | ||
await connector.close() | ||
Args: | ||
instance_uri (str): | ||
The instance URI specifies the instance relative to the project, | ||
region, and cluster. For example: | ||
"projects/my-project/locations/us-central1/clusters/my-cluster/instances/my-instance" | ||
user (str): | ||
The database user name, e.g., postgres | ||
password (str): | ||
The database user's password, e.g., secret-password | ||
db_name (str): | ||
The name of the database, e.g., mydb | ||
""" | ||
connector = AsyncConnector() | ||
|
||
async def getconn() -> asyncpg.Connection: | ||
conn: asyncpg.Connection = await connector.connect( | ||
inst_uri, | ||
"asyncpg", | ||
user=user, | ||
password=password, | ||
db=db, | ||
ip_type="PSC", | ||
) | ||
return conn | ||
|
||
# create SQLAlchemy connection pool | ||
engine = sqlalchemy.ext.asyncio.create_async_engine( | ||
"postgresql+asyncpg://", | ||
async_creator=getconn, | ||
execution_options={"isolation_level": "AUTOCOMMIT"}, | ||
) | ||
return engine, connector | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_connection_with_asyncpg() -> None: | ||
"""Basic test to get time from database.""" | ||
inst_uri = os.environ["ALLOYDB_PSC_INSTANCE_URI"] | ||
user = os.environ["ALLOYDB_USER"] | ||
password = os.environ["ALLOYDB_PASS"] | ||
db = os.environ["ALLOYDB_DB"] | ||
|
||
pool, connector = await create_sqlalchemy_engine(inst_uri, user, password, db) | ||
|
||
async with pool.connect() as conn: | ||
res = (await conn.execute(sqlalchemy.text("SELECT 1"))).fetchone() | ||
assert res[0] == 1 | ||
|
||
await connector.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright 2024 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from datetime import datetime | ||
import os | ||
from typing import Tuple | ||
|
||
import pg8000 | ||
import sqlalchemy | ||
|
||
from google.cloud.alloydb.connector import Connector | ||
|
||
|
||
def create_sqlalchemy_engine( | ||
inst_uri: str, | ||
user: str, | ||
password: str, | ||
db: str, | ||
) -> Tuple[sqlalchemy.engine.Engine, Connector]: | ||
"""Creates a connection pool for an AlloyDB instance and returns the pool | ||
and the connector. Callers are responsible for closing the pool and the | ||
connector. | ||
A sample invocation looks like: | ||
engine, connector = create_sqlalchemy_engine( | ||
inst_uri, | ||
user, | ||
password, | ||
db, | ||
) | ||
with engine.connect() as conn: | ||
time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() | ||
conn.commit() | ||
curr_time = time[0] | ||
# do something with query result | ||
connector.close() | ||
Args: | ||
instance_uri (str): | ||
The instance URI specifies the instance relative to the project, | ||
region, and cluster. For example: | ||
"projects/my-project/locations/us-central1/clusters/my-cluster/instances/my-instance" | ||
user (str): | ||
The database user name, e.g., postgres | ||
password (str): | ||
The database user's password, e.g., secret-password | ||
db_name (str): | ||
The name of the database, e.g., mydb | ||
""" | ||
connector = Connector() | ||
|
||
def getconn() -> pg8000.dbapi.Connection: | ||
conn: pg8000.dbapi.Connection = connector.connect( | ||
inst_uri, | ||
"pg8000", | ||
user=user, | ||
password=password, | ||
db=db, | ||
ip_type="PSC", | ||
) | ||
return conn | ||
|
||
# create SQLAlchemy connection pool | ||
engine = sqlalchemy.create_engine( | ||
"postgresql+pg8000://", | ||
creator=getconn, | ||
) | ||
return engine, connector | ||
|
||
|
||
def test_pg8000_time() -> None: | ||
"""Basic test to get time from database.""" | ||
inst_uri = os.environ["ALLOYDB_PSC_INSTANCE_URI"] | ||
user = os.environ["ALLOYDB_USER"] | ||
password = os.environ["ALLOYDB_PASS"] | ||
db = os.environ["ALLOYDB_DB"] | ||
|
||
engine, connector = create_sqlalchemy_engine(inst_uri, user, password, db) | ||
with engine.connect() as conn: | ||
time = conn.execute(sqlalchemy.text("SELECT NOW()")).fetchone() | ||
conn.commit() | ||
curr_time = time[0] | ||
assert type(curr_time) is datetime | ||
connector.close() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.