Skip to content

Commit a5dff87

Browse files
authored
Merge pull request #141 from grillazz/refactor
refactor: simplify postgresql env vars
2 parents 3b98c21 + 85789ee commit a5dff87

File tree

9 files changed

+57
-72
lines changed

9 files changed

+57
-72
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

db/create.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
-- DROP DATABASE IF EXISTS devdb;
2+
-- CREATE DATABASE devdb;
13
\connect devdb;
24
CREATE SCHEMA shakespeare;
35
CREATE SCHEMA happy_hog;

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"

tests/conftest.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from httpx import AsyncClient
2+
from httpx import AsyncClient, ASGITransport
33

44
from app.database import engine
55
from app.main import app
@@ -29,10 +29,16 @@ async def start_db():
2929

3030
@pytest.fixture(scope="session")
3131
async def client(start_db) -> AsyncClient:
32-
async with AsyncClient(
32+
33+
transport = ASGITransport(
3334
app=app,
35+
36+
)
37+
async with AsyncClient(
38+
# app=app,
3439
base_url="http://testserver/v1",
3540
headers={"Content-Type": "application/json"},
41+
transport=transport,
3642
) as test_client:
3743
app.state.redis = await get_redis()
3844
yield test_client

0 commit comments

Comments
 (0)