|
| 1 | +import asyncio |
1 | 2 | from copy import deepcopy |
2 | 3 | from datetime import UTC, datetime, timedelta, timezone |
3 | 4 | from enum import Enum |
|
8 | 9 | from sqlalchemy.orm import joinedload, selectinload |
9 | 10 | from sqlalchemy.sql.functions import coalesce |
10 | 11 |
|
| 12 | +from app.db.base import GetDB, IS_SQLITE |
11 | 13 | from app.db.compiles_types import DateDiff |
12 | 14 | from app.db.models import ( |
13 | 15 | Admin, |
|
39 | 41 | from .group import get_groups_by_ids |
40 | 42 |
|
41 | 43 | _USER_AGENT_MAX_LEN = UserSubscriptionUpdate.__table__.columns.user_agent.type.length or 512 |
42 | | - |
43 | | - |
44 | 44 | async def load_user_attrs( |
45 | 45 | user: User, |
46 | 46 | *, |
@@ -696,12 +696,32 @@ async def _delete_user_dependencies(db: AsyncSession, user_ids: list[int]): |
696 | 696 | if not user_ids: |
697 | 697 | return |
698 | 698 |
|
699 | | - await db.execute(delete(NodeUserUsage).where(NodeUserUsage.user_id.in_(user_ids))) |
700 | | - await db.execute(delete(NotificationReminder).where(NotificationReminder.user_id.in_(user_ids))) |
701 | | - await db.execute(delete(UserSubscriptionUpdate).where(UserSubscriptionUpdate.user_id.in_(user_ids))) |
702 | | - await db.execute(delete(UserUsageResetLogs).where(UserUsageResetLogs.user_id.in_(user_ids))) |
703 | | - await db.execute(delete(NextPlan).where(NextPlan.user_id.in_(user_ids))) |
704 | | - await db.execute(users_groups_association.delete().where(users_groups_association.c.user_id.in_(user_ids))) |
| 699 | + statements = [ |
| 700 | + delete(NodeUserUsage).where(NodeUserUsage.user_id.in_(user_ids)), |
| 701 | + delete(NotificationReminder).where(NotificationReminder.user_id.in_(user_ids)), |
| 702 | + delete(UserSubscriptionUpdate).where(UserSubscriptionUpdate.user_id.in_(user_ids)), |
| 703 | + delete(UserUsageResetLogs).where(UserUsageResetLogs.user_id.in_(user_ids)), |
| 704 | + delete(NextPlan).where(NextPlan.user_id.in_(user_ids)), |
| 705 | + users_groups_association.delete().where(users_groups_association.c.user_id.in_(user_ids)), |
| 706 | + ] |
| 707 | + |
| 708 | + if IS_SQLITE: |
| 709 | + for stmt in statements: |
| 710 | + await db.execute(stmt) |
| 711 | + return |
| 712 | + |
| 713 | + async def execute_in_isolated_session(statement): |
| 714 | + async with GetDB() as isolated_db: |
| 715 | + await isolated_db.execute(statement) |
| 716 | + await isolated_db.commit() |
| 717 | + |
| 718 | + results = await asyncio.gather( |
| 719 | + *(execute_in_isolated_session(stmt) for stmt in statements), |
| 720 | + return_exceptions=True, |
| 721 | + ) |
| 722 | + for result in results: |
| 723 | + if isinstance(result, Exception): |
| 724 | + raise result |
705 | 725 |
|
706 | 726 |
|
707 | 727 | async def remove_user(db: AsyncSession, db_user: User) -> User: |
|
0 commit comments