init db if super user doesn't exist

This commit is contained in:
hay-kot 2021-04-29 22:38:16 -08:00
commit 401f08dba8
4 changed files with 35 additions and 14 deletions

View file

@ -1,10 +1,10 @@
import os
import secrets
from pathlib import Path
from typing import Optional
from typing import Any, Optional, Union
import dotenv
from pydantic import BaseSettings, Field
from pydantic import BaseSettings, Field, PostgresDsn, validator
APP_VERSION = "v0.5.0beta"
DB_VERSION = "v0.5.0"
@ -82,9 +82,13 @@ class AppDirectories:
app_dirs = AppDirectories(CWD, DATA_DIR)
def determine_sqlite_path() -> str:
def determine_sqlite_path(path=False) -> str:
global app_dirs
db_path = app_dirs.DATA_DIR.joinpath(f"mealie_{DB_VERSION}.db") # ! Temporary Until Alembic
db_path = app_dirs.DATA_DIR.joinpath(f"mealie_{DB_VERSION}.db") # ! Temporary Until Alembic
if path:
return db_path
return "sqlite:///" + str(db_path.absolute())
@ -104,9 +108,24 @@ class AppSettings(BaseSettings):
return "/redoc" if self.API_DOCS else None
SECRET: str = determine_secrets(DATA_DIR, PRODUCTION)
DB_URL: str = Field(default_factory=determine_sqlite_path)
DB_USERNAME: str = 'mealie'
DB_PASSWORD: str = 'mealie'
DB_URL: Union[str, PostgresDsn]
POSTGRES_USER: str = "mealie"
POSTGRES_PASSWORD: str = "mealie"
POSTGRES_SERVER: str = "postgres"
POSTGRES_DB: str = "mealie"
@validator("DB_URL", pre=True)
def assemble_db_connection(cls, v: Optional[str], values: dict[str, Any]) -> Any:
if isinstance(v, str):
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 ''}",
)
DEFAULT_GROUP: str = "Home"
DEFAULT_EMAIL: str = "changeme@email.com"

View file

@ -4,9 +4,7 @@ from mealie.core.config import settings
from mealie.db.models.db_session import sql_global_init
from sqlalchemy.orm.session import Session
sql_exists = True
sql_exists = Path(settings.DB_URL).is_file()
SessionLocal = sql_global_init(settings.DB_URL)

View file

@ -2,7 +2,7 @@ from mealie.core import root_logger
from mealie.core.config import settings
from mealie.core.security import get_password_hash
from mealie.db.database import db
from mealie.db.db_setup import create_session, sql_exists
from mealie.db.db_setup import create_session, sqlite_exists
from mealie.schema.settings import SiteSettings
from mealie.schema.theme import SiteTheme
from sqlalchemy.orm import Session
@ -51,7 +51,9 @@ def default_user_init(session: Session):
def main():
if sql_exists:
session = create_session()
init_user = db.users.get(session, "1", "id")
if init_user:
print("Database Exists")
else:
print("Database Doesn't Exists, Initializing...")

View file

@ -15,9 +15,11 @@ API_PORT=9000
API_DOCS=True
# Sets the Database type to use.
# DB_URL
DB_USERNAME=mealie
DB_PASSWORD=mealie
DB_URL=sqlite
# POSTGRES_USER
# POSTGRES_PASSWORD
# POSTGRES_SERVER
# POSTGRES_DB
# Sets the token expiration time in hours.
TOKEN_TIME=24