Coverage for opt/mealie/lib/python3.12/site-packages/mealie/alembic/versions/2024-06-22-10.17.03_32d69327997b_add_staple_flag_to_foods.py: 85%

24 statements  

« prev     ^ index     » next       coverage.py v7.10.6, created at 2025-12-05 14:03 +0000

1"""Add staple flag to foods 

2 

3Revision ID: 32d69327997b 

4Revises: 7788478a0338 

5Create Date: 2024-06-22 10:17:03.323966 

6 

7""" 

8 

9import sqlalchemy as sa 1a

10from alembic import op 1a

11from sqlalchemy import orm 1a

12 

13# revision identifiers, used by Alembic. 

14revision = "32d69327997b" 1a

15down_revision = "7788478a0338" 1a

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

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

18 

19 

20def is_postgres(): 1a

21 return op.get_context().dialect.name == "postgresql" 1a

22 

23 

24def upgrade(): 1a

25 with op.batch_alter_table("ingredient_foods") as batch_op: 1a

26 batch_op.add_column(sa.Column("on_hand", sa.Boolean(), nullable=True, default=False)) 1a

27 

28 bind = op.get_bind() 1a

29 session = orm.Session(bind=bind) 1a

30 

31 with session: 1a

32 if is_postgres(): 32 ↛ 33line 32 didn't jump to line 33 because the condition on line 32 was never true1a

33 stmt = "UPDATE ingredient_foods SET on_hand = FALSE;" 

34 else: 

35 stmt = "UPDATE ingredient_foods SET on_hand = 0;" 1a

36 

37 session.execute(sa.text(stmt)) 1a

38 

39 # forbid nulls after migration 

40 with op.batch_alter_table("ingredient_foods") as batch_op: 1a

41 batch_op.alter_column("on_hand", nullable=False) 1a

42 

43 

44def downgrade(): 1a

45 with op.batch_alter_table("ingredient_foods") as batch_op: 

46 batch_op.drop_column("on_hand")