mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-08-06 14:05:21 +02:00
fix: Good data being deleted upon restore (#4376)
Co-authored-by: Kuchenpirat <24235032+Kuchenpirat@users.noreply.github.com>
This commit is contained in:
parent
80caa5ffaf
commit
e47d171463
1 changed files with 15 additions and 13 deletions
|
@ -20,8 +20,9 @@ def fix_dangling_refs(session: Session):
|
||||||
REASSIGN_REF_TABLES = ["group_meal_plans", "recipes", "shopping_lists"]
|
REASSIGN_REF_TABLES = ["group_meal_plans", "recipes", "shopping_lists"]
|
||||||
DELETE_REF_TABLES = ["long_live_tokens", "password_reset_tokens", "recipe_comments", "recipe_timeline_events"]
|
DELETE_REF_TABLES = ["long_live_tokens", "password_reset_tokens", "recipe_comments", "recipe_timeline_events"]
|
||||||
|
|
||||||
groups = session.query(Group).all()
|
all_groups = session.query(Group).all()
|
||||||
for group in groups:
|
all_users = session.query(User).all()
|
||||||
|
for group in all_groups:
|
||||||
# Find an arbitrary admin user in the group
|
# 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
|
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:
|
if not default_user:
|
||||||
|
@ -32,7 +33,7 @@ def fix_dangling_refs(session: Session):
|
||||||
if not default_user:
|
if not default_user:
|
||||||
continue
|
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:
|
for table_name in REASSIGN_REF_TABLES:
|
||||||
table = SqlAlchemyBase.metadata.tables[table_name]
|
table = SqlAlchemyBase.metadata.tables[table_name]
|
||||||
|
@ -40,7 +41,7 @@ def fix_dangling_refs(session: Session):
|
||||||
update(table)
|
update(table)
|
||||||
.where(
|
.where(
|
||||||
and_(
|
and_(
|
||||||
table.c.user_id.notin_(valid_user_ids),
|
table.c.user_id.notin_(valid_group_user_ids),
|
||||||
table.c.group_id == group.id,
|
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})'
|
f'in "{table_name}" table to default user ({default_user.id})'
|
||||||
)
|
)
|
||||||
|
|
||||||
for table_name in DELETE_REF_TABLES:
|
valid_user_ids_all_groups = {user.id for user in all_users}
|
||||||
table = SqlAlchemyBase.metadata.tables[table_name]
|
for table_name in DELETE_REF_TABLES:
|
||||||
delete_stmt = table.delete().where(table.c.user_id.notin_(valid_user_ids))
|
table = SqlAlchemyBase.metadata.tables[table_name]
|
||||||
result = session.execute(delete_stmt)
|
delete_stmt = table.delete().where(table.c.user_id.notin_(valid_user_ids_all_groups))
|
||||||
|
result = session.execute(delete_stmt)
|
||||||
|
|
||||||
if result.rowcount:
|
if result.rowcount:
|
||||||
logger.info(
|
logger.info(
|
||||||
f'Deleted {result.rowcount} {"row" if result.rowcount == 1 else "rows"} '
|
f'Deleted {result.rowcount} {"row" if result.rowcount == 1 else "rows"} '
|
||||||
f'in "{table_name}" table with invalid user ids'
|
f'in "{table_name}" table with invalid user ids'
|
||||||
)
|
)
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue