mirror of
https://github.com/mealie-recipes/mealie.git
synced 2025-08-05 13:35:23 +02:00
fix: delete shopping list if no users are found (#3290)
Some checks are pending
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Docker Nightly Production / Build Tagged Release (push) Blocked by required conditions
Docker Nightly Production / Notify Discord (push) Blocked by required conditions
Docker Nightly Production / Backend Server Tests (push) Waiting to run
Docker Nightly Production / Frontend and End-to-End Tests (push) Waiting to run
Some checks are pending
CodeQL / Analyze (javascript-typescript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Docker Nightly Production / Build Tagged Release (push) Blocked by required conditions
Docker Nightly Production / Notify Discord (push) Blocked by required conditions
Docker Nightly Production / Backend Server Tests (push) Waiting to run
Docker Nightly Production / Frontend and End-to-End Tests (push) Waiting to run
This commit is contained in:
parent
63a362a48a
commit
f1fbf0d120
1 changed files with 23 additions and 7 deletions
|
@ -13,6 +13,9 @@ from sqlalchemy import orm
|
|||
|
||||
import mealie.db.migration_types
|
||||
from alembic import op
|
||||
from mealie.core.root_logger import get_logger
|
||||
|
||||
logger = get_logger()
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = "2298bb460ffd"
|
||||
|
@ -37,13 +40,20 @@ def find_user_id_for_group(group_id: UUID):
|
|||
with session:
|
||||
try:
|
||||
# try to find an admin user
|
||||
user_id = session.execute(sa.text(stmt).bindparams(group_id=group_id)).scalar_one()
|
||||
return session.execute(sa.text(stmt).bindparams(group_id=group_id)).scalar_one()
|
||||
except orm.exc.NoResultFound:
|
||||
pass
|
||||
|
||||
try:
|
||||
# fallback to any user
|
||||
user_id = session.execute(
|
||||
return session.execute(
|
||||
sa.text("SELECT id FROM users WHERE group_id=:group_id LIMIT 1").bindparams(group_id=group_id)
|
||||
).scalar_one()
|
||||
return user_id
|
||||
except orm.exc.NoResultFound:
|
||||
pass
|
||||
|
||||
# no user could be found
|
||||
return None
|
||||
|
||||
|
||||
def populate_shopping_list_users():
|
||||
|
@ -54,11 +64,17 @@ def populate_shopping_list_users():
|
|||
list_ids_and_group_ids = session.execute(sa.text("SELECT id, group_id FROM shopping_lists")).all()
|
||||
for list_id, group_id in list_ids_and_group_ids:
|
||||
user_id = find_user_id_for_group(group_id)
|
||||
session.execute(
|
||||
sa.text(f"UPDATE shopping_lists SET user_id=:user_id WHERE id=:id").bindparams(
|
||||
user_id=user_id, id=list_id
|
||||
if user_id:
|
||||
session.execute(
|
||||
sa.text(f"UPDATE shopping_lists SET user_id=:user_id WHERE id=:id").bindparams(
|
||||
user_id=user_id, id=list_id
|
||||
)
|
||||
)
|
||||
)
|
||||
else:
|
||||
logger.warning(
|
||||
f"No user found for shopping list {list_id} with group {group_id}; deleting shopping list"
|
||||
)
|
||||
session.execute(sa.text(f"DELETE FROM shopping_lists WHERE id=:id").bindparams(id=list_id))
|
||||
|
||||
|
||||
def upgrade():
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue