mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 14:33:33 -07:00
run init_db before startup
This commit is contained in:
parent
262f7beb24
commit
8e22c02630
10 changed files with 48 additions and 94 deletions
4
.vscode/tasks.json
vendored
4
.vscode/tasks.json
vendored
|
@ -35,7 +35,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "Dev: Start Frontend",
|
"label": "Dev: Start Frontend",
|
||||||
"command": "make vue",
|
"command": "make frontend",
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"presentation": {
|
"presentation": {
|
||||||
"reveal": "always",
|
"reveal": "always",
|
||||||
|
@ -45,7 +45,7 @@
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"label": "Dev: Start Docs Server",
|
"label": "Dev: Start Docs Server",
|
||||||
"command": "make mdocs",
|
"command": "make docs",
|
||||||
"type": "shell",
|
"type": "shell",
|
||||||
"presentation": {
|
"presentation": {
|
||||||
"reveal": "always",
|
"reveal": "always",
|
||||||
|
|
|
@ -31,8 +31,9 @@ RUN apk add --update --no-cache --virtual .build-deps \
|
||||||
|
|
||||||
|
|
||||||
COPY ./mealie /app/mealie
|
COPY ./mealie /app/mealie
|
||||||
|
RUN poetry install --no-dev
|
||||||
COPY ./Caddyfile /app
|
COPY ./Caddyfile /app
|
||||||
COPY ./app_data/templates /app/data/templates
|
COPY ./dev/data/templates /app/data/templates
|
||||||
COPY --from=build-stage /app/dist /app/dist
|
COPY --from=build-stage /app/dist /app/dist
|
||||||
|
|
||||||
VOLUME [ "/app/data/" ]
|
VOLUME [ "/app/data/" ]
|
||||||
|
|
10
makefile
10
makefile
|
@ -5,12 +5,16 @@ setup:
|
||||||
cd ..
|
cd ..
|
||||||
|
|
||||||
backend:
|
backend:
|
||||||
source ./.venv/bin/activate && python mealie/app.py
|
source ./.venv/bin/activate && \
|
||||||
|
python mealie/db/init_db.py && \
|
||||||
|
python mealie/app.py
|
||||||
|
|
||||||
vue:
|
.PHONY: frontend
|
||||||
|
frontend:
|
||||||
cd frontend && npm run serve
|
cd frontend && npm run serve
|
||||||
|
|
||||||
mdocs:
|
.PHONY: docs
|
||||||
|
docs:
|
||||||
source ./.venv/bin/activate && \
|
source ./.venv/bin/activate && \
|
||||||
cd docs && \
|
cd docs && \
|
||||||
mkdocs serve
|
mkdocs serve
|
||||||
|
|
|
@ -4,23 +4,10 @@ from fastapi.logger import logger
|
||||||
|
|
||||||
# import utils.startup as startup
|
# import utils.startup as startup
|
||||||
from mealie.core.config import APP_VERSION, PORT, docs_url, redoc_url
|
from mealie.core.config import APP_VERSION, PORT, docs_url, redoc_url
|
||||||
from mealie.db.db_setup import sql_exists
|
from mealie.routes import backup_routes, debug_routes, migration_routes, setting_routes, theme_routes
|
||||||
from mealie.db.init_db import init_db
|
|
||||||
from mealie.routes import (
|
|
||||||
backup_routes,
|
|
||||||
debug_routes,
|
|
||||||
migration_routes,
|
|
||||||
setting_routes,
|
|
||||||
theme_routes,
|
|
||||||
)
|
|
||||||
from mealie.routes.groups import groups
|
from mealie.routes.groups import groups
|
||||||
from mealie.routes.mealplans import mealplans
|
from mealie.routes.mealplans import mealplans
|
||||||
from mealie.routes.recipe import (
|
from mealie.routes.recipe import all_recipe_routes, category_routes, recipe_crud_routes, tag_routes
|
||||||
all_recipe_routes,
|
|
||||||
category_routes,
|
|
||||||
recipe_crud_routes,
|
|
||||||
tag_routes,
|
|
||||||
)
|
|
||||||
from mealie.routes.users import users
|
from mealie.routes.users import users
|
||||||
|
|
||||||
app = FastAPI(
|
app = FastAPI(
|
||||||
|
@ -32,10 +19,6 @@ app = FastAPI(
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def data_base_first_run():
|
|
||||||
init_db()
|
|
||||||
|
|
||||||
|
|
||||||
def start_scheduler():
|
def start_scheduler():
|
||||||
import mealie.services.scheduler.scheduled_jobs
|
import mealie.services.scheduler.scheduled_jobs
|
||||||
|
|
||||||
|
@ -62,9 +45,6 @@ def api_routers():
|
||||||
app.include_router(debug_routes.router)
|
app.include_router(debug_routes.router)
|
||||||
|
|
||||||
|
|
||||||
if not sql_exists:
|
|
||||||
data_base_first_run()
|
|
||||||
|
|
||||||
api_routers()
|
api_routers()
|
||||||
start_scheduler()
|
start_scheduler()
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,3 @@
|
||||||
from mealie.schema.category import RecipeCategoryResponse, RecipeTagResponse
|
|
||||||
from mealie.schema.meal import MealPlanInDB
|
|
||||||
from mealie.schema.recipe import Recipe
|
|
||||||
from mealie.schema.settings import SiteSettings as SiteSettingsSchema
|
|
||||||
from mealie.schema.sign_up import SignUpOut
|
|
||||||
from mealie.schema.theme import SiteTheme
|
|
||||||
from mealie.schema.user import GroupInDB, UserInDB
|
|
||||||
from sqlalchemy.orm import load_only
|
|
||||||
from sqlalchemy.orm.session import Session
|
|
||||||
|
|
||||||
from mealie.db.db_base import BaseDocument
|
from mealie.db.db_base import BaseDocument
|
||||||
from mealie.db.models.group import Group
|
from mealie.db.models.group import Group
|
||||||
from mealie.db.models.mealplan import MealPlanModel
|
from mealie.db.models.mealplan import MealPlanModel
|
||||||
|
@ -16,6 +6,14 @@ from mealie.db.models.settings import SiteSettings
|
||||||
from mealie.db.models.sign_up import SignUp
|
from mealie.db.models.sign_up import SignUp
|
||||||
from mealie.db.models.theme import SiteThemeModel
|
from mealie.db.models.theme import SiteThemeModel
|
||||||
from mealie.db.models.users import User
|
from mealie.db.models.users import User
|
||||||
|
from mealie.schema.category import RecipeCategoryResponse, RecipeTagResponse
|
||||||
|
from mealie.schema.meal import MealPlanInDB
|
||||||
|
from mealie.schema.recipe import Recipe
|
||||||
|
from mealie.schema.settings import SiteSettings as SiteSettingsSchema
|
||||||
|
from mealie.schema.sign_up import SignUpOut
|
||||||
|
from mealie.schema.theme import SiteTheme
|
||||||
|
from mealie.schema.user import GroupInDB, UserInDB
|
||||||
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
|
|
||||||
class _Recipes(BaseDocument):
|
class _Recipes(BaseDocument):
|
||||||
|
|
|
@ -1,14 +1,13 @@
|
||||||
|
from fastapi.logger import logger
|
||||||
from mealie.core.config import DEFAULT_GROUP
|
from mealie.core.config import DEFAULT_GROUP
|
||||||
from mealie.core.security import get_password_hash
|
from mealie.core.security import get_password_hash
|
||||||
from fastapi.logger import logger
|
from mealie.db.database import db
|
||||||
|
from mealie.db.db_setup import create_session, sql_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
|
||||||
from sqlalchemy.orm.session import Session
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
from mealie.db.database import db
|
|
||||||
from mealie.db.db_setup import create_session
|
|
||||||
|
|
||||||
|
|
||||||
def init_db(db: Session = None) -> None:
|
def init_db(db: Session = None) -> None:
|
||||||
if not db:
|
if not db:
|
||||||
|
@ -55,3 +54,12 @@ def default_user_init(session: Session):
|
||||||
|
|
||||||
logger.info("Generating Default User")
|
logger.info("Generating Default User")
|
||||||
db.users.create(session, default_user)
|
db.users.create(session, default_user)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
if sql_exists:
|
||||||
|
print("Database Exists")
|
||||||
|
exit()
|
||||||
|
else:
|
||||||
|
print("Database Doesn't Exists, Initializing...")
|
||||||
|
init_db()
|
7
mealie/run.sh
Normal file → Executable file
7
mealie/run.sh
Normal file → Executable file
|
@ -1,12 +1,13 @@
|
||||||
#!/bin/sh
|
#!/bin/sh
|
||||||
|
|
||||||
|
# Initialize Database Prerun
|
||||||
|
python mealie/db/init_db.py
|
||||||
|
|
||||||
## Migrations
|
## Migrations
|
||||||
# TODO
|
# TODO
|
||||||
|
|
||||||
# Database Init
|
|
||||||
|
|
||||||
## Web Server
|
## Web Server
|
||||||
caddy start --config ./Caddyfile
|
caddy start --config ./Caddyfile
|
||||||
|
|
||||||
## Start API
|
# Start API
|
||||||
uvicorn mealie.app:app --host 0.0.0.0 --port 9000
|
uvicorn mealie.app:app --host 0.0.0.0 --port 9000
|
|
@ -2,13 +2,7 @@ import json
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from tests.test_routes.utils.routes_data import recipe_test_data
|
from tests.test_routes.utils.routes_data import recipe_test_data
|
||||||
from tests.utils.routes import (
|
from tests.utils.routes import MEALPLAN_ALL, MEALPLAN_CREATE, MEALPLAN_PREFIX, RECIPES_CREATE_URL, RECIPES_PREFIX
|
||||||
MEALPLAN_ALL,
|
|
||||||
MEALPLAN_CREATE,
|
|
||||||
MEALPLAN_PREFIX,
|
|
||||||
RECIPES_CREATE_URL,
|
|
||||||
RECIPES_PREFIX,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_meal_plan_template(first=None, second=None):
|
def get_meal_plan_template(first=None, second=None):
|
||||||
|
@ -90,9 +84,7 @@ def test_update_mealplan(api_client, slug_1, slug_2, token):
|
||||||
existing_mealplan["meals"][0]["slug"] = slug_2
|
existing_mealplan["meals"][0]["slug"] = slug_2
|
||||||
existing_mealplan["meals"][1]["slug"] = slug_1
|
existing_mealplan["meals"][1]["slug"] = slug_1
|
||||||
|
|
||||||
response = api_client.put(
|
response = api_client.put(f"{MEALPLAN_PREFIX}/{plan_uid}", json=existing_mealplan, headers=token)
|
||||||
f"{MEALPLAN_PREFIX}/{plan_uid}", json=existing_mealplan, headers=token
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
|
@ -2,11 +2,8 @@ import json
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from slugify import slugify
|
from slugify import slugify
|
||||||
from tests.test_routes.utils.routes_data import (RecipeTestData, raw_recipe,
|
from tests.test_routes.utils.routes_data import RecipeTestData, raw_recipe, raw_recipe_no_image, recipe_test_data
|
||||||
raw_recipe_no_image,
|
from tests.utils.routes import RECIPES_ALL, RECIPES_CREATE, RECIPES_CREATE_URL, RECIPES_PREFIX
|
||||||
recipe_test_data)
|
|
||||||
from tests.utils.routes import (RECIPES_ALL, RECIPES_CREATE,
|
|
||||||
RECIPES_CREATE_URL, RECIPES_PREFIX)
|
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize("recipe_data", recipe_test_data)
|
@pytest.mark.parametrize("recipe_data", recipe_test_data)
|
||||||
|
@ -43,9 +40,7 @@ def test_create_no_image(api_client):
|
||||||
|
|
||||||
|
|
||||||
def test_read_all_post(api_client):
|
def test_read_all_post(api_client):
|
||||||
response = api_client.post(
|
response = api_client.post(RECIPES_ALL, json={"properties": ["slug", "description", "rating"]})
|
||||||
RECIPES_ALL, json={"properties": ["slug", "description", "rating"]}
|
|
||||||
)
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
|
@ -65,9 +60,7 @@ def test_read_update(api_client, recipe_data):
|
||||||
test_categories = ["one", "two", "three"]
|
test_categories = ["one", "two", "three"]
|
||||||
recipe["recipeCategory"] = test_categories
|
recipe["recipeCategory"] = test_categories
|
||||||
|
|
||||||
response = api_client.put(
|
response = api_client.put(f"{RECIPES_PREFIX}/{recipe_data.expected_slug}", json=recipe)
|
||||||
f"{RECIPES_PREFIX}/{recipe_data.expected_slug}", json=recipe
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert json.loads(response.text) == recipe_data.expected_slug
|
assert json.loads(response.text) == recipe_data.expected_slug
|
||||||
|
@ -90,9 +83,7 @@ def test_rename(api_client, recipe_data):
|
||||||
new_slug = slugify(new_name)
|
new_slug = slugify(new_name)
|
||||||
recipe["name"] = new_name
|
recipe["name"] = new_name
|
||||||
|
|
||||||
response = api_client.put(
|
response = api_client.put(f"{RECIPES_PREFIX}/{recipe_data.expected_slug}", json=recipe)
|
||||||
f"{RECIPES_PREFIX}/{recipe_data.expected_slug}", json=recipe
|
|
||||||
)
|
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
assert json.loads(response.text) == new_slug
|
assert json.loads(response.text) == new_slug
|
||||||
|
|
|
@ -7,29 +7,14 @@ BASE = "/api/users"
|
||||||
TOKEN_URL = "/api/auth/token"
|
TOKEN_URL = "/api/auth/token"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope="session")
|
@fixture(scope="session")
|
||||||
def default_user():
|
def default_user():
|
||||||
return {
|
return {"id": 1, "fullName": "Change Me", "email": "changeme@email.com", "group": "Home", "admin": True}
|
||||||
"id": 1,
|
|
||||||
"fullName": "Change Me",
|
|
||||||
"email": "changeme@email.com",
|
|
||||||
"group": "Home",
|
|
||||||
"admin": True
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@fixture(scope="session")
|
@fixture(scope="session")
|
||||||
def new_user():
|
def new_user():
|
||||||
return {
|
return {"id": 2, "fullName": "My New User", "email": "newuser@email.com", "group": "Home", "admin": False}
|
||||||
"id": 2,
|
|
||||||
"fullName": "My New User",
|
|
||||||
"email": "newuser@email.com",
|
|
||||||
"group": "Home",
|
|
||||||
"admin": False
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def test_superuser_login(api_client: requests):
|
def test_superuser_login(api_client: requests):
|
||||||
|
@ -55,7 +40,7 @@ def test_create_user(api_client: requests, token, new_user):
|
||||||
"email": "newuser@email.com",
|
"email": "newuser@email.com",
|
||||||
"password": "MyStrongPassword",
|
"password": "MyStrongPassword",
|
||||||
"group": "Home",
|
"group": "Home",
|
||||||
"admin": False
|
"admin": False,
|
||||||
}
|
}
|
||||||
|
|
||||||
response = api_client.post(f"{BASE}", json=create_data, headers=token)
|
response = api_client.post(f"{BASE}", json=create_data, headers=token)
|
||||||
|
@ -74,13 +59,7 @@ def test_get_all_users(api_client: requests, token, new_user, default_user):
|
||||||
|
|
||||||
|
|
||||||
def test_update_user(api_client: requests, token):
|
def test_update_user(api_client: requests, token):
|
||||||
update_data = {
|
update_data = {"id": 1, "fullName": "Updated Name", "email": "updated@email.com", "group": "Home", "admin": True}
|
||||||
"id": 1,
|
|
||||||
"fullName": "Updated Name",
|
|
||||||
"email": "updated@email.com",
|
|
||||||
"group": "Home",
|
|
||||||
"admin": True
|
|
||||||
}
|
|
||||||
response = api_client.put(f"{BASE}/1", headers=token, json=update_data)
|
response = api_client.put(f"{BASE}/1", headers=token, json=update_data)
|
||||||
|
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue