mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
test connection arguments for pg
This commit is contained in:
parent
cd6bc96bd0
commit
dcf58cdc8e
2 changed files with 25 additions and 11 deletions
|
@ -109,23 +109,28 @@ class AppSettings(BaseSettings):
|
|||
|
||||
SECRET: str = determine_secrets(DATA_DIR, PRODUCTION)
|
||||
|
||||
DB_URL: Union[str, PostgresDsn] = "sqlite"
|
||||
DB_ENGINE: Optional[str] = None # Optional: 'sqlite', 'postgres'
|
||||
POSTGRES_USER: str = "mealie"
|
||||
POSTGRES_PASSWORD: str = "mealie"
|
||||
POSTGRES_SERVER: str = "postgres"
|
||||
POSTGRES_PORT: str = 5432
|
||||
POSTGRES_DB: str = "mealie"
|
||||
|
||||
DB_URL: Union[str, PostgresDsn] = None # Actual DB_URL is calculated with `assemble_db_connection`
|
||||
|
||||
@validator("DB_URL", pre=True)
|
||||
def assemble_db_connection(cls, v: Optional[str], values: dict[str, Any]) -> Any:
|
||||
if isinstance(v, str) and "sqlite" in v:
|
||||
return determine_sqlite_path()
|
||||
return PostgresDsn.build(
|
||||
scheme="postgresql",
|
||||
user=values.get("POSTGRES_USER"),
|
||||
password=values.get("POSTGRES_PASSWORD"),
|
||||
host=values.get("POSTGRES_SERVER"),
|
||||
path=f"/{values.get('POSTGRES_DB') or ''}",
|
||||
)
|
||||
engine = values.get("DB_ENGINE", "sqlite")
|
||||
if engine == "postgres":
|
||||
host = f"{values.get('POSTGRES_SERVER')}:{values.get('POSTGRES_PORT')}"
|
||||
return PostgresDsn.build(
|
||||
scheme="postgresql",
|
||||
user=values.get("POSTGRES_USER"),
|
||||
password=values.get("POSTGRES_PASSWORD"),
|
||||
host=host,
|
||||
path=f"/{values.get('POSTGRES_DB') or ''}",
|
||||
)
|
||||
return determine_sqlite_path()
|
||||
|
||||
DEFAULT_GROUP: str = "Home"
|
||||
DEFAULT_EMAIL: str = "changeme@email.com"
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
from pathlib import Path
|
||||
|
||||
import re
|
||||
from mealie.core.config import CWD, DATA_DIR, AppDirectories, AppSettings, determine_data_dir, determine_secrets
|
||||
|
||||
|
||||
|
@ -47,6 +47,15 @@ def test_non_default_settings(monkeypatch):
|
|||
assert app_settings.REDOC_URL is None
|
||||
assert app_settings.DOCS_URL is None
|
||||
|
||||
def test_default_connection_args():
|
||||
app_settings = AppSettings()
|
||||
assert re.match(r"sqlite:////.*mealie/dev/data/mealie_v0.5.0.db", app_settings.DB_URL)
|
||||
|
||||
def test_pg_connection_args(monkeypatch):
|
||||
monkeypatch.setenv("DB_ENGINE", "postgres")
|
||||
app_settings = AppSettings()
|
||||
assert app_settings.DB_URL == "postgresql://mealie:mealie@postgres:5432/mealie"
|
||||
|
||||
|
||||
def test_secret_generation(tmp_path):
|
||||
app_dirs = AppDirectories(CWD, DATA_DIR)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue