Skip to content

Commit 0741bcd

Browse files
authored
Merge pull request #145 from datakind/DeleteBatch
Delete batch
2 parents 12f1119 + 123c05b commit 0741bcd

1 file changed

Lines changed: 39 additions & 7 deletions

File tree

src/webapp/routers/data.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import uuid
44
from datetime import datetime, date
55
from databricks.sdk import WorkspaceClient
6-
from typing import Annotated, Any, Dict, List, cast, IO
7-
from pydantic import BaseModel
6+
from typing import Annotated, Any, Dict, List, cast, IO, Optional
7+
from pydantic import BaseModel, Field
88
from fastapi import APIRouter, Depends, HTTPException, status, Response
99
from fastapi.responses import FileResponse
1010
from sqlalchemy import and_, or_
@@ -101,6 +101,33 @@ class BatchInfo(BaseModel):
101101
updated_by: str | None = None
102102

103103

104+
class DeletedFile(BaseModel):
105+
file: str = Field(..., description="Basename of the deleted file")
106+
path: str = Field(..., description="Bucket object path, e.g. 'validated/<name>'")
107+
deleted_at: datetime = Field(
108+
..., description="UTC timestamp when deletion occurred"
109+
)
110+
111+
112+
class DeleteBatchResponse(BaseModel):
113+
inst_id: str
114+
batch_id: str
115+
deleted: List[DeletedFile] = Field(
116+
default_factory=list, description="Files deleted in storage"
117+
)
118+
not_found: List[str] = Field(
119+
default_factory=list, description="Files not found in storage"
120+
)
121+
errors: List[str] = Field(
122+
default_factory=list, description="Errors encountered during delete"
123+
)
124+
db_deleted_rows: int = Field(..., description="Number of FileTable rows deleted")
125+
batch_deleted: bool = Field(
126+
..., description="Whether the BatchTable row was deleted"
127+
)
128+
message: Optional[str] = Field(None, description="Optional info message")
129+
130+
104131
class DataInfo(BaseModel):
105132
"""The Data object that's returned. Generally maps to a file, but technically maps to a GCS blob."""
106133

@@ -684,7 +711,7 @@ def update_batch(
684711
}
685712

686713

687-
@router.patch("/{inst_id}/delete-batch/{batch_id}")
714+
@router.patch("/{inst_id}/delete-batch/{batch_id}", response_model=DeleteBatchResponse)
688715
def delete_batch(
689716
inst_id: str,
690717
batch_id: str,
@@ -710,10 +737,13 @@ def delete_batch(
710737
)
711738

712739
# 2) Gather filenames to delete
740+
713741
batch_files: list[str] = list(
714742
sess.execute(
715-
select(FileTable.name).where(
716-
FileTable.id == str_to_uuid(batch_id),
743+
select(FileTable.name)
744+
.join(FileTable.batches) # many-to-many via association_table
745+
.where(
746+
BatchTable.id == str_to_uuid(batch_id),
717747
FileTable.inst_id == str_to_uuid(inst_id),
718748
)
719749
)
@@ -755,9 +785,11 @@ def delete_batch(
755785
try:
756786
rows = (
757787
sess.execute(
758-
select(FileTable).where(
788+
select(FileTable)
789+
.join(FileTable.batches)
790+
.where(
791+
BatchTable.id == str_to_uuid(batch_id),
759792
FileTable.inst_id == str_to_uuid(inst_id),
760-
FileTable.id == str_to_uuid(batch_id),
761793
FileTable.name.in_(target_names),
762794
)
763795
)

0 commit comments

Comments
 (0)