Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,5 @@ SMTP_TLS=False
SMTP_PORT=1025

# Postgres
POSTGRES_SERVER=localhost
POSTGRES_DB=app
POSTGRES_USER=postgres
POSTGRES_PASSWORD=changethis
DATABASE_URL=postgresql://postgres:${POSTGRES_PASSWORD}@localhost:5432/app
2 changes: 1 addition & 1 deletion backend/app/alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@


def get_url():
return str(settings.SQLALCHEMY_DATABASE_URI)
return str(settings.DATABASE_URL)


def run_migrations_offline():
Expand Down
29 changes: 12 additions & 17 deletions backend/app/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
HttpUrl,
PostgresDsn,
computed_field,
field_validator,
model_validator,
)
from pydantic_settings import BaseSettings, SettingsConfigDict
Expand All @@ -27,23 +28,16 @@ class Settings(BaseSettings):

PROJECT_NAME: str
SENTRY_DSN: HttpUrl | None = None
POSTGRES_SERVER: str
POSTGRES_PORT: int = 5432
POSTGRES_USER: str
POSTGRES_PASSWORD: str = ""
POSTGRES_DB: str = ""
DATABASE_URL: PostgresDsn

@computed_field # type: ignore[prop-decorator]
@property
def SQLALCHEMY_DATABASE_URI(self) -> PostgresDsn:
return PostgresDsn.build(
scheme="postgresql+psycopg",
username=self.POSTGRES_USER,
password=self.POSTGRES_PASSWORD,
host=self.POSTGRES_SERVER,
port=self.POSTGRES_PORT,
path=self.POSTGRES_DB,
)
@field_validator("DATABASE_URL", mode="before")
@classmethod
def _use_psycopg_driver(cls, value: str | PostgresDsn) -> str:
database_url = str(value)
for scheme in ("postgres://", "postgresql://"):
if database_url.startswith(scheme):
return database_url.replace(scheme, "postgresql+psycopg://", 1)
return database_url

SMTP_TLS: bool = True
SMTP_SSL: bool = False
Expand Down Expand Up @@ -85,7 +79,8 @@ def _check_default_secret(self, var_name: str, value: str | None) -> None:
@model_validator(mode="after")
def _enforce_non_default_secrets(self) -> Self:
self._check_default_secret("SECRET_KEY", self.SECRET_KEY)
self._check_default_secret("POSTGRES_PASSWORD", self.POSTGRES_PASSWORD)
for host in self.DATABASE_URL.hosts():
self._check_default_secret("DATABASE_URL password", host["password"])
self._check_default_secret(
"FIRST_SUPERUSER_PASSWORD", self.FIRST_SUPERUSER_PASSWORD
)
Expand Down
2 changes: 1 addition & 1 deletion backend/app/core/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from app.core.config import settings
from app.models import User, UserCreate

engine = create_engine(str(settings.SQLALCHEMY_DATABASE_URI))
engine = create_engine(str(settings.DATABASE_URL))


# make sure all SQLModel models are imported (app.models) before initializing DB
Expand Down
10 changes: 3 additions & 7 deletions compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ services:
db:
image: postgres:18
healthcheck:
test: ["CMD-SHELL", "pg_isready -U ${POSTGRES_USER} -d ${POSTGRES_DB}"]
test: ["CMD-SHELL", "pg_isready -U postgres -d app"]
interval: 10s
retries: 5
start_period: 30s
Expand All @@ -29,8 +29,7 @@ services:
- app-db-data:/var/lib/postgresql
environment:
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD:?Variable not set}
- POSTGRES_USER=${POSTGRES_USER:?Variable not set}
- POSTGRES_DB=${POSTGRES_DB:?Variable not set}
- POSTGRES_DB=app

adminer:
image: adminer
Expand Down Expand Up @@ -62,10 +61,7 @@ services:
SMTP_USER: ${SMTP_USER:-}
SMTP_PASSWORD: ${SMTP_PASSWORD:-}
EMAILS_FROM_EMAIL: ${EMAILS_FROM_EMAIL}
POSTGRES_SERVER: db
POSTGRES_DB: ${POSTGRES_DB:?Variable not set}
POSTGRES_USER: ${POSTGRES_USER:?Variable not set}
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:?Variable not set}
DATABASE_URL: postgresql://postgres:${POSTGRES_PASSWORD:?Variable not set}@db:5432/app
SENTRY_DSN: ${SENTRY_DSN:-}

healthcheck:
Expand Down
2 changes: 0 additions & 2 deletions deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ You can set several other environment variables:
* `SMTP_USER`: The SMTP server user to send emails.
* `SMTP_PASSWORD`: The SMTP server password to send emails.
* `EMAILS_FROM_EMAIL`: The email account to send emails from.
* `POSTGRES_USER`: The Postgres user, you can leave the default.
* `POSTGRES_DB`: The database name to use for this application. You can leave the default of `app`.
* `SENTRY_DSN`: The DSN for Sentry, if you are using it.

## GitHub Actions Environment Variables
Expand Down
Loading