Coverage for opt/mealie/lib/python3.12/site-packages/mealie/alembic/versions/2025-02-09-15.31.00_7cf3054cbbcc_remove_instructions_index.py: 77%
84 statements
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
« prev ^ index » next coverage.py v7.15.2, created at 2026-10-07 03:04 +0000
1"""remove instructions index
3Revision ID: 7cf3054cbbcc
4Revises: b9e516e2d3b3
5Create Date: 2025-02-09 15:31:00.772295
7"""
9import sqlalchemy as sa
10from sqlalchemy import orm
11from alembic import op
12from mealie.db.models._model_utils.guid import GUID
13from mealie.core.root_logger import get_logger
15# revision identifiers, used by Alembic.
16revision = "7cf3054cbbcc"
17down_revision: str | None = "b9e516e2d3b3"
18branch_labels: str | tuple[str, ...] | None = None
19depends_on: str | tuple[str, ...] | None = None
21logger = get_logger()
24class SqlAlchemyBase(orm.DeclarativeBase):
25 @classmethod
26 def normalized_fields(cls) -> list[orm.InstrumentedAttribute]:
27 return []
30class RecipeModel(SqlAlchemyBase):
31 __tablename__ = "recipes"
33 id: orm.Mapped[GUID] = orm.mapped_column(GUID, primary_key=True, default=GUID.generate)
34 name_normalized: orm.Mapped[str] = orm.mapped_column(sa.String, nullable=False, index=True)
35 description_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
37 @classmethod
38 def normalized_fields(cls):
39 return [cls.name_normalized, cls.description_normalized]
42class RecipeIngredientModel(SqlAlchemyBase):
43 __tablename__ = "recipes_ingredients"
45 id: orm.Mapped[int] = orm.mapped_column(sa.Integer, primary_key=True)
46 note_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
47 original_text_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
49 @classmethod
50 def normalized_fields(cls):
51 return [cls.note_normalized, cls.original_text_normalized]
54class IngredientFoodModel(SqlAlchemyBase):
55 __tablename__ = "ingredient_foods"
56 id: orm.Mapped[GUID] = orm.mapped_column(GUID, primary_key=True, default=GUID.generate)
57 name_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
58 plural_name_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
60 @classmethod
61 def normalized_fields(cls):
62 return [cls.name_normalized, cls.plural_name_normalized]
65class IngredientFoodAliasModel(SqlAlchemyBase):
66 __tablename__ = "ingredient_foods_aliases"
67 id: orm.Mapped[GUID] = orm.mapped_column(GUID, primary_key=True, default=GUID.generate)
68 name_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
70 @classmethod
71 def normalized_fields(cls):
72 return [cls.name_normalized]
75class IngredientUnitModel(SqlAlchemyBase):
76 __tablename__ = "ingredient_units"
77 id: orm.Mapped[GUID] = orm.mapped_column(GUID, primary_key=True, default=GUID.generate)
78 name_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
79 plural_name_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
80 abbreviation_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
81 plural_abbreviation_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
83 @classmethod
84 def normalized_fields(cls):
85 return [
86 cls.name_normalized,
87 cls.plural_name_normalized,
88 cls.abbreviation_normalized,
89 cls.plural_abbreviation_normalized,
90 ]
93class IngredientUnitAliasModel(SqlAlchemyBase):
94 __tablename__ = "ingredient_units_aliases"
95 id: orm.Mapped[GUID] = orm.mapped_column(GUID, primary_key=True, default=GUID.generate)
96 name_normalized: orm.Mapped[str | None] = orm.mapped_column(sa.String, index=True)
98 @classmethod
99 def normalized_fields(cls):
100 return [cls.name_normalized]
103def truncate_normalized_fields() -> None:
104 bind = op.get_bind()
105 session = orm.Session(bind=bind)
107 models: list[type[SqlAlchemyBase]] = [
108 RecipeModel,
109 RecipeIngredientModel,
110 IngredientFoodModel,
111 IngredientFoodAliasModel,
112 IngredientUnitModel,
113 IngredientUnitAliasModel,
114 ]
116 for model in models:
117 for record in session.query(model).all(): 117 ↛ 118line 117 didn't jump to line 118 because the loop on line 117 never started
118 for field in model.normalized_fields():
119 if not (field_value := getattr(record, field.key)):
120 continue
122 setattr(record, field.key, field_value[:255])
124 try:
125 session.commit()
126 except Exception:
127 logger.exception(f"Failed to truncate normalized fields for {model.__name__}")
128 session.rollback()
131def upgrade():
132 # ### commands auto generated by Alembic - please adjust! ###
133 with op.batch_alter_table("recipe_instructions", schema=None) as batch_op:
134 batch_op.drop_index("ix_recipe_instructions_text")
136 # ### end Alembic commands ###
138 truncate_normalized_fields()
141def downgrade():
142 # ### commands auto generated by Alembic - please adjust! ###
143 with op.batch_alter_table("recipe_instructions", schema=None) as batch_op:
144 batch_op.create_index("ix_recipe_instructions_text", ["text"], unique=False)
146 # ### end Alembic commands ###