feat: improve database indexing (#2104)

* add indices to all foreign keys and some fields that are used for ordering and filtering

* add missing migrations

* update migration orders

---------

Co-authored-by: Hayden <64056131+hay-kot@users.noreply.github.com>
This commit is contained in:
Sören 2023-02-11 20:40:53 +01:00 committed by GitHub
parent f9ba7711ec
commit 7d4a379f0d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 340 additions and 58 deletions

View File

@ -0,0 +1,280 @@
"""add missing foreign key and order indices
Revision ID: ff5f73b01a7a
Revises: 165d943c64ee
Create Date: 2023-02-07 20:57:21.066927
"""
from alembic import op
# revision identifiers, used by Alembic.
revision = "ff5f73b01a7a"
down_revision = "165d943c64ee"
branch_labels = None
depends_on = None
def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_index(op.f("ix_api_extras_created_at"), "api_extras", ["created_at"], unique=False)
op.create_index(op.f("ix_api_extras_recipee_id"), "api_extras", ["recipee_id"], unique=False)
op.create_index(op.f("ix_categories_created_at"), "categories", ["created_at"], unique=False)
op.create_index(op.f("ix_cookbooks_created_at"), "cookbooks", ["created_at"], unique=False)
op.create_index(op.f("ix_cookbooks_group_id"), "cookbooks", ["group_id"], unique=False)
op.create_index(op.f("ix_cookbooks_slug"), "cookbooks", ["slug"], unique=False)
op.create_index(
op.f("ix_cookbooks_to_categories_category_id"), "cookbooks_to_categories", ["category_id"], unique=False
)
op.create_index(
op.f("ix_cookbooks_to_categories_cookbook_id"), "cookbooks_to_categories", ["cookbook_id"], unique=False
)
op.create_index(op.f("ix_cookbooks_to_tags_cookbook_id"), "cookbooks_to_tags", ["cookbook_id"], unique=False)
op.create_index(op.f("ix_cookbooks_to_tags_tag_id"), "cookbooks_to_tags", ["tag_id"], unique=False)
op.create_index(op.f("ix_cookbooks_to_tools_cookbook_id"), "cookbooks_to_tools", ["cookbook_id"], unique=False)
op.create_index(op.f("ix_cookbooks_to_tools_tool_id"), "cookbooks_to_tools", ["tool_id"], unique=False)
op.create_index(op.f("ix_group_data_exports_created_at"), "group_data_exports", ["created_at"], unique=False)
op.create_index(
op.f("ix_group_events_notifier_options_created_at"),
"group_events_notifier_options",
["created_at"],
unique=False,
)
op.create_index(
op.f("ix_group_events_notifiers_created_at"), "group_events_notifiers", ["created_at"], unique=False
)
op.create_index(op.f("ix_group_meal_plan_rules_created_at"), "group_meal_plan_rules", ["created_at"], unique=False)
op.create_index(op.f("ix_group_meal_plan_rules_group_id"), "group_meal_plan_rules", ["group_id"], unique=False)
op.create_index(op.f("ix_group_meal_plans_created_at"), "group_meal_plans", ["created_at"], unique=False)
op.create_index(op.f("ix_group_preferences_created_at"), "group_preferences", ["created_at"], unique=False)
op.create_index(op.f("ix_group_reports_created_at"), "group_reports", ["created_at"], unique=False)
op.create_index(op.f("ix_group_to_categories_category_id"), "group_to_categories", ["category_id"], unique=False)
op.create_index(op.f("ix_group_to_categories_group_id"), "group_to_categories", ["group_id"], unique=False)
op.create_index(op.f("ix_groups_created_at"), "groups", ["created_at"], unique=False)
op.create_index(
op.f("ix_ingredient_food_extras_created_at"), "ingredient_food_extras", ["created_at"], unique=False
)
op.create_index(
op.f("ix_ingredient_food_extras_ingredient_food_id"),
"ingredient_food_extras",
["ingredient_food_id"],
unique=False,
)
op.create_index(op.f("ix_ingredient_foods_created_at"), "ingredient_foods", ["created_at"], unique=False)
op.create_index(op.f("ix_ingredient_foods_group_id"), "ingredient_foods", ["group_id"], unique=False)
op.create_index(op.f("ix_ingredient_foods_label_id"), "ingredient_foods", ["label_id"], unique=False)
op.create_index(op.f("ix_ingredient_units_created_at"), "ingredient_units", ["created_at"], unique=False)
op.create_index(op.f("ix_ingredient_units_group_id"), "ingredient_units", ["group_id"], unique=False)
op.create_index(op.f("ix_invite_tokens_created_at"), "invite_tokens", ["created_at"], unique=False)
op.create_index(op.f("ix_invite_tokens_group_id"), "invite_tokens", ["group_id"], unique=False)
op.create_index(op.f("ix_long_live_tokens_created_at"), "long_live_tokens", ["created_at"], unique=False)
op.create_index(op.f("ix_long_live_tokens_token"), "long_live_tokens", ["token"], unique=False)
op.create_index(op.f("ix_long_live_tokens_user_id"), "long_live_tokens", ["user_id"], unique=False)
op.create_index(op.f("ix_multi_purpose_labels_created_at"), "multi_purpose_labels", ["created_at"], unique=False)
op.create_index(op.f("ix_notes_created_at"), "notes", ["created_at"], unique=False)
op.create_index(op.f("ix_notes_recipe_id"), "notes", ["recipe_id"], unique=False)
op.create_index(op.f("ix_password_reset_tokens_created_at"), "password_reset_tokens", ["created_at"], unique=False)
op.create_index(op.f("ix_password_reset_tokens_user_id"), "password_reset_tokens", ["user_id"], unique=False)
op.create_index(
op.f("ix_plan_rules_to_categories_category_id"), "plan_rules_to_categories", ["category_id"], unique=False
)
op.create_index(
op.f("ix_plan_rules_to_categories_group_plan_rule_id"),
"plan_rules_to_categories",
["group_plan_rule_id"],
unique=False,
)
op.create_index(op.f("ix_plan_rules_to_tags_plan_rule_id"), "plan_rules_to_tags", ["plan_rule_id"], unique=False)
op.create_index(op.f("ix_plan_rules_to_tags_tag_id"), "plan_rules_to_tags", ["tag_id"], unique=False)
op.create_index(op.f("ix_recipe_assets_created_at"), "recipe_assets", ["created_at"], unique=False)
op.create_index(op.f("ix_recipe_assets_recipe_id"), "recipe_assets", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipe_comments_created_at"), "recipe_comments", ["created_at"], unique=False)
op.create_index(op.f("ix_recipe_comments_recipe_id"), "recipe_comments", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipe_comments_user_id"), "recipe_comments", ["user_id"], unique=False)
op.create_index(
op.f("ix_recipe_ingredient_ref_link_created_at"), "recipe_ingredient_ref_link", ["created_at"], unique=False
)
op.create_index(
op.f("ix_recipe_ingredient_ref_link_instruction_id"),
"recipe_ingredient_ref_link",
["instruction_id"],
unique=False,
)
op.create_index(
op.f("ix_recipe_ingredient_ref_link_reference_id"), "recipe_ingredient_ref_link", ["reference_id"], unique=False
)
op.create_index(op.f("ix_recipe_instructions_created_at"), "recipe_instructions", ["created_at"], unique=False)
op.create_index(op.f("ix_recipe_instructions_position"), "recipe_instructions", ["position"], unique=False)
op.create_index(op.f("ix_recipe_instructions_recipe_id"), "recipe_instructions", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipe_nutrition_created_at"), "recipe_nutrition", ["created_at"], unique=False)
op.create_index(op.f("ix_recipe_nutrition_recipe_id"), "recipe_nutrition", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipe_settings_created_at"), "recipe_settings", ["created_at"], unique=False)
op.create_index(op.f("ix_recipe_settings_recipe_id"), "recipe_settings", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipe_share_tokens_created_at"), "recipe_share_tokens", ["created_at"], unique=False)
op.create_index(op.f("ix_recipe_share_tokens_recipe_id"), "recipe_share_tokens", ["recipe_id"], unique=False)
op.create_index(
op.f("ix_recipe_timeline_events_created_at"), "recipe_timeline_events", ["created_at"], unique=False
)
op.create_index(op.f("ix_recipe_timeline_events_recipe_id"), "recipe_timeline_events", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipe_timeline_events_timestamp"), "recipe_timeline_events", ["timestamp"], unique=False)
op.create_index(op.f("ix_recipe_timeline_events_user_id"), "recipe_timeline_events", ["user_id"], unique=False)
op.create_index(op.f("ix_recipes_created_at"), "recipes", ["created_at"], unique=False)
op.create_index(op.f("ix_recipes_name"), "recipes", ["name"], unique=False)
op.create_index(op.f("ix_recipes_ingredients_created_at"), "recipes_ingredients", ["created_at"], unique=False)
op.create_index(op.f("ix_recipes_ingredients_food_id"), "recipes_ingredients", ["food_id"], unique=False)
op.create_index(op.f("ix_recipes_ingredients_position"), "recipes_ingredients", ["position"], unique=False)
op.create_index(op.f("ix_recipes_ingredients_unit_id"), "recipes_ingredients", ["unit_id"], unique=False)
op.create_index(
op.f("ix_recipes_to_categories_category_id"), "recipes_to_categories", ["category_id"], unique=False
)
op.create_index(op.f("ix_recipes_to_categories_recipe_id"), "recipes_to_categories", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipes_to_tags_recipe_id"), "recipes_to_tags", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipes_to_tags_tag_id"), "recipes_to_tags", ["tag_id"], unique=False)
op.create_index(op.f("ix_recipes_to_tools_recipe_id"), "recipes_to_tools", ["recipe_id"], unique=False)
op.create_index(op.f("ix_recipes_to_tools_tool_id"), "recipes_to_tools", ["tool_id"], unique=False)
op.create_index(op.f("ix_report_entries_created_at"), "report_entries", ["created_at"], unique=False)
op.create_index(op.f("ix_report_entries_report_id"), "report_entries", ["report_id"], unique=False)
op.create_index(op.f("ix_server_tasks_created_at"), "server_tasks", ["created_at"], unique=False)
op.create_index(op.f("ix_shopping_list_extras_created_at"), "shopping_list_extras", ["created_at"], unique=False)
op.create_index(
op.f("ix_shopping_list_extras_shopping_list_id"), "shopping_list_extras", ["shopping_list_id"], unique=False
)
op.create_index(
op.f("ix_shopping_list_item_extras_created_at"), "shopping_list_item_extras", ["created_at"], unique=False
)
op.create_index(
op.f("ix_shopping_list_item_extras_shopping_list_item_id"),
"shopping_list_item_extras",
["shopping_list_item_id"],
unique=False,
)
op.create_index(
op.f("ix_shopping_list_item_recipe_reference_created_at"),
"shopping_list_item_recipe_reference",
["created_at"],
unique=False,
)
op.create_index(op.f("ix_shopping_list_items_created_at"), "shopping_list_items", ["created_at"], unique=False)
op.create_index(op.f("ix_shopping_list_items_position"), "shopping_list_items", ["position"], unique=False)
op.create_index(
op.f("ix_shopping_list_items_shopping_list_id"), "shopping_list_items", ["shopping_list_id"], unique=False
)
op.create_index(
op.f("ix_shopping_list_recipe_reference_created_at"),
"shopping_list_recipe_reference",
["created_at"],
unique=False,
)
op.create_index(op.f("ix_shopping_lists_created_at"), "shopping_lists", ["created_at"], unique=False)
op.create_index(op.f("ix_tags_created_at"), "tags", ["created_at"], unique=False)
op.create_index(op.f("ix_tools_created_at"), "tools", ["created_at"], unique=False)
op.create_index(op.f("ix_tools_group_id"), "tools", ["group_id"], unique=False)
op.create_index(op.f("ix_users_created_at"), "users", ["created_at"], unique=False)
op.create_index(op.f("ix_users_to_favorites_recipe_id"), "users_to_favorites", ["recipe_id"], unique=False)
op.create_index(op.f("ix_users_to_favorites_user_id"), "users_to_favorites", ["user_id"], unique=False)
op.create_index(op.f("ix_webhook_urls_created_at"), "webhook_urls", ["created_at"], unique=False)
# ### end Alembic commands ###
def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f("ix_webhook_urls_created_at"), table_name="webhook_urls")
op.drop_index(op.f("ix_users_to_favorites_user_id"), table_name="users_to_favorites")
op.drop_index(op.f("ix_users_to_favorites_recipe_id"), table_name="users_to_favorites")
op.drop_index(op.f("ix_users_created_at"), table_name="users")
op.drop_index(op.f("ix_tools_group_id"), table_name="tools")
op.drop_index(op.f("ix_tools_created_at"), table_name="tools")
op.drop_index(op.f("ix_tags_created_at"), table_name="tags")
op.drop_index(op.f("ix_shopping_lists_created_at"), table_name="shopping_lists")
op.drop_index(op.f("ix_shopping_list_recipe_reference_created_at"), table_name="shopping_list_recipe_reference")
op.drop_index(op.f("ix_shopping_list_items_shopping_list_id"), table_name="shopping_list_items")
op.drop_index(op.f("ix_shopping_list_items_position"), table_name="shopping_list_items")
op.drop_index(op.f("ix_shopping_list_items_created_at"), table_name="shopping_list_items")
op.drop_index(
op.f("ix_shopping_list_item_recipe_reference_created_at"), table_name="shopping_list_item_recipe_reference"
)
op.drop_index(op.f("ix_shopping_list_item_extras_shopping_list_item_id"), table_name="shopping_list_item_extras")
op.drop_index(op.f("ix_shopping_list_item_extras_created_at"), table_name="shopping_list_item_extras")
op.drop_index(op.f("ix_shopping_list_extras_shopping_list_id"), table_name="shopping_list_extras")
op.drop_index(op.f("ix_shopping_list_extras_created_at"), table_name="shopping_list_extras")
op.drop_index(op.f("ix_server_tasks_created_at"), table_name="server_tasks")
op.drop_index(op.f("ix_report_entries_report_id"), table_name="report_entries")
op.drop_index(op.f("ix_report_entries_created_at"), table_name="report_entries")
op.drop_index(op.f("ix_recipes_to_tools_tool_id"), table_name="recipes_to_tools")
op.drop_index(op.f("ix_recipes_to_tools_recipe_id"), table_name="recipes_to_tools")
op.drop_index(op.f("ix_recipes_to_tags_tag_id"), table_name="recipes_to_tags")
op.drop_index(op.f("ix_recipes_to_tags_recipe_id"), table_name="recipes_to_tags")
op.drop_index(op.f("ix_recipes_to_categories_recipe_id"), table_name="recipes_to_categories")
op.drop_index(op.f("ix_recipes_to_categories_category_id"), table_name="recipes_to_categories")
op.drop_index(op.f("ix_recipes_ingredients_unit_id"), table_name="recipes_ingredients")
op.drop_index(op.f("ix_recipes_ingredients_position"), table_name="recipes_ingredients")
op.drop_index(op.f("ix_recipes_ingredients_food_id"), table_name="recipes_ingredients")
op.drop_index(op.f("ix_recipes_ingredients_created_at"), table_name="recipes_ingredients")
op.drop_index(op.f("ix_recipes_name"), table_name="recipes")
op.drop_index(op.f("ix_recipes_created_at"), table_name="recipes")
op.drop_index(op.f("ix_recipe_timeline_events_user_id"), table_name="recipe_timeline_events")
op.drop_index(op.f("ix_recipe_timeline_events_timestamp"), table_name="recipe_timeline_events")
op.drop_index(op.f("ix_recipe_timeline_events_recipe_id"), table_name="recipe_timeline_events")
op.drop_index(op.f("ix_recipe_timeline_events_created_at"), table_name="recipe_timeline_events")
op.drop_index(op.f("ix_recipe_share_tokens_recipe_id"), table_name="recipe_share_tokens")
op.drop_index(op.f("ix_recipe_share_tokens_created_at"), table_name="recipe_share_tokens")
op.drop_index(op.f("ix_recipe_settings_recipe_id"), table_name="recipe_settings")
op.drop_index(op.f("ix_recipe_settings_created_at"), table_name="recipe_settings")
op.drop_index(op.f("ix_recipe_nutrition_recipe_id"), table_name="recipe_nutrition")
op.drop_index(op.f("ix_recipe_nutrition_created_at"), table_name="recipe_nutrition")
op.drop_index(op.f("ix_recipe_instructions_recipe_id"), table_name="recipe_instructions")
op.drop_index(op.f("ix_recipe_instructions_position"), table_name="recipe_instructions")
op.drop_index(op.f("ix_recipe_instructions_created_at"), table_name="recipe_instructions")
op.drop_index(op.f("ix_recipe_ingredient_ref_link_reference_id"), table_name="recipe_ingredient_ref_link")
op.drop_index(op.f("ix_recipe_ingredient_ref_link_instruction_id"), table_name="recipe_ingredient_ref_link")
op.drop_index(op.f("ix_recipe_ingredient_ref_link_created_at"), table_name="recipe_ingredient_ref_link")
op.drop_index(op.f("ix_recipe_comments_user_id"), table_name="recipe_comments")
op.drop_index(op.f("ix_recipe_comments_recipe_id"), table_name="recipe_comments")
op.drop_index(op.f("ix_recipe_comments_created_at"), table_name="recipe_comments")
op.drop_index(op.f("ix_recipe_assets_recipe_id"), table_name="recipe_assets")
op.drop_index(op.f("ix_recipe_assets_created_at"), table_name="recipe_assets")
op.drop_index(op.f("ix_plan_rules_to_tags_tag_id"), table_name="plan_rules_to_tags")
op.drop_index(op.f("ix_plan_rules_to_tags_plan_rule_id"), table_name="plan_rules_to_tags")
op.drop_index(op.f("ix_plan_rules_to_categories_group_plan_rule_id"), table_name="plan_rules_to_categories")
op.drop_index(op.f("ix_plan_rules_to_categories_category_id"), table_name="plan_rules_to_categories")
op.drop_index(op.f("ix_password_reset_tokens_user_id"), table_name="password_reset_tokens")
op.drop_index(op.f("ix_password_reset_tokens_created_at"), table_name="password_reset_tokens")
op.drop_index(op.f("ix_notes_recipe_id"), table_name="notes")
op.drop_index(op.f("ix_notes_created_at"), table_name="notes")
op.drop_index(op.f("ix_multi_purpose_labels_created_at"), table_name="multi_purpose_labels")
op.drop_index(op.f("ix_long_live_tokens_user_id"), table_name="long_live_tokens")
op.drop_index(op.f("ix_long_live_tokens_token"), table_name="long_live_tokens")
op.drop_index(op.f("ix_long_live_tokens_created_at"), table_name="long_live_tokens")
op.drop_index(op.f("ix_invite_tokens_group_id"), table_name="invite_tokens")
op.drop_index(op.f("ix_invite_tokens_created_at"), table_name="invite_tokens")
op.drop_index(op.f("ix_ingredient_units_group_id"), table_name="ingredient_units")
op.drop_index(op.f("ix_ingredient_units_created_at"), table_name="ingredient_units")
op.drop_index(op.f("ix_ingredient_foods_label_id"), table_name="ingredient_foods")
op.drop_index(op.f("ix_ingredient_foods_group_id"), table_name="ingredient_foods")
op.drop_index(op.f("ix_ingredient_foods_created_at"), table_name="ingredient_foods")
op.drop_index(op.f("ix_ingredient_food_extras_ingredient_food_id"), table_name="ingredient_food_extras")
op.drop_index(op.f("ix_ingredient_food_extras_created_at"), table_name="ingredient_food_extras")
op.drop_index(op.f("ix_groups_created_at"), table_name="groups")
op.drop_index(op.f("ix_group_to_categories_group_id"), table_name="group_to_categories")
op.drop_index(op.f("ix_group_to_categories_category_id"), table_name="group_to_categories")
op.drop_index(op.f("ix_group_reports_created_at"), table_name="group_reports")
op.drop_index(op.f("ix_group_preferences_created_at"), table_name="group_preferences")
op.drop_index(op.f("ix_group_meal_plans_created_at"), table_name="group_meal_plans")
op.drop_index(op.f("ix_group_meal_plan_rules_group_id"), table_name="group_meal_plan_rules")
op.drop_index(op.f("ix_group_meal_plan_rules_created_at"), table_name="group_meal_plan_rules")
op.drop_index(op.f("ix_group_events_notifiers_created_at"), table_name="group_events_notifiers")
op.drop_index(op.f("ix_group_events_notifier_options_created_at"), table_name="group_events_notifier_options")
op.drop_index(op.f("ix_group_data_exports_created_at"), table_name="group_data_exports")
op.drop_index(op.f("ix_cookbooks_to_tools_tool_id"), table_name="cookbooks_to_tools")
op.drop_index(op.f("ix_cookbooks_to_tools_cookbook_id"), table_name="cookbooks_to_tools")
op.drop_index(op.f("ix_cookbooks_to_tags_tag_id"), table_name="cookbooks_to_tags")
op.drop_index(op.f("ix_cookbooks_to_tags_cookbook_id"), table_name="cookbooks_to_tags")
op.drop_index(op.f("ix_cookbooks_to_categories_cookbook_id"), table_name="cookbooks_to_categories")
op.drop_index(op.f("ix_cookbooks_to_categories_category_id"), table_name="cookbooks_to_categories")
op.drop_index(op.f("ix_cookbooks_slug"), table_name="cookbooks")
op.drop_index(op.f("ix_cookbooks_group_id"), table_name="cookbooks")
op.drop_index(op.f("ix_cookbooks_created_at"), table_name="cookbooks")
op.drop_index(op.f("ix_categories_created_at"), table_name="categories")
op.drop_index(op.f("ix_api_extras_recipee_id"), table_name="api_extras")
op.drop_index(op.f("ix_api_extras_created_at"), table_name="api_extras")
# ### end Alembic commands ###

View File

@ -6,7 +6,7 @@ from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
class SqlAlchemyBase(DeclarativeBase):
id: Mapped[int] = mapped_column(Integer, primary_key=True)
created_at: Mapped[datetime | None] = mapped_column(DateTime, default=datetime.now)
created_at: Mapped[datetime | None] = mapped_column(DateTime, default=datetime.now, index=True)
update_at: Mapped[datetime | None] = mapped_column(DateTime, default=datetime.now, onupdate=datetime.now)

View File

@ -18,11 +18,11 @@ class CookBook(SqlAlchemyBase, BaseMixins):
id: Mapped[guid.GUID] = mapped_column(guid.GUID, primary_key=True, default=guid.GUID.generate)
position: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
group_id: Mapped[guid.GUID | None] = mapped_column(guid.GUID, ForeignKey("groups.id"))
group_id: Mapped[guid.GUID | None] = mapped_column(guid.GUID, ForeignKey("groups.id"), index=True)
group: Mapped[Optional["Group"]] = orm.relationship("Group", back_populates="cookbooks")
name: Mapped[str] = mapped_column(String, nullable=False)
slug: Mapped[str] = mapped_column(String, nullable=False)
slug: Mapped[str] = mapped_column(String, nullable=False, index=True)
description: Mapped[str | None] = mapped_column(String, default="")
public: Mapped[str | None] = mapped_column(Boolean, default=False)

View File

@ -15,7 +15,7 @@ class GroupInviteToken(SqlAlchemyBase, BaseMixins):
token: Mapped[str] = mapped_column(String, index=True, nullable=False, unique=True)
uses_left: Mapped[int] = mapped_column(Integer, nullable=False, default=1)
group_id: Mapped[guid.GUID | None] = mapped_column(guid.GUID, ForeignKey("groups.id"))
group_id: Mapped[guid.GUID | None] = mapped_column(guid.GUID, ForeignKey("groups.id"), index=True)
group: Mapped[Optional["Group"]] = orm.relationship("Group", back_populates="invite_tokens")
@auto_init()

View File

@ -21,7 +21,7 @@ class GroupMealPlanRules(BaseMixins, SqlAlchemyBase):
__tablename__ = "group_meal_plan_rules"
id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate)
group_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False)
group_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False, index=True)
day: Mapped[str] = mapped_column(
String, nullable=False, default="unset"

View File

@ -23,7 +23,7 @@ class ReportEntryModel(SqlAlchemyBase, BaseMixins):
exception: Mapped[str] = mapped_column(String, nullable=True)
timestamp: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
report_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("group_reports.id"), nullable=False)
report_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("group_reports.id"), nullable=False, index=True)
report: Mapped["ReportModel"] = orm.relationship("ReportModel", back_populates="entries")
@auto_init()

View File

@ -37,11 +37,11 @@ class ShoppingListItem(SqlAlchemyBase, BaseMixins):
# Id's
id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate)
shopping_list_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("shopping_lists.id"))
shopping_list_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("shopping_lists.id"), index=True)
# Meta
is_ingredient: Mapped[bool | None] = mapped_column(Boolean, default=True)
position: Mapped[int] = mapped_column(Integer, nullable=False, default=0)
position: Mapped[int] = mapped_column(Integer, nullable=False, default=0, index=True)
checked: Mapped[bool | None] = mapped_column(Boolean, default=False)
quantity: Mapped[float | None] = mapped_column(Float, default=1)

View File

@ -40,19 +40,21 @@ class ExtrasGeneric:
# used specifically for recipe extras
class ApiExtras(ExtrasGeneric, SqlAlchemyBase):
__tablename__ = "api_extras"
recipee_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"))
recipee_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"), index=True)
class IngredientFoodExtras(ExtrasGeneric, SqlAlchemyBase):
__tablename__ = "ingredient_food_extras"
ingredient_food_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("ingredient_foods.id"))
ingredient_food_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("ingredient_foods.id"), index=True)
class ShoppingListExtras(ExtrasGeneric, SqlAlchemyBase):
__tablename__ = "shopping_list_extras"
shopping_list_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("shopping_lists.id"))
shopping_list_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("shopping_lists.id"), index=True)
class ShoppingListItemExtras(ExtrasGeneric, SqlAlchemyBase):
__tablename__ = "shopping_list_item_extras"
shopping_list_item_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("shopping_list_items.id"))
shopping_list_item_id: Mapped[GUID | None] = mapped_column(
GUID, sa.ForeignKey("shopping_list_items.id"), index=True
)

View File

@ -8,7 +8,7 @@ from mealie.db.models._model_utils.guid import GUID
class RecipeAsset(SqlAlchemyBase):
__tablename__ = "recipe_assets"
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"))
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"), index=True)
name: Mapped[str | None] = mapped_column(sa.String)
icon: Mapped[str | None] = mapped_column(sa.String)
file_name: Mapped[str | None] = mapped_column(sa.String)

View File

@ -19,29 +19,29 @@ logger = root_logger.get_logger()
group_to_categories = sa.Table(
"group_to_categories",
SqlAlchemyBase.metadata,
sa.Column("group_id", GUID, sa.ForeignKey("groups.id")),
sa.Column("category_id", GUID, sa.ForeignKey("categories.id")),
sa.Column("group_id", GUID, sa.ForeignKey("groups.id"), index=True),
sa.Column("category_id", GUID, sa.ForeignKey("categories.id"), index=True),
)
plan_rules_to_categories = sa.Table(
"plan_rules_to_categories",
SqlAlchemyBase.metadata,
sa.Column("group_plan_rule_id", GUID, sa.ForeignKey("group_meal_plan_rules.id")),
sa.Column("category_id", GUID, sa.ForeignKey("categories.id")),
sa.Column("group_plan_rule_id", GUID, sa.ForeignKey("group_meal_plan_rules.id"), index=True),
sa.Column("category_id", GUID, sa.ForeignKey("categories.id"), index=True),
)
recipes_to_categories = sa.Table(
"recipes_to_categories",
SqlAlchemyBase.metadata,
sa.Column("recipe_id", GUID, sa.ForeignKey("recipes.id")),
sa.Column("category_id", GUID, sa.ForeignKey("categories.id")),
sa.Column("recipe_id", GUID, sa.ForeignKey("recipes.id"), index=True),
sa.Column("category_id", GUID, sa.ForeignKey("categories.id"), index=True),
)
cookbooks_to_categories = sa.Table(
"cookbooks_to_categories",
SqlAlchemyBase.metadata,
sa.Column("cookbook_id", GUID, sa.ForeignKey("cookbooks.id")),
sa.Column("category_id", GUID, sa.ForeignKey("categories.id")),
sa.Column("cookbook_id", GUID, sa.ForeignKey("cookbooks.id"), index=True),
sa.Column("category_id", GUID, sa.ForeignKey("categories.id"), index=True),
)

