mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
init db if super user doesn't exist
This commit is contained in:
parent
5efad58650
commit
401f08dba8
4 changed files with 35 additions and 14 deletions
|
@ -1,10 +1,10 @@
|
||||||
import os
|
import os
|
||||||
import secrets
|
import secrets
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from typing import Optional
|
from typing import Any, Optional, Union
|
||||||
|
|
||||||
import dotenv
|
import dotenv
|
||||||
from pydantic import BaseSettings, Field
|
from pydantic import BaseSettings, Field, PostgresDsn, validator
|
||||||
|
|
||||||
APP_VERSION = "v0.5.0beta"
|
APP_VERSION = "v0.5.0beta"
|
||||||
DB_VERSION = "v0.5.0"
|
DB_VERSION = "v0.5.0"
|
||||||
|
@ -82,9 +82,13 @@ class AppDirectories:
|
||||||
app_dirs = AppDirectories(CWD, DATA_DIR)
|
app_dirs = AppDirectories(CWD, DATA_DIR)
|
||||||
|
|
||||||
|
|
||||||
def determine_sqlite_path() -> str:
|
def determine_sqlite_path(path=False) -> str:
|
||||||
global app_dirs
|
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())
|
return "sqlite:///" + str(db_path.absolute())
|
||||||
|
|
||||||
|
|
||||||
|
@ -104,9 +108,24 @@ class AppSettings(BaseSettings):
|
||||||
return "/redoc" if self.API_DOCS else None
|
return "/redoc" if self.API_DOCS else None
|
||||||
|
|
||||||
SECRET: str = determine_secrets(DATA_DIR, PRODUCTION)
|
SECRET: str = determine_secrets(DATA_DIR, PRODUCTION)
|
||||||
DB_URL: str = Field(default_factory=determine_sqlite_path)
|
|
||||||
DB_USERNAME: str = 'mealie'
|
DB_URL: Union[str, PostgresDsn]
|
||||||
DB_PASSWORD: str = 'mealie'
|
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_GROUP: str = "Home"
|
||||||
DEFAULT_EMAIL: str = "changeme@email.com"
|
DEFAULT_EMAIL: str = "changeme@email.com"
|
||||||
|
|
|
@ -4,9 +4,7 @@ from mealie.core.config import settings
|
||||||
from mealie.db.models.db_session import sql_global_init
|
from mealie.db.models.db_session import sql_global_init
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
sql_exists = True
|
|
||||||
|
|
||||||
sql_exists = Path(settings.DB_URL).is_file()
|
|
||||||
SessionLocal = sql_global_init(settings.DB_URL)
|
SessionLocal = sql_global_init(settings.DB_URL)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@ from mealie.core import root_logger
|
||||||
from mealie.core.config import settings
|
from mealie.core.config import settings
|
||||||
from mealie.core.security import get_password_hash
|
from mealie.core.security import get_password_hash
|
||||||
from mealie.db.database import db
|
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.settings import SiteSettings
|
||||||
from mealie.schema.theme import SiteTheme
|
from mealie.schema.theme import SiteTheme
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
|
@ -51,7 +51,9 @@ def default_user_init(session: Session):
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if sql_exists:
|
session = create_session()
|
||||||
|
init_user = db.users.get(session, "1", "id")
|
||||||
|
if init_user:
|
||||||
print("Database Exists")
|
print("Database Exists")
|
||||||
else:
|
else:
|
||||||
print("Database Doesn't Exists, Initializing...")
|
print("Database Doesn't Exists, Initializing...")
|
||||||
|
|
|
@ -15,9 +15,11 @@ API_PORT=9000
|
||||||
API_DOCS=True
|
API_DOCS=True
|
||||||
|
|
||||||
# Sets the Database type to use.
|
# Sets the Database type to use.
|
||||||
# DB_URL
|
DB_URL=sqlite
|
||||||
DB_USERNAME=mealie
|
# POSTGRES_USER
|
||||||
DB_PASSWORD=mealie
|
# POSTGRES_PASSWORD
|
||||||
|
# POSTGRES_SERVER
|
||||||
|
# POSTGRES_DB
|
||||||
# Sets the token expiration time in hours.
|
# Sets the token expiration time in hours.
|
||||||
TOKEN_TIME=24
|
TOKEN_TIME=24
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue