mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-07-09 03:04:54 -04:00
fix: Prevent Creating Groups With No Name (#2803)
* prevent creating groups with no name
* add db fix fro groups with no name
* moved non-actionable fix logs to debug level
* 🧹
* use id as default name to avoid collisions
* simplified group name constraint
* removed redundant import
This commit is contained in:
parent
c3ec875d59
commit
8311db7e60
50
mealie/db/fixes/fix_group_with_no_name.py
Normal file
50
mealie/db/fixes/fix_group_with_no_name.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from slugify import slugify
|
||||||
|
from sqlalchemy.exc import IntegrityError
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
|
||||||
|
from mealie.core import root_logger
|
||||||
|
from mealie.db.models.group import Group
|
||||||
|
|
||||||
|
logger = root_logger.get_logger("init_db")
|
||||||
|
|
||||||
|
|
||||||
|
def _do_fix(session: Session, group: Group, counter: int):
|
||||||
|
if counter:
|
||||||
|
new_name = f"{group.id} ({counter})"
|
||||||
|
else:
|
||||||
|
new_name = str(group.id)
|
||||||
|
|
||||||
|
group.name = new_name
|
||||||
|
group.slug = slugify(group.name)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
|
||||||
|
def fix_group_with_no_name(session: Session):
|
||||||
|
groups = session.query(Group).filter(Group.name == "").all()
|
||||||
|
if not groups:
|
||||||
|
logger.debug("No group found with an empty name; skipping fix")
|
||||||
|
return
|
||||||
|
|
||||||
|
logger.info(
|
||||||
|
f'{len(groups)} {"group" if len(groups) == 1 else "groups"} found with a missing name; '
|
||||||
|
f"applying default name"
|
||||||
|
)
|
||||||
|
|
||||||
|
offset = 0
|
||||||
|
for i, group in enumerate(groups):
|
||||||
|
attempts = 0
|
||||||
|
while True:
|
||||||
|
if attempts >= 3:
|
||||||
|
raise Exception(
|
||||||
|
f'Unable to fix empty group name for group_id "{group.id}": too many attempts ({attempts})'
|
||||||
|
)
|
||||||
|
|
||||||
|
counter = i + offset
|
||||||
|
try:
|
||||||
|
_do_fix(session, group, counter)
|
||||||
|
break
|
||||||
|
except IntegrityError:
|
||||||
|
session.rollback()
|
||||||
|
attempts += 1
|
||||||
|
offset += 1
|
||||||
|
continue
|
@ -13,7 +13,7 @@ def fix_slug_food_names(db: AllRepositories):
|
|||||||
logger = root_logger.get_logger("init_db")
|
logger = root_logger.get_logger("init_db")
|
||||||
|
|
||||||
if not food:
|
if not food:
|
||||||
logger.info(f"No food found with slug: '{check_for_food}' skipping fix")
|
logger.debug(f"No food found with slug: '{check_for_food}' skipping fix")
|
||||||
return
|
return
|
||||||
|
|
||||||
all_foods = db.ingredient_foods.get_all()
|
all_foods = db.ingredient_foods.get_all()
|
||||||
|
@ -10,6 +10,7 @@ from alembic.runtime import migration
|
|||||||
from mealie.core import root_logger
|
from mealie.core import root_logger
|
||||||
from mealie.core.config import get_app_settings
|
from mealie.core.config import get_app_settings
|
||||||
from mealie.db.db_setup import session_context
|
from mealie.db.db_setup import session_context
|
||||||
|
from mealie.db.fixes.fix_group_with_no_name import fix_group_with_no_name
|
||||||
from mealie.db.fixes.fix_slug_foods import fix_slug_food_names
|
from mealie.db.fixes.fix_slug_foods import fix_slug_food_names
|
||||||
from mealie.repos.all_repositories import get_repositories
|
from mealie.repos.all_repositories import get_repositories
|
||||||
from mealie.repos.repository_factory import AllRepositories
|
from mealie.repos.repository_factory import AllRepositories
|
||||||
@ -104,6 +105,7 @@ def main():
|
|||||||
init_db(db)
|
init_db(db)
|
||||||
|
|
||||||
safe_try(lambda: fix_slug_food_names(db))
|
safe_try(lambda: fix_slug_food_names(db))
|
||||||
|
safe_try(lambda: fix_group_with_no_name(session))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -61,7 +61,7 @@ class ChangePassword(MealieModel):
|
|||||||
|
|
||||||
|
|
||||||
class GroupBase(MealieModel):
|
class GroupBase(MealieModel):
|
||||||
name: str
|
name: constr(strip_whitespace=True, min_length=1) # type: ignore
|
||||||
|
|
||||||
class Config:
|
class Config:
|
||||||
orm_mode = True
|
orm_mode = True
|
||||||
|
Loading…
x
Reference in New Issue
Block a user