View File

@ -18,11 +18,11 @@ class RecipeComment(SqlAlchemyBase, BaseMixins):
text: Mapped[str | None] = mapped_column(String)
# Recipe Link
recipe_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("recipes.id"), nullable=False)
recipe_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("recipes.id"), nullable=False, index=True)
recipe: Mapped["RecipeModel"] = orm.relationship("RecipeModel", back_populates="comments")
# User Link
user_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("users.id"), nullable=False)
user_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("users.id"), nullable=False, index=True)
user: Mapped["User"] = orm.relationship(
"User", back_populates="comments", single_parent=True, foreign_keys=[user_id]
)

View File

@ -19,7 +19,7 @@ class IngredientUnitModel(SqlAlchemyBase, BaseMixins):
id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate)
# ID Relationships
group_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False)
group_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False, index=True)
group: Mapped["Group"] = orm.relationship("Group", back_populates="ingredient_units", foreign_keys=[group_id])
name: Mapped[str | None] = mapped_column(String)
@ -39,7 +39,7 @@ class IngredientFoodModel(SqlAlchemyBase, BaseMixins):
id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate)
# ID Relationships
group_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False)
group_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False, index=True)
group: Mapped["Group"] = orm.relationship("Group", back_populates="ingredient_foods", foreign_keys=[group_id])
name: Mapped[str | None] = mapped_column(String)
@ -47,7 +47,7 @@ class IngredientFoodModel(SqlAlchemyBase, BaseMixins):
ingredients: Mapped[list["RecipeIngredient"]] = orm.relationship("RecipeIngredient", back_populates="food")
extras: Mapped[list[IngredientFoodExtras]] = orm.relationship("IngredientFoodExtras", cascade="all, delete-orphan")
label_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("multi_purpose_labels.id"))
label_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("multi_purpose_labels.id"), index=True)
label: Mapped[MultiPurposeLabel | None] = orm.relationship(MultiPurposeLabel, uselist=False, back_populates="foods")
@api_extras
@ -59,17 +59,17 @@ class IngredientFoodModel(SqlAlchemyBase, BaseMixins):
class RecipeIngredient(SqlAlchemyBase, BaseMixins):
__tablename__ = "recipes_ingredients"
id: Mapped[int] = mapped_column(Integer, primary_key=True)
position: Mapped[int | None] = mapped_column(Integer)
position: Mapped[int | None] = mapped_column(Integer, index=True)
recipe_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("recipes.id"))
title: Mapped[str | None] = mapped_column(String) # Section Header - Shows if Present
note: Mapped[str | None] = mapped_column(String) # Force Show Text - Overrides Concat
# Scaling Items
unit_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("ingredient_units.id"))
unit_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("ingredient_units.id"), index=True)
unit: Mapped[IngredientUnitModel | None] = orm.relationship(IngredientUnitModel, uselist=False)
food_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("ingredient_foods.id"))
food_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("ingredient_foods.id"), index=True)
food: Mapped[IngredientFoodModel | None] = orm.relationship(IngredientFoodModel, uselist=False)
quantity: Mapped[float | None] = mapped_column(Float)

