mirror of
https://github.com/mealie-recipes/mealie.git
synced 2026-02-04 06:53:12 -05:00
feature/additional-db (#371)
* add support for setting db_url * fix tests * add db_username/password env variables * init db if super user doesn't exist * fix tests * fix tests * set SQLite default DB_URL * don't run tests on draft PRs * add lint/black tests * add test-all * spell check settings * black/flake8 * check format fail * new badges * rename workflow * fix formatting * remove white-space * test connection arguments for pg * format * add new values to template * format * remove old script * monkeypatch test db * working docker-compose for postgres * update docs * test pg workflow * format * add driver * install w/ poetry * setup container * change image * set database to localhost * update tests * set url * fix url path * disable cache * database init * bust cache * get by name Co-authored-by: hay-kot <hay-kot@pm.me>
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import os
|
||||
import secrets
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from typing import Any, Optional, Union
|
||||
|
||||
import dotenv
|
||||
from pydantic import BaseSettings, Field, validator
|
||||
from pydantic import BaseSettings, Field, PostgresDsn, validator
|
||||
|
||||
APP_VERSION = "v0.5.0beta"
|
||||
DB_VERSION = "v0.5.0"
|
||||
@@ -57,7 +57,6 @@ class AppDirectories:
|
||||
self.CHOWDOWN_DIR: Path = self.MIGRATION_DIR.joinpath("chowdown")
|
||||
self.TEMPLATE_DIR: Path = data_dir.joinpath("templates")
|
||||
self.USER_DIR: Path = data_dir.joinpath("users")
|
||||
self.SQLITE_DIR: Path = data_dir.joinpath("db")
|
||||
self.RECIPE_DATA_DIR: Path = data_dir.joinpath("recipes")
|
||||
self.TEMP_DIR: Path = data_dir.joinpath(".temp")
|
||||
|
||||
@@ -70,7 +69,6 @@ class AppDirectories:
|
||||
self.DEBUG_DIR,
|
||||
self.MIGRATION_DIR,
|
||||
self.TEMPLATE_DIR,
|
||||
self.SQLITE_DIR,
|
||||
self.NEXTCLOUD_DIR,
|
||||
self.CHOWDOWN_DIR,
|
||||
self.RECIPE_DATA_DIR,
|
||||
@@ -84,6 +82,16 @@ class AppDirectories:
|
||||
app_dirs = AppDirectories(CWD, DATA_DIR)
|
||||
|
||||
|
||||
def determine_sqlite_path(path=False, suffix=DB_VERSION) -> str:
|
||||
global app_dirs
|
||||
db_path = app_dirs.DATA_DIR.joinpath(f"mealie_{suffix}.db") # ! Temporary Until Alembic
|
||||
|
||||
if path:
|
||||
return db_path
|
||||
|
||||
return "sqlite:///" + str(db_path.absolute())
|
||||
|
||||
|
||||
class AppSettings(BaseSettings):
|
||||
global DATA_DIR
|
||||
PRODUCTION: bool = Field(True, env="PRODUCTION")
|
||||
@@ -100,21 +108,29 @@ class AppSettings(BaseSettings):
|
||||
return "/redoc" if self.API_DOCS else None
|
||||
|
||||
SECRET: str = determine_secrets(DATA_DIR, PRODUCTION)
|
||||
DATABASE_TYPE: str = Field("sqlite", env="DB_TYPE")
|
||||
|
||||
@validator("DATABASE_TYPE", pre=True)
|
||||
def validate_db_type(cls, v: str) -> Optional[str]:
|
||||
if v != "sqlite":
|
||||
raise ValueError("Unable to determine database type. Acceptible options are 'sqlite'")
|
||||
else:
|
||||
return v
|
||||
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"
|
||||
|
||||
# Used to Set SQLite File Version
|
||||
SQLITE_FILE: Optional[Union[str, Path]]
|
||||
DB_URL: Union[str, PostgresDsn] = None # Actual DB_URL is calculated with `assemble_db_connection`
|
||||
|
||||
@validator("SQLITE_FILE", pre=True)
|
||||
def identify_sqlite_file(cls, v: str) -> Optional[str]:
|
||||
return app_dirs.SQLITE_DIR.joinpath(f"mealie_{DB_VERSION}.sqlite")
|
||||
@validator("DB_URL", pre=True)
|
||||
def assemble_db_connection(cls, v: Optional[str], values: dict[str, Any]) -> Any:
|
||||
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"
|
||||
|
||||
Reference in New Issue
Block a user