Skip to content

Commit 7a4e633

Browse files
authored
Merge branch 'main' into dependabot/pip/sqlmodel-0.0.22
2 parents 3e90b24 + 1d138f8 commit 7a4e633

File tree

10 files changed

+61
-10
lines changed

10 files changed

+61
-10
lines changed

.env.azure

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Use these values to connect to the Azure database from within the devcontainer
1+
# Set these values to connect to the Azure database
2+
# Use write_azure_env.sh or write_azure_env.ps1 to set these values
23
POSTGRES_DATABASE="db"
34
POSTGRES_HOST="YOUR-SERVER-NAME.postgres.database.azure.com"
45
POSTGRES_SSL="require"

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2023 Azure Samples
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,10 @@ Follow these steps to deploy a PostgreSQL Flexible Server to Azure with the pgve
100100

101101
This will create a new resource group, and create the PostgreSQL Flexible server inside that group.
102102

103-
1. The example Python scripts look for configuration variables from a `.env` file located in the directory from where you invoke the scripts. You can easily create a file with the correct variables for your PostgreSQL server by running this command that copies the `azd` environment variables into your local `.env`:
103+
1. The example Python scripts look for configuration variables from a `.env` file located in the directory from where you invoke the scripts. You can easily create a file with the correct variables for your PostgreSQL server by running this script that copies the necessary `azd` environment variables into your local `.env`:
104104

105105
```shell
106-
azd env get-values > .env
106+
./write_azure_env.sh
107107
```
108108

109109
1. Now you may run the Python scripts in order to interact with the PostgreSQL server.

examples/asyncpg_items.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ async def async_main():
2323
POSTGRES_PASSWORD = os.environ["POSTGRES_PASSWORD"]
2424

2525
DATABASE_URI = f"postgresql://{POSTGRES_USERNAME}:{POSTGRES_PASSWORD}@{POSTGRES_HOST}/{POSTGRES_DATABASE}"
26+
# Specify SSL mode if needed
27+
if POSTGRES_SSL := os.environ.get("POSTGRES_SSL"):
28+
DATABASE_URI += f"?sslmode={POSTGRES_SSL}"
2629

2730
conn = await asyncpg.connect(DATABASE_URI)
2831

examples/sqlalchemy_async.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class Item(Base):
2121
id: Mapped[int] = mapped_column(primary_key=True)
2222
embedding = mapped_column(Vector(3))
2323

24+
2425
# Define HNSW index to support vector similarity search through the vector_l2_ops access method (Euclidean distance). The SQL operator for Euclidean distance is written as <->.
2526
index = Index(
2627
"hnsw_index_for_euclidean_distance_similarity_search",
@@ -30,6 +31,7 @@ class Item(Base):
3031
postgresql_ops={"embedding": "vector_l2_ops"},
3132
)
3233

34+
3335
async def insert_objects(async_session: async_sessionmaker[AsyncSession]) -> None:
3436
async with async_session() as session:
3537
async with session.begin():

examples/sqlalchemy_items.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ class Item(Base):
1717
id: Mapped[int] = mapped_column(primary_key=True)
1818
embedding = mapped_column(Vector(3))
1919

20+
2021
# Define HNSW index to support vector similarity search through the vector_l2_ops access method (Euclidean distance). The SQL operator for Euclidean distance is written as <->.
2122
index = Index(
2223
"hnsw_index_for_euclidean_distance_similarity_search",

examples/sqlalchemy_movies.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Movie(Base):
2020
title: Mapped[str] = mapped_column()
2121
title_vector = mapped_column(Vector(1536)) # ada-002 is 1536-dimensional
2222

23+
2324
# Define HNSW index to support vector similarity search through the vector_cosine_ops access method (cosine distance). The SQL operator for cosine distance is written as <=>.
2425
index = Index(
2526
"hnsw_index_for_cosine_distance_similarity_search",
@@ -81,9 +82,8 @@ class Movie(Base):
8182

8283
# Find the 5 most similar movies to "Winnie the Pooh"
8384
most_similars = session.scalars(
84-
select(Movie).order_by(
85-
Movie.title_vector.cosine_distance(target_movie.title_vector)
86-
).limit(5))
85+
select(Movie).order_by(Movie.title_vector.cosine_distance(target_movie.title_vector)).limit(5)
86+
)
8787
print(f"Five most similar movies to '{target_movie.title}':")
8888
for movie in most_similars:
8989
print(f"\t{movie.title}")

requirements.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
psycopg2==2.9.9
1+
psycopg2==2.9.10
22
python-dotenv==1.0.1
3-
SQLAlchemy[asyncio]==2.0.31
4-
pgvector==0.3.0
3+
SQLAlchemy[asyncio]==2.0.37
4+
pgvector==0.3.5
55
SQLModel==0.0.22
6-
asyncpg==0.29.0
6+
asyncpg==0.30.0
77
azure-identity

write_azure_env.ps1

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Clear the contents of the .env file
2+
Set-Content -Path .env -Value ""
3+
4+
# Append new values to the .env file
5+
$postgresDatabase = azd env get-value POSTGRES_DATABASE
6+
$postgresHost = azd env get-value POSTGRES_HOST
7+
$postgresSSL = azd env get-value POSTGRES_SSL
8+
$postgresUsername = azd env get-value POSTGRES_USERNAME
9+
10+
Add-Content -Path .env -Value "POSTGRES_DATABASE=$postgresDatabase"
11+
Add-Content -Path .env -Value "POSTGRES_HOST=$postgresHost"
12+
Add-Content -Path .env -Value "POSTGRES_SSL=$postgresSSL"
13+
Add-Content -Path .env -Value "POSTGRES_USERNAME=$postgresUsername"

write_azure_env.sh

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/bash
2+
3+
# Clear the contents of the .env file
4+
> .env
5+
6+
# Append new values to the .env file
7+
echo "POSTGRES_DATABASE=$(azd env get-value POSTGRES_DATABASE)" >> .env
8+
echo "POSTGRES_HOST=$(azd env get-value POSTGRES_HOST)" >> .env
9+
echo "POSTGRES_SSL=$(azd env get-value POSTGRES_SSL)" >> .env
10+
echo "POSTGRES_USERNAME=$(azd env get-value POSTGRES_USERNAME)" >> .env

0 commit comments

Comments
 (0)