From 8e22c0263052e35bde72f6cb81fa32e5dd798816 Mon Sep 17 00:00:00 2001 From: hay-kot Date: Sat, 20 Mar 2021 16:23:31 -0800 Subject: [PATCH] run init_db before startup --- .vscode/tasks.json | 4 ++-- Dockerfile | 3 ++- makefile | 10 ++++++--- mealie/app.py | 24 ++------------------ mealie/db/database.py | 18 +++++++-------- mealie/db/init_db.py | 16 ++++++++++---- mealie/run.sh | 7 +++--- tests/test_routes/test_meal_routes.py | 12 ++-------- tests/test_routes/test_recipe_routes.py | 19 +++++----------- tests/test_routes/test_user_routes.py | 29 ++++--------------------- 10 files changed, 48 insertions(+), 94 deletions(-) mode change 100644 => 100755 mealie/run.sh diff --git a/.vscode/tasks.json b/.vscode/tasks.json index df56f017d..9b7582bd1 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -35,7 +35,7 @@ }, { "label": "Dev: Start Frontend", - "command": "make vue", + "command": "make frontend", "type": "shell", "presentation": { "reveal": "always", @@ -45,7 +45,7 @@ }, { "label": "Dev: Start Docs Server", - "command": "make mdocs", + "command": "make docs", "type": "shell", "presentation": { "reveal": "always", diff --git a/Dockerfile b/Dockerfile index 42ebf796c..4b6df2a0c 100644 --- a/Dockerfile +++ b/Dockerfile @@ -31,8 +31,9 @@ RUN apk add --update --no-cache --virtual .build-deps \ COPY ./mealie /app/mealie +RUN poetry install --no-dev 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 VOLUME [ "/app/data/" ] diff --git a/makefile b/makefile index c4c75b277..6fbdc0d45 100644 --- a/makefile +++ b/makefile @@ -5,12 +5,16 @@ setup: cd .. 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 -mdocs: +.PHONY: docs +docs: source ./.venv/bin/activate && \ cd docs && \ mkdocs serve diff --git a/mealie/app.py b/mealie/app.py index e704e0164..69113cdfd 100644 --- a/mealie/app.py +++ b/mealie/app.py @@ -4,23 +4,10 @@ from fastapi.logger import logger # import utils.startup as startup from mealie.core.config import APP_VERSION, PORT, docs_url, redoc_url -from mealie.db.db_setup import sql_exists -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 import backup_routes, debug_routes, migration_routes, setting_routes, theme_routes from mealie.routes.groups import groups from mealie.routes.mealplans import mealplans -from mealie.routes.recipe import ( - all_recipe_routes, - category_routes, - recipe_crud_routes, - tag_routes, -) +from mealie.routes.recipe import all_recipe_routes, category_routes, recipe_crud_routes, tag_routes from mealie.routes.users import users app = FastAPI( @@ -32,10 +19,6 @@ app = FastAPI( ) -def data_base_first_run(): - init_db() - - def start_scheduler(): import mealie.services.scheduler.scheduled_jobs @@ -62,9 +45,6 @@ def api_routers(): app.include_router(debug_routes.router) -if not sql_exists: - data_base_first_run() - api_routers() start_scheduler() diff --git a/mealie/db/database.py b/mealie/db/database.py index 0b8a5d335..a519ed309 100644 --- a/mealie/db/database.py +++ b/mealie/db/database.py @@ -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.models.group import Group 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.theme import SiteThemeModel 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): diff --git a/mealie/db/init_db.py b/mealie/db/init_db.py index 6a2bb0b4e..6108d60fd 100644 --- a/mealie/db/init_db.py +++ b/mealie/db/init_db.py @@ -1,14 +1,13 @@ +from fastapi.logger import logger from mealie.core.config import DEFAULT_GROUP 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.theme import SiteTheme from sqlalchemy.orm 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: if not db: @@ -55,3 +54,12 @@ def default_user_init(session: Session): logger.info("Generating 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() \ No newline at end of file diff --git a/mealie/run.sh b/mealie/run.sh old mode 100644 new mode 100755 index f85e29e6a..5e51291a0 --- a/mealie/run.sh +++ b/mealie/run.sh @@ -1,12 +1,13 @@ #!/bin/sh +# Initialize Database Prerun +python mealie/db/init_db.py + ## Migrations # TODO -# Database Init - ## Web Server caddy start --config ./Caddyfile -## Start API +# Start API uvicorn mealie.app:app --host 0.0.0.0 --port 9000 \ No newline at end of file diff --git a/tests/test_routes/test_meal_routes.py b/tests/test_routes/test_meal_routes.py index 950ef6741..01d044629 100644 --- a/tests/test_routes/test_meal_routes.py +++ b/tests/test_routes/test_meal_routes.py @@ -2,13 +2,7 @@ import json import pytest from tests.test_routes.utils.routes_data import recipe_test_data -from tests.utils.routes import ( - MEALPLAN_ALL, - MEALPLAN_CREATE, - MEALPLAN_PREFIX, - RECIPES_CREATE_URL, - RECIPES_PREFIX, -) +from tests.utils.routes import MEALPLAN_ALL, MEALPLAN_CREATE, MEALPLAN_PREFIX, RECIPES_CREATE_URL, RECIPES_PREFIX 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"][1]["slug"] = slug_1 - response = api_client.put( - f"{MEALPLAN_PREFIX}/{plan_uid}", json=existing_mealplan, headers=token - ) + response = api_client.put(f"{MEALPLAN_PREFIX}/{plan_uid}", json=existing_mealplan, headers=token) assert response.status_code == 200 diff --git a/tests/test_routes/test_recipe_routes.py b/tests/test_routes/test_recipe_routes.py index adc25867d..2a0007537 100644 --- a/tests/test_routes/test_recipe_routes.py +++ b/tests/test_routes/test_recipe_routes.py @@ -2,11 +2,8 @@ import json import pytest from slugify import slugify -from tests.test_routes.utils.routes_data import (RecipeTestData, raw_recipe, - raw_recipe_no_image, - recipe_test_data) -from tests.utils.routes import (RECIPES_ALL, RECIPES_CREATE, - RECIPES_CREATE_URL, RECIPES_PREFIX) +from tests.test_routes.utils.routes_data import RecipeTestData, raw_recipe, raw_recipe_no_image, 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) @@ -43,9 +40,7 @@ def test_create_no_image(api_client): def test_read_all_post(api_client): - response = api_client.post( - RECIPES_ALL, json={"properties": ["slug", "description", "rating"]} - ) + response = api_client.post(RECIPES_ALL, json={"properties": ["slug", "description", "rating"]}) assert response.status_code == 200 @@ -65,9 +60,7 @@ def test_read_update(api_client, recipe_data): test_categories = ["one", "two", "three"] recipe["recipeCategory"] = test_categories - response = api_client.put( - f"{RECIPES_PREFIX}/{recipe_data.expected_slug}", json=recipe - ) + response = api_client.put(f"{RECIPES_PREFIX}/{recipe_data.expected_slug}", json=recipe) assert response.status_code == 200 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) recipe["name"] = new_name - response = api_client.put( - f"{RECIPES_PREFIX}/{recipe_data.expected_slug}", json=recipe - ) + response = api_client.put(f"{RECIPES_PREFIX}/{recipe_data.expected_slug}", json=recipe) assert response.status_code == 200 assert json.loads(response.text) == new_slug diff --git a/tests/test_routes/test_user_routes.py b/tests/test_routes/test_user_routes.py index 475bde5b4..34eddd5ab 100644 --- a/tests/test_routes/test_user_routes.py +++ b/tests/test_routes/test_user_routes.py @@ -7,29 +7,14 @@ BASE = "/api/users" TOKEN_URL = "/api/auth/token" - - - @fixture(scope="session") def default_user(): - return { - "id": 1, - "fullName": "Change Me", - "email": "changeme@email.com", - "group": "Home", - "admin": True - } + return {"id": 1, "fullName": "Change Me", "email": "changeme@email.com", "group": "Home", "admin": True} @fixture(scope="session") def new_user(): - return { - "id": 2, - "fullName": "My New User", - "email": "newuser@email.com", - "group": "Home", - "admin": False - } + return {"id": 2, "fullName": "My New User", "email": "newuser@email.com", "group": "Home", "admin": False} 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", "password": "MyStrongPassword", "group": "Home", - "admin": False + "admin": False, } 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): - update_data = { - "id": 1, - "fullName": "Updated Name", - "email": "updated@email.com", - "group": "Home", - "admin": True - } + update_data = {"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) assert response.status_code == 200