mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-12-28 13:05:26 -05:00
Fix/fix slug names (#1026)
* fix food seeder to use value instead of keys * fix all recipe names being slugified * add helper path utilities * add fix script for database to rename foods * add safe calling for fixes
This commit is contained in:
27
mealie/db/fixes/fix_slug_foods.py
Normal file
27
mealie/db/fixes/fix_slug_foods.py
Normal file
@@ -0,0 +1,27 @@
|
||||
import json
|
||||
|
||||
from mealie.core import root_logger
|
||||
from mealie.repos.repository_factory import AllRepositories
|
||||
from mealie.repos.seed.resources import foods as food_resources
|
||||
|
||||
|
||||
def fix_slug_food_names(db: AllRepositories):
|
||||
check_for_food = "dairy-products-and-dairy-substitutes"
|
||||
|
||||
food = db.ingredient_foods.get_one(check_for_food, "name")
|
||||
|
||||
logger = root_logger.get_logger("init_db")
|
||||
|
||||
if not food:
|
||||
logger.info("No food found with slug: '{}' skipping fix".format(check_for_food))
|
||||
return
|
||||
|
||||
all_foods = db.ingredient_foods.get_all()
|
||||
|
||||
seed_foods: dict[str, str] = json.loads(food_resources.en_us.read_text())
|
||||
|
||||
for food in all_foods:
|
||||
if food.name in seed_foods:
|
||||
food.name = seed_foods[food.name]
|
||||
logger.info("Updating food: {}".format(food.name))
|
||||
db.ingredient_foods.update(food.id, food)
|
||||
@@ -1,4 +1,5 @@
|
||||
from pathlib import Path
|
||||
from typing import Callable
|
||||
|
||||
from sqlalchemy import engine
|
||||
|
||||
@@ -8,6 +9,7 @@ from alembic.runtime import migration
|
||||
from mealie.core import root_logger
|
||||
from mealie.core.config import get_app_settings
|
||||
from mealie.db.db_setup import create_session
|
||||
from mealie.db.fixes.fix_slug_foods import fix_slug_food_names
|
||||
from mealie.repos.all_repositories import get_repositories
|
||||
from mealie.repos.repository_factory import AllRepositories
|
||||
from mealie.repos.seed.init_users import default_user_init
|
||||
@@ -55,6 +57,13 @@ def db_is_at_head(alembic_cfg: config.Config) -> bool:
|
||||
return set(context.get_current_heads()) == set(directory.get_heads())
|
||||
|
||||
|
||||
def safe_try(name: str, func: Callable):
|
||||
try:
|
||||
func()
|
||||
except Exception as e:
|
||||
logger.error(f"Error calling '{name}': {e}")
|
||||
|
||||
|
||||
def main():
|
||||
alembic_cfg = Config(str(PROJECT_DIR / "alembic.ini"))
|
||||
if db_is_at_head(alembic_cfg):
|
||||
@@ -73,6 +82,8 @@ def main():
|
||||
logger.info("Database contains no users, initializing...")
|
||||
init_db(db)
|
||||
|
||||
safe_try("fix slug food names", lambda: fix_slug_food_names(db))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user