Skip to content

Commit dec09ac

Browse files
authored
Merge pull request #28 from Azure-Samples/writeenv
Move away from "azd env get-values > .env"
2 parents 2c4762f + 9762b5b commit dec09ac

File tree

8 files changed

+36
-6
lines changed

8 files changed

+36
-6
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"

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}")

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)