Coverage for opt/mealie/lib/python3.12/site-packages/mealie/db/fixes/fix_group_with_no_name.py: 19%
34 statements
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:32 +0000
« prev ^ index » next coverage.py v7.10.6, created at 2025-11-25 15:32 +0000
1from slugify import slugify 1a
2from sqlalchemy.exc import IntegrityError 1a
3from sqlalchemy.orm import Session 1a
5from mealie.core import root_logger 1a
6from mealie.db.models.group import Group 1a
8logger = root_logger.get_logger("init_db") 1a
11def _do_fix(session: Session, group: Group, counter: int): 1a
12 if counter:
13 new_name = f"{group.id} ({counter})"
14 else:
15 new_name = str(group.id)
17 group.name = new_name
18 group.slug = slugify(group.name)
19 session.commit()
22def fix_group_with_no_name(session: Session): 1a
23 groups = session.query(Group).filter(Group.name == "").all()
24 if not groups:
25 logger.debug("No group found with an empty name; skipping fix")
26 return
28 logger.info(
29 f"{len(groups)} {'group' if len(groups) == 1 else 'groups'} found with a missing name; applying default name"
30 )
32 offset = 0
33 for i, group in enumerate(groups):
34 attempts = 0
35 while True:
36 if attempts >= 3:
37 raise Exception(
38 f'Unable to fix empty group name for group_id "{group.id}": too many attempts ({attempts})'
39 )
41 counter = i + offset
42 try:
43 _do_fix(session, group, counter)
44 break
45 except IntegrityError:
46 session.rollback()
47 attempts += 1
48 offset += 1
49 continue