From f6295bcbe76f0e2c9ed38d190bb15a2b990b71f7 Mon Sep 17 00:00:00 2001 From: vshvechko Date: Mon, 17 Jun 2024 13:54:28 +0300 Subject: [PATCH] fix(patch): fix duplication error in patch "M2-6879" for arbittrary DBs (M2-7055) (#1415) --- .../m2_6879_create_deleted_respondents.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/apps/shared/commands/patches/m2_6879_create_deleted_respondents.py b/src/apps/shared/commands/patches/m2_6879_create_deleted_respondents.py index 7e3e29f8f93..279014bd5b9 100644 --- a/src/apps/shared/commands/patches/m2_6879_create_deleted_respondents.py +++ b/src/apps/shared/commands/patches/m2_6879_create_deleted_respondents.py @@ -71,10 +71,12 @@ async def get_answers_applets_respondents( async def get_missing_applet_respondent( - session: AsyncSession, owner_id: uuid.UUID, arbitrary_applet_respondents: set[tuple[uuid.UUID, uuid.UUID]] + session: AsyncSession, applet_ids: list[uuid.UUID], arbitrary_applet_respondents: set[tuple[uuid.UUID, uuid.UUID]] ) -> list[tuple[uuid.UUID, uuid.UUID]]: query: Query = select(UserAppletAccessSchema.user_id, UserAppletAccessSchema.applet_id) - query = query.where(UserAppletAccessSchema.owner_id == owner_id, UserAppletAccessSchema.role == Role.RESPONDENT) + query = query.where( + UserAppletAccessSchema.applet_id.in_(applet_ids), UserAppletAccessSchema.role == Role.RESPONDENT + ) db_result = await session.execute(query) roles_users_applets = db_result.all() return list(arbitrary_applet_respondents - set(roles_users_applets)) @@ -93,7 +95,12 @@ async def find_and_create_missing_roles_arbitrary( roles = [] for offset in range(0, count, limit): arbitrary_applet_respondents = await get_answers_applets_respondents(arbitrary_session, limit, offset) - missing_users_applets = await get_missing_applet_respondent(session, owner_id, arbitrary_applet_respondents) + + applet_ids = {x[1] for x in arbitrary_applet_respondents} + + missing_users_applets = await get_missing_applet_respondent( + session, list(applet_ids), arbitrary_applet_respondents + ) for user_id, applet_id in missing_users_applets: schema = UserAppletAccessSchema( user_id=user_id, @@ -137,10 +144,10 @@ async def main(session: AsyncSession, *args, **kwargs): async with session_maker() as arb_session: try: await find_and_create_missing_roles_arbitrary(session, arb_session, workspace.user_id) - await arb_session.commit() + await session.commit() print(f"Processing workspace#{i + 1} {workspace.id} " f"finished") except Exception: - await arb_session.rollback() + await session.rollback() print(f"[bold red]Error: Workspace#{i + 1} {workspace.id} processing error[/bold red]") raise except asyncio.TimeoutError: