Merge pull request #3395 from tba-code/postgres-url-feature

feat: PostgresProvider - Add POSTGRES_URL_OVERRIDE
This commit is contained in:
boc-the-git 2024-04-04 13:18:45 +11:00 committed by GitHub
commit bae7acbc3b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 36 deletions

View File

@ -35,6 +35,7 @@
| POSTGRES_SERVER | postgres | Postgres database server address | | POSTGRES_SERVER | postgres | Postgres database server address |
| POSTGRES_PORT | 5432 | Postgres database port | | POSTGRES_PORT | 5432 | Postgres database port |
| POSTGRES_DB | mealie | Postgres database name | | POSTGRES_DB | mealie | Postgres database name |
| POSTGRES_URL_OVERRIDE | None | Optional Postgres URL override to use instead of POSTGRES_* variables |
### Email ### Email

View File

@ -5,40 +5,39 @@ PostgreSQL might be considered if you need to support many concurrent users. In
**For Environment Variable Configuration, see** [Backend Configuration](./backend-config.md) **For Environment Variable Configuration, see** [Backend Configuration](./backend-config.md)
```yaml ```yaml
---
version: "3.7"
services: services:
mealie: mealie:
image: ghcr.io/mealie-recipes/mealie:v1.4.0 # (3) image: ghcr.io/mealie-recipes/mealie:v1.4.0 # (3)
container_name: mealie container_name: mealie
restart: always
ports: ports:
- "9925:9000" # (1) - "9925:9000" # (1)
deploy: deploy:
resources: resources:
limits: limits:
memory: 1000M # (2) memory: 1000M # (2)
depends_on:
- postgres
volumes: volumes:
- mealie-data:/app/data/ - mealie-data:/app/data/
environment: environment:
# Set Backend ENV Variables Here # Set Backend ENV Variables Here
- ALLOW_SIGNUP=true ALLOW_SIGNUP: true
- PUID=1000 PUID: 1000
- PGID=1000 PGID: 1000
- TZ=America/Anchorage TZ: America/Anchorage
- MAX_WORKERS=1 MAX_WORKERS: 1
- WEB_CONCURRENCY=1 WEB_CONCURRENCY: 1
- BASE_URL=https://mealie.yourdomain.com BASE_URL: https://mealie.yourdomain.com
# Database Settings # Database Settings
- DB_ENGINE=postgres DB_ENGINE: postgres
- POSTGRES_USER=mealie POSTGRES_USER: mealie
- POSTGRES_PASSWORD=mealie POSTGRES_PASSWORD: mealie
- POSTGRES_SERVER=postgres POSTGRES_SERVER: postgres
- POSTGRES_PORT=5432 POSTGRES_PORT: 5432
- POSTGRES_DB=mealie POSTGRES_DB: mealie
restart: always depends_on:
postgres:
condition: service_healthy
postgres: postgres:
container_name: postgres container_name: postgres
image: postgres:15 image: postgres:15
@ -48,12 +47,15 @@ services:
environment: environment:
POSTGRES_PASSWORD: mealie POSTGRES_PASSWORD: mealie
POSTGRES_USER: mealie POSTGRES_USER: mealie
healthcheck:
test: ["CMD", "pg_isready"]
interval: 30s
timeout: 20s
retries: 3
volumes: volumes:
mealie-data: mealie-data:
driver: local
mealie-pgdata: mealie-pgdata:
driver: local
``` ```
<!-- Updating This? Be Sure to also update the SQLite Annotations --> <!-- Updating This? Be Sure to also update the SQLite Annotations -->

View File

@ -9,12 +9,11 @@ SQLite is a popular, open source, self-contained, zero-configuration database th
**For Environment Variable Configuration, see** [Backend Configuration](./backend-config.md) **For Environment Variable Configuration, see** [Backend Configuration](./backend-config.md)
```yaml ```yaml
---
version: "3.7"
services: services:
mealie: mealie:
image: ghcr.io/mealie-recipes/mealie:v1.4.0 # (3) image: ghcr.io/mealie-recipes/mealie:v1.4.0 # (3)
container_name: mealie container_name: mealie
restart: always
ports: ports:
- "9925:9000" # (1) - "9925:9000" # (1)
deploy: deploy:
@ -25,18 +24,16 @@ services:
- mealie-data:/app/data/ - mealie-data:/app/data/
environment: environment:
# Set Backend ENV Variables Here # Set Backend ENV Variables Here
- ALLOW_SIGNUP=true ALLOW_SIGNUP: true
- PUID=1000 PUID: 1000
- PGID=1000 PGID: 1000
- TZ=America/Anchorage TZ: America/Anchorage
- MAX_WORKERS=1 MAX_WORKERS: 1
- WEB_CONCURRENCY=1 WEB_CONCURRENCY: 1
- BASE_URL=https://mealie.yourdomain.com BASE_URL: https://mealie.yourdomain.com
restart: always
volumes: volumes:
mealie-data: mealie-data:
driver: local
``` ```
<!-- Updating This? Be Sure to also update the Postgres Annotations --> <!-- Updating This? Be Sure to also update the Postgres Annotations -->

View File

@ -39,18 +39,25 @@ class PostgresProvider(AbstractDBProvider, BaseSettings):
POSTGRES_SERVER: str = "postgres" POSTGRES_SERVER: str = "postgres"
POSTGRES_PORT: str = "5432" POSTGRES_PORT: str = "5432"
POSTGRES_DB: str = "mealie" POSTGRES_DB: str = "mealie"
POSTGRES_URL_OVERRIDE: str | None = None
model_config = SettingsConfigDict(arbitrary_types_allowed=True, extra="allow") model_config = SettingsConfigDict(arbitrary_types_allowed=True, extra="allow")
@property @property
def db_url(self) -> str: def db_url(self) -> str:
host = f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}" if self.POSTGRES_URL_OVERRIDE:
url = PostgresDsn(url=self.POSTGRES_URL_OVERRIDE)
if not url.scheme == ("postgresql"):
raise ValueError("POSTGRES_URL_OVERRIDE scheme must be postgresql")
return str(url)
return str( return str(
PostgresDsn.build( PostgresDsn.build(
scheme="postgresql", scheme="postgresql",
username=self.POSTGRES_USER, username=self.POSTGRES_USER,
password=urlparse.quote_plus(self.POSTGRES_PASSWORD), password=urlparse.quote_plus(self.POSTGRES_PASSWORD),
host=host, host=f"{self.POSTGRES_SERVER}:{self.POSTGRES_PORT}",
path=f"{self.POSTGRES_DB or ''}", path=f"{self.POSTGRES_DB or ''}",
) )
) )