Skip to content

Commit

Permalink
Merge pull request #141 from grillazz/refactor
Browse files Browse the repository at this point in the history
refactor: simplify postgresql env vars
  • Loading branch information
grillazz authored Mar 24, 2024
2 parents 3b98c21 + 85789ee commit a5dff87
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 72 deletions.
9 changes: 1 addition & 8 deletions .env
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
PYTHONDONTWRITEBYTECODE=1
PYTHONUNBUFFERED=1

SQL_DB=devdb
SQL_TEST_DB=testdb
SQL_HOST=db
SQL_USER=user
SQL_PASS=secret

# Postgres
POSTGRES_SERVER=db
POSTGRES_HOST=db
POSTGRES_PORT=5432
POSTGRES_DB=devdb
POSTGRES_TEST_DB=testdb
POSTGRES_USER=user
POSTGRES_PASSWORD=secret

Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/build-and-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ jobs:
env:
PYTHONDONTWRITEBYTECODE: 1
PYTHONUNBUFFERED: 1
SQL_DB: testdb
SQL_HOST: 127.0.0.1
SQL_USER: app-user
POSTGRES_DB: testdb
POSTGRES_HOST: 127.0.0.1
POSTGRES_USER: app-user
POSTGRES_PASSWORD: secret
PGPASSWORD: secret
REDIS_HOST: 127.0.0.1
Expand Down
24 changes: 12 additions & 12 deletions app/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class Settings(BaseSettings):
JWT_ALGORITHM: str
JWT_EXPIRE: int

SQL_USER: str
SQL_PASS: str
SQL_HOST: str
SQL_DB: str
POSTGRES_USER: str
POSTGRES_PASSWORD: str
POSTGRES_HOST: str
POSTGRES_DB: str

@computed_field
@property
Expand Down Expand Up @@ -57,20 +57,20 @@ def asyncpg_url(self) -> PostgresDsn:
The URL is built using the MultiHostUrl.build method, which takes the following parameters:
- scheme: The scheme of the URL. In this case, it is "postgresql+asyncpg".
- username: The username for the SQL database, retrieved from the SQL_USER environment variable.
- password: The password for the SQL database, retrieved from the SQL_PASS environment variable.
- host: The host of the SQL database, retrieved from the SQL_HOST environment variable.
- path: The path of the SQL database, retrieved from the SQL_DB environment variable.
- username: The username for the Postgres database, retrieved from the POSTGRES_USER environment variable.
- password: The password for the Postgres database, retrieved from the POSTGRES_PASSWORD environment variable.
- host: The host of the Postgres database, retrieved from the POSTGRES_HOST environment variable.
- path: The path of the Postgres database, retrieved from the POSTGRES_DB environment variable.
Returns:
PostgresDsn: The constructed PostgresDsn URL for asyncpg.
"""
return MultiHostUrl.build(
scheme="postgresql+asyncpg",
username=self.SQL_USER,
password=self.SQL_PASS,
host=self.SQL_HOST,
path=self.SQL_DB,
username=self.POSTGRES_USER,
password=self.POSTGRES_PASSWORD,
host=self.POSTGRES_HOST,
path=self.POSTGRES_DB,
)


Expand Down
2 changes: 2 additions & 0 deletions db/create.sql
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- DROP DATABASE IF EXISTS devdb;
-- CREATE DATABASE devdb;
\connect devdb;
CREATE SCHEMA shakespeare;
CREATE SCHEMA happy_hog;
Expand Down
6 changes: 3 additions & 3 deletions docker-compose.test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ version: '3.8'
services:
app:
environment:
- SQL_DB=testdb
- POSTGRES_DB=testdb

db:
environment:
- POSTGRES_USER=${SQL_USER}
- SQL_DB=testdb
- POSTGRES_USER=${POSTGRES_USER}
- POSTGRES_DB=testdb
7 changes: 4 additions & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,16 @@ services:
- fastapi_postgres_data:/var/lib/postgresql/data
env_file:
- .env
- .secrets
ports:
- 5432:5432
environment:
- POSTGRES_USER=${SQL_USER}
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD?Variable not set}
- POSTGRES_USER=${POSTGRES_USER?Variable not set}
- POSTGRES_DB=${POSTGRES_DB?Variable not set}
healthcheck:
test:
[
"CMD-SHELL", "pg_isready -d $SQL_DB -U $SQL_USER"
"CMD-SHELL", "pg_isready -d $POSTGRES_DB -U $POSTGRES_USER"
]
interval: 5s
timeout: 5s
Expand Down
57 changes: 20 additions & 37 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
[tool.poetry]
name = "fastapi-sqlalchemy-asyncpg"
version = "0.0.8"
version = "0.0.9"
description = ""
authors = ["Jakub Miazek <[email protected]>"]
packages = []
license = "MIT"

[tool.poetry.dependencies]
python = "^3.11"
python = "^3.12"
fastapi = "0.110.0"
pydantic = {version = "2.6.4", extras = ["email"]}
pydantic-settings = "2.2.1"
Expand Down Expand Up @@ -54,8 +54,8 @@ ignore = ["E501"]

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

[tool.ruff.flake8-quotes]
docstring-quotes = "double"
Expand Down
10 changes: 8 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from httpx import AsyncClient
from httpx import AsyncClient, ASGITransport

from app.database import engine
from app.main import app
Expand Down Expand Up @@ -29,10 +29,16 @@ async def start_db():

@pytest.fixture(scope="session")
async def client(start_db) -> AsyncClient:
async with AsyncClient(

transport = ASGITransport(
app=app,

)
async with AsyncClient(
# app=app,
base_url="http://testserver/v1",
headers={"Content-Type": "application/json"},
transport=transport,
) as test_client:
app.state.redis = await get_redis()
yield test_client

0 comments on commit a5dff87

Please sign in to comment.