Skip to content

Commit 7e34800

Browse files
committed
refactor: simplify postgresql env vars
1 parent f7abc9f commit 7e34800

File tree

7 files changed

+47
-70
lines changed

7 files changed

+47
-70
lines changed

.env

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
11
PYTHONDONTWRITEBYTECODE=1
22
PYTHONUNBUFFERED=1
33

4-
SQL_DB=devdb
5-
SQL_TEST_DB=testdb
6-
SQL_HOST=db
7-
SQL_USER=user
8-
SQL_PASS=secret
9-
104
# Postgres
11-
POSTGRES_SERVER=db
5+
POSTGRES_HOST=db
126
POSTGRES_PORT=5432
137
POSTGRES_DB=devdb
14-
POSTGRES_TEST_DB=testdb
158
POSTGRES_USER=user
169
POSTGRES_PASSWORD=secret
1710

.github/workflows/build-and-test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ jobs:
1818
env:
1919
PYTHONDONTWRITEBYTECODE: 1
2020
PYTHONUNBUFFERED: 1
21-
SQL_DB: testdb
22-
SQL_HOST: 127.0.0.1
23-
SQL_USER: app-user
21+
POSTGRES_DB: testdb
22+
POSTGRES_HOST: 127.0.0.1
23+
POSTGRES_USER: app-user
2424
POSTGRES_PASSWORD: secret
2525
PGPASSWORD: secret
2626
REDIS_HOST: 127.0.0.1

app/config.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ class Settings(BaseSettings):
2121
JWT_ALGORITHM: str
2222
JWT_EXPIRE: int
2323

24-
SQL_USER: str
25-
SQL_PASS: str
26-
SQL_HOST: str
27-
SQL_DB: str
24+
POSTGRES_USER: str
25+
POSTGRES_PASSWORD: str
26+
POSTGRES_HOST: str
27+
POSTGRES_DB: str
2828

2929
@computed_field
3030
@property
@@ -57,20 +57,20 @@ def asyncpg_url(self) -> PostgresDsn:
5757
5858
The URL is built using the MultiHostUrl.build method, which takes the following parameters:
5959
- scheme: The scheme of the URL. In this case, it is "postgresql+asyncpg".
60-
- username: The username for the SQL database, retrieved from the SQL_USER environment variable.
61-
- password: The password for the SQL database, retrieved from the SQL_PASS environment variable.
62-
- host: The host of the SQL database, retrieved from the SQL_HOST environment variable.
63-
- path: The path of the SQL database, retrieved from the SQL_DB environment variable.
60+
- username: The username for the Postgres database, retrieved from the POSTGRES_USER environment variable.
61+
- password: The password for the Postgres database, retrieved from the POSTGRES_PASSWORD environment variable.
62+
- host: The host of the Postgres database, retrieved from the POSTGRES_HOST environment variable.
63+
- path: The path of the Postgres database, retrieved from the POSTGRES_DB environment variable.
6464
6565
Returns:
6666
PostgresDsn: The constructed PostgresDsn URL for asyncpg.
6767
"""
6868
return MultiHostUrl.build(
6969
scheme="postgresql+asyncpg",
70-
username=self.SQL_USER,
71-
password=self.SQL_PASS,
72-
host=self.SQL_HOST,
73-
path=self.SQL_DB,
70+
username=self.POSTGRES_USER,
71+
password=self.POSTGRES_PASSWORD,
72+
host=self.POSTGRES_HOST,
73+
path=self.POSTGRES_DB,
7474
)
7575

7676

docker-compose.test.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ version: '3.8'
33
services:
44
app:
55
environment:
6-
- SQL_DB=testdb
6+
- POSTGRES_DB=testdb
77

88
db:
99
environment:
10-
- POSTGRES_USER=${SQL_USER}
11-
- SQL_DB=testdb
10+
- POSTGRES_USER=${POSTGRES_USER}
11+
- POSTGRES_DB=testdb

docker-compose.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,16 @@ services:
3131
- fastapi_postgres_data:/var/lib/postgresql/data
3232
env_file:
3333
- .env
34-
- .secrets
3534
ports:
3635
- 5432:5432
3736
environment:
38-
- POSTGRES_USER=${SQL_USER}
37+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
38+
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
39+
- POSTGRES_DB=${POSTGRES_DB?Variable not set}
3940
healthcheck:
4041
test:
4142
[
42-
"CMD-SHELL", "pg_isready -d $SQL_DB -U $SQL_USER"
43+
"CMD-SHELL", "pg_isready -d $POSTGRES_DB -U $POSTGRES_USER"
4344
]
4445
interval: 5s
4546
timeout: 5s

poetry.lock

Lines changed: 20 additions & 37 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
[tool.poetry]
22
name = "fastapi-sqlalchemy-asyncpg"
3-
version = "0.0.8"
3+
version = "0.0.9"
44
description = ""
55
authors = ["Jakub Miazek <the@grillazz.com>"]
66
packages = []
77
license = "MIT"
88

99
[tool.poetry.dependencies]
10-
python = "^3.11"
10+
python = "^3.12"
1111
fastapi = "0.110.0"
1212
pydantic = {version = "2.6.4", extras = ["email"]}
1313
pydantic-settings = "2.2.1"
@@ -54,8 +54,8 @@ ignore = ["E501"]
5454

5555
# Exclude a variety of commonly ignored directories.
5656
exclude = ["alembic",]
57-
# Assume Python 3.10.
58-
target-version = "py311"
57+
# Assume Python 3.11.
58+
target-version = "py312"
5959

6060
[tool.ruff.flake8-quotes]
6161
docstring-quotes = "double"

0 commit comments

Comments
 (0)