Skip to content

Commit 21c68a4

Browse files
author
Bob Tabor
committed
Parse connection string to URL
1 parent 6edc9f4 commit 21c68a4

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

‎requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ fastapi==0.111.1
55
psycopg2
66
SQLAlchemy==2.0.31
77
sqlmodel==0.0.20
8+
urllib

‎src/fastapi_app/models.py‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
import os
22
import typing
33
from datetime import datetime
4-
4+
from urllib.parse import quote_plus
55
from sqlmodel import Field, SQLModel, create_engine
66

77
sql_url = ""
88
if os.getenv("AZURE_POSTGRESQL_CONNECTIONSTRING"):
9-
sql_url = os.getenv("AZURE_POSTGRESQL_CONNECTIONSTRING")
9+
env_connection_string = os.getenv("AZURE_POSTGRESQL_CONNECTIONSTRING")
10+
11+
# Parse the connection string
12+
details = dict(item.split('=') for item in env_connection_string.split())
13+
14+
# Properly format the URL for SQLAlchemy
15+
sql_url = (
16+
f"postgresql://{quote_plus(details['user'])}:{quote_plus(details['password'])}"
17+
f"@{details['host']}:{details['port']}/{details['dbname']}?sslmode={details['sslmode']}"
18+
)
19+
1020
else:
1121
POSTGRES_USERNAME = os.environ.get("DBUSER")
1222
POSTGRES_PASSWORD = os.environ.get("DBPASS")

‎src/requirements.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ fastapi==0.111.1
55
psycopg2
66
SQLAlchemy==2.0.31
77
sqlmodel==0.0.20
8+
urllib

0 commit comments

Comments
 (0)