View File

@ -8,8 +8,8 @@ from .._model_utils.guid import GUID
class RecipeIngredientRefLink(SqlAlchemyBase, BaseMixins):
__tablename__ = "recipe_ingredient_ref_link"
instruction_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("recipe_instructions.id"))
reference_id: Mapped[GUID | None] = mapped_column(GUID)
instruction_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("recipe_instructions.id"), index=True)
reference_id: Mapped[GUID | None] = mapped_column(GUID, index=True)
@auto_init()
def __init__(self, **_) -> None:
@ -19,8 +19,8 @@ class RecipeIngredientRefLink(SqlAlchemyBase, BaseMixins):
class RecipeInstruction(SqlAlchemyBase):
__tablename__ = "recipe_instructions"
id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate)
recipe_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("recipes.id"))
position: Mapped[int | None] = mapped_column(Integer)
recipe_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("recipes.id"), index=True)
position: Mapped[int | None] = mapped_column(Integer, index=True)
type: Mapped[str | None] = mapped_column(String, default="")
title: Mapped[str | None] = mapped_column(String)
text: Mapped[str | None] = mapped_column(String)

View File

@ -8,7 +8,7 @@ from mealie.db.models._model_utils.guid import GUID
class Note(SqlAlchemyBase):
__tablename__ = "notes"
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"))
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"), index=True)
title: Mapped[str | None] = mapped_column(sa.String)
text: Mapped[str | None] = mapped_column(sa.String)

