-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add support for PSC #291
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,102 @@ | ||
# 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 | ||
|
||
# [START alloydb_sqlalchemy_connect_async_connector_psc] | ||
jackwotherspoon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 | ||
|
||
|
||
# [END alloydb_sqlalchemy_connect_async_connector_psc] | ||
|
||
|
||
@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,100 @@ | ||
# 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 | ||
|
||
# [START alloydb_sqlalchemy_connect_connector_psc] | ||
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 | ||
|
||
|
||
# [END alloydb_sqlalchemy_connect_connector_psc] | ||
|
||
|
||
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Was this necessary in Python?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried without it and it failed:
https://github.com/GoogleCloudPlatform/alloydb-python-connector/actions/runs/8471675334/job/23489146452