From e47d1714634b91e5b3996c18ae55d485464b954c Mon Sep 17 00:00:00 2001 From: Michael Genson <71845777+michael-genson@users.noreply.github.com> Date: Wed, 16 Oct 2024 09:56:07 -0500 Subject: [PATCH] fix: Good data being deleted upon restore (#4376) Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com> --- mealie/db/fixes/fix_migration_data.py | 28 ++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/mealie/db/fixes/fix_migration_data.py b/mealie/db/fixes/fix_migration_data.py index d594fce14a7..200b160d509 100644 --- a/mealie/db/fixes/fix_migration_data.py +++ b/mealie/db/fixes/fix_migration_data.py @@ -20,8 +20,9 @@ def fix_dangling_refs(session: Session): REASSIGN_REF_TABLES = ["group_meal_plans", "recipes", "shopping_lists"] DELETE_REF_TABLES = ["long_live_tokens", "password_reset_tokens", "recipe_comments", "recipe_timeline_events"] - groups = session.query(Group).all() - for group in groups: + all_groups = session.query(Group).all() + all_users = session.query(User).all() + for group in all_groups: # Find an arbitrary admin user in the group default_user = session.query(User).filter(User.group_id == group.id, User.admin == True).first() # noqa: E712 - required for SQLAlchemy comparison if not default_user: @@ -32,7 +33,7 @@ def fix_dangling_refs(session: Session): if not default_user: continue - valid_user_ids = {user.id for user in group.users} + valid_group_user_ids = {user.id for user in all_users if user.group_id == group.id} for table_name in REASSIGN_REF_TABLES: table = SqlAlchemyBase.metadata.tables[table_name] @@ -40,7 +41,7 @@ def fix_dangling_refs(session: Session): update(table) .where( and_( - table.c.user_id.notin_(valid_user_ids), + table.c.user_id.notin_(valid_group_user_ids), table.c.group_id == group.id, ) ) @@ -54,16 +55,17 @@ def fix_dangling_refs(session: Session): f'in "{table_name}" table to default user ({default_user.id})' ) - for table_name in DELETE_REF_TABLES: - table = SqlAlchemyBase.metadata.tables[table_name] - delete_stmt = table.delete().where(table.c.user_id.notin_(valid_user_ids)) - result = session.execute(delete_stmt) + valid_user_ids_all_groups = {user.id for user in all_users} + for table_name in DELETE_REF_TABLES: + table = SqlAlchemyBase.metadata.tables[table_name] + delete_stmt = table.delete().where(table.c.user_id.notin_(valid_user_ids_all_groups)) + result = session.execute(delete_stmt) - if result.rowcount: - logger.info( - f'Deleted {result.rowcount} {"row" if result.rowcount == 1 else "rows"} ' - f'in "{table_name}" table with invalid user ids' - ) + if result.rowcount: + logger.info( + f'Deleted {result.rowcount} {"row" if result.rowcount == 1 else "rows"} ' + f'in "{table_name}" table with invalid user ids' + ) session.commit()