Coverage for opt/mealie/lib/python3.12/site-packages/mealie/alembic/versions/2023-08-06-21.00.34_04ac51cbe9a4_added_group_slug.py: 57%

33 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-11-25 17:29 +0000

1"""added group slug 

2 

3Revision ID: 04ac51cbe9a4 

4Revises: b3dbb554ba53 

5Create Date: 2023-08-06 21:00:34.582905 

6 

7""" 

8 

9import sqlalchemy as sa 1a

10from slugify import slugify 1a

11from sqlalchemy.orm import Session 1a

12 

13from alembic import op 1a

14from mealie.db.models.group.group import Group 1a

15 

16# revision identifiers, used by Alembic. 

17revision = "04ac51cbe9a4" 1a

18down_revision = "b3dbb554ba53" 1a

19branch_labels: str | tuple[str, ...] | None = None 1a

20depends_on: str | tuple[str, ...] | None = None 1a

21 

22 

23def populate_group_slugs(session: Session): 1a

24 groups: list[Group] = session.query(Group).all() 1a

25 seen_slugs: set[str] = set() 1a

26 for group in groups: 26 ↛ 27line 26 didn't jump to line 27 because the loop on line 26 never started1a

27 original_name = group.name 

28 new_name = original_name 

29 attempts = 0 

30 while True: 

31 slug = slugify(new_name) 

32 if slug not in seen_slugs: 

33 break 

34 

35 attempts += 1 

36 new_name = f"{original_name} ({attempts})" 

37 

38 seen_slugs.add(slug) 

39 session.execute( 

40 sa.text(f"UPDATE {Group.__tablename__} SET name=:name, slug=:slug WHERE id=:id").bindparams( 

41 name=new_name, slug=slug, id=group.id 

42 ) 

43 ) 

44 

45 session.commit() 1a

46 

47 

48def upgrade(): 1a

49 # ### commands auto generated by Alembic - please adjust! ### 

50 op.add_column("groups", sa.Column("slug", sa.String(), nullable=True)) 1a

51 op.create_index(op.f("ix_groups_slug"), "groups", ["slug"], unique=True) 1a

52 # ### end Alembic commands ### 

53 

54 session = Session(bind=op.get_bind()) 1a

55 populate_group_slugs(session) 1a

56 

57 

58def downgrade(): 1a

59 # ### commands auto generated by Alembic - please adjust! ### 

60 op.drop_index(op.f("ix_groups_slug"), table_name="groups") 

61 op.drop_column("groups", "slug") 

62 # ### end Alembic commands ###