View File

@ -8,7 +8,7 @@ from mealie.db.models._model_utils.guid import GUID
class Nutrition(SqlAlchemyBase):
__tablename__ = "recipe_nutrition"
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"))
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"), index=True)
calories: Mapped[str | None] = mapped_column(sa.String)
fat_content: Mapped[str | None] = mapped_column(sa.String)
fiber_content: Mapped[str | None] = mapped_column(sa.String)

View File

@ -54,7 +54,7 @@ class RecipeModel(SqlAlchemyBase, BaseMixins):
)
# General Recipe Properties
name: Mapped[str] = mapped_column(sa.String, nullable=False)
name: Mapped[str] = mapped_column(sa.String, nullable=False, index=True)
description: Mapped[str | None] = mapped_column(sa.String)
image: Mapped[str | None] = mapped_column(sa.String)

View File

@ -18,11 +18,11 @@ class RecipeTimelineEvent(SqlAlchemyBase, BaseMixins):
id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate)
# Parent Recipe
recipe_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("recipes.id"), nullable=False)
recipe_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("recipes.id"), nullable=False, index=True)
recipe: Mapped["RecipeModel"] = relationship("RecipeModel", back_populates="timeline_events")
# Related User (Actor)
user_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("users.id"), nullable=False)
user_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("users.id"), nullable=False, index=True)
user: Mapped["User"] = relationship(
"User", back_populates="recipe_timeline_events", single_parent=True, foreign_keys=[user_id]
)
@ -34,7 +34,7 @@ class RecipeTimelineEvent(SqlAlchemyBase, BaseMixins):
image: Mapped[str | None] = mapped_column(String)
# Timestamps
timestamp: Mapped[datetime | None] = mapped_column(DateTime)
timestamp: Mapped[datetime | None] = mapped_column(DateTime, index=True)
@auto_init()
def __init__(

View File

@ -8,7 +8,7 @@ from mealie.db.models._model_utils.guid import GUID
class RecipeSettings(SqlAlchemyBase):
__tablename__ = "recipe_settings"
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"))
recipe_id: Mapped[GUID | None] = mapped_column(GUID, sa.ForeignKey("recipes.id"), index=True)
public: Mapped[bool | None] = mapped_column(sa.Boolean)
show_nutrition: Mapped[bool | None] = mapped_column(sa.Boolean)
show_assets: Mapped[bool | None] = mapped_column(sa.Boolean)

View File

@ -22,7 +22,7 @@ class RecipeShareTokenModel(SqlAlchemyBase, BaseMixins):
group_id: Mapped[GUID] = mapped_column(GUID, sa.ForeignKey("groups.id"), nullable=False, index=True)
recipe_id: Mapped[GUID] = mapped_column(GUID, sa.ForeignKey("recipes.id"), nullable=False)
recipe_id: Mapped[GUID] = mapped_column(GUID, sa.ForeignKey("recipes.id"), nullable=False, index=True)
recipe: Mapped["RecipeModel"] = sa.orm.relationship("RecipeModel", back_populates="share_tokens", uselist=False)
expires_at: Mapped[datetime] = mapped_column(sa.DateTime, nullable=False)

View File

@ -19,22 +19,22 @@ logger = root_logger.get_logger()
recipes_to_tags = sa.Table(
"recipes_to_tags",
SqlAlchemyBase.metadata,
sa.Column("recipe_id", guid.GUID, sa.ForeignKey("recipes.id")),
sa.Column("tag_id", guid.GUID, sa.ForeignKey("tags.id")),
sa.Column("recipe_id", guid.GUID, sa.ForeignKey("recipes.id"), index=True),
sa.Column("tag_id", guid.GUID, sa.ForeignKey("tags.id"), index=True),
)
plan_rules_to_tags = sa.Table(
"plan_rules_to_tags",
SqlAlchemyBase.metadata,
sa.Column("plan_rule_id", guid.GUID, sa.ForeignKey("group_meal_plan_rules.id")),
sa.Column("tag_id", guid.GUID, sa.ForeignKey("tags.id")),
sa.Column("plan_rule_id", guid.GUID, sa.ForeignKey("group_meal_plan_rules.id"), index=True),
sa.Column("tag_id", guid.GUID, sa.ForeignKey("tags.id"), index=True),
)
cookbooks_to_tags = sa.Table(
"cookbooks_to_tags",
SqlAlchemyBase.metadata,
sa.Column("cookbook_id", guid.GUID, sa.ForeignKey("cookbooks.id")),
sa.Column("tag_id", guid.GUID, sa.ForeignKey("tags.id")),
sa.Column("cookbook_id", guid.GUID, sa.ForeignKey("cookbooks.id"), index=True),
sa.Column("tag_id", guid.GUID, sa.ForeignKey("tags.id"), index=True),
)

View File

@ -15,15 +15,15 @@ if TYPE_CHECKING:
recipes_to_tools = Table(
"recipes_to_tools",
SqlAlchemyBase.metadata,
Column("recipe_id", GUID, ForeignKey("recipes.id")),
Column("tool_id", GUID, ForeignKey("tools.id")),
Column("recipe_id", GUID, ForeignKey("recipes.id"), index=True),
Column("tool_id", GUID, ForeignKey("tools.id"), index=True),
)
cookbooks_to_tools = Table(
"cookbooks_to_tools",
SqlAlchemyBase.metadata,
Column("cookbook_id", GUID, ForeignKey("cookbooks.id")),
Column("tool_id", GUID, ForeignKey("tools.id")),
Column("cookbook_id", GUID, ForeignKey("cookbooks.id"), index=True),
Column("tool_id", GUID, ForeignKey("tools.id"), index=True),
)
@ -33,7 +33,7 @@ class Tool(SqlAlchemyBase, BaseMixins):
id: Mapped[GUID] = mapped_column(GUID, primary_key=True, default=GUID.generate)
# ID Relationships
group_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False)
group_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("groups.id"), nullable=False, index=True)
group: Mapped["Group"] = orm.relationship("Group", back_populates="tools", foreign_keys=[group_id])
name: Mapped[str] = mapped_column(String, index=True, unique=True, nullable=False)

View File

@ -13,7 +13,7 @@ if TYPE_CHECKING:
class PasswordResetModel(SqlAlchemyBase, BaseMixins):
__tablename__ = "password_reset_tokens"
user_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("users.id"), nullable=False)
user_id: Mapped[GUID] = mapped_column(GUID, ForeignKey("users.id"), nullable=False, index=True)
user: Mapped["User"] = orm.relationship("User", back_populates="password_reset_tokens", uselist=False)
token: Mapped[str] = mapped_column(String(64), unique=True, nullable=False)

View File

@ -6,6 +6,6 @@ from .._model_utils import GUID
users_to_favorites = Table(
"users_to_favorites",
SqlAlchemyBase.metadata,
Column("user_id", GUID, ForeignKey("users.id")),
Column("recipe_id", GUID, ForeignKey("recipes.id")),
Column("user_id", GUID, ForeignKey("users.id"), index=True),
Column("recipe_id", GUID, ForeignKey("recipes.id"), index=True),
)

View File

@ -21,9 +21,9 @@ if TYPE_CHECKING:
class LongLiveToken(SqlAlchemyBase, BaseMixins):
__tablename__ = "long_live_tokens"
name: Mapped[str] = mapped_column(String, nullable=False)
token: Mapped[str] = mapped_column(String, nullable=False)
token: Mapped[str] = mapped_column(String, nullable=False, index=True)
user_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("users.id"))
user_id: Mapped[GUID | None] = mapped_column(GUID, ForeignKey("users.id"), index=True)
user: Mapped[Optional["User"]] = orm.relationship("User")
def __init__(self, name, token, user_id, **_) -> None:

View File

@ -4,7 +4,7 @@ from mealie.core.config import get_app_settings
from mealie.services.backups_v2.alchemy_exporter import AlchemyExporter
ALEMBIC_VERSIONS = [
{"version_num": "165d943c64ee"},
{"version_num": "ff5f73b01a7a"},
]