Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Delete vfolder invitation and permission rows when deleting vfolders (#2780) #2860

Merged
merged 3 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/2780.fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Delete vfolder invitation and permission rows when deleting vfolders.
7 changes: 6 additions & 1 deletion src/ai/backend/manager/api/vfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,9 @@
vfolders,
)
from ..models.utils import execute_with_retry
from ..models.vfolder import (
delete_vfolder_relation_rows,
)
from .auth import admin_required, auth_required, superadmin_required
from .exceptions import (
BackendAgentError,
Expand Down Expand Up @@ -2254,9 +2257,11 @@ async def _delete(
permission=VFolderHostPermission.DELETE,
)

vfolder_row_ids = (entry["id"],)
await delete_vfolder_relation_rows(root_ctx.db, vfolder_row_ids)
await update_vfolder_status(
root_ctx.db,
(entry["id"],),
vfolder_row_ids,
VFolderOperationStatus.DELETE_PENDING,
)

Expand Down
48 changes: 47 additions & 1 deletion src/ai/backend/manager/models/vfolder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@
import logging
import os.path
import uuid
from collections.abc import Iterable, Mapping
from datetime import datetime
from pathlib import PurePosixPath
from typing import TYPE_CHECKING, Any, Final, List, Mapping, NamedTuple, Optional, Sequence
from typing import (
TYPE_CHECKING,
Any,
Final,
List,
NamedTuple,
Optional,
Sequence,
)

import aiohttp
import aiotools
Expand Down Expand Up @@ -398,6 +407,12 @@ class VFolderCloneInfo(NamedTuple):
)


class VFolderInvitationRow(Base):
__table__ = vfolder_invitations

vfolder_row = relationship("VFolderRow", back_populates="invitation_rows")


vfolder_permissions = sa.Table(
"vfolder_permissions",
metadata,
Expand Down Expand Up @@ -426,6 +441,7 @@ class VFolderRow(Base):
back_populates="vfolder_rows",
primaryjoin="GroupRow.id == foreign(VFolderRow.group)",
)
invitation_rows = relationship(VFolderInvitationRow, back_populates="vfolder_row")

@classmethod
async def get(
Expand Down Expand Up @@ -1144,6 +1160,34 @@ async def _update_source_vfolder() -> None:
return task_id, target_folder_id.folder_id


async def _delete_vfolder_permission_rows(
db_session: SASession,
vfolder_row_ids: Iterable[uuid.UUID],
) -> None:
stmt = sa.delete(VFolderInvitationRow).where(VFolderInvitationRow.vfolder.in_(vfolder_row_ids))
await db_session.execute(stmt)


async def _delete_vfolder_invitation_rows(
db_session: SASession,
vfolder_row_ids: Iterable[uuid.UUID],
) -> None:
stmt = sa.delete(vfolder_permissions).where(vfolder_permissions.c.vfolder.in_(vfolder_row_ids))
await db_session.execute(stmt)


async def delete_vfolder_relation_rows(
db_engine: ExtendedAsyncSAEngine,
vfolder_row_ids: Iterable[uuid.UUID],
) -> None:
async def _delete() -> None:
async with db_engine.begin_session() as db_session:
await _delete_vfolder_invitation_rows(db_session, vfolder_row_ids)
await _delete_vfolder_permission_rows(db_session, vfolder_row_ids)

await execute_with_retry(_delete)


async def initiate_vfolder_deletion(
db_engine: ExtendedAsyncSAEngine,
requested_vfolders: Sequence[VFolderDeletionInfo],
Expand All @@ -1158,6 +1202,8 @@ async def initiate_vfolder_deletion(
return 0
elif vfolder_info_len == 1:
vfolders.c.id == vfolder_ids[0]

await delete_vfolder_relation_rows(db_engine, vfolder_ids)
await update_vfolder_status(
db_engine, vfolder_ids, VFolderOperationStatus.DELETE_ONGOING, do_log=False
)
Expand Down
Loading