import: make FILETYPE import idempotent by skipping existing and in-batch duplicates; tested via Docker admin import twice without UNIQUE constraint errors
This commit is contained in:
@@ -411,6 +411,8 @@ def import_filetype(db: Session, file_path: str) -> Dict[str, Any]:
|
||||
reader = csv.DictReader(f)
|
||||
|
||||
batch = []
|
||||
# Track seen file types in-memory to avoid duplicates within the same CSV
|
||||
seen_in_batch = set()
|
||||
for row_num, row in enumerate(reader, start=2):
|
||||
result['total_rows'] += 1
|
||||
|
||||
@@ -418,9 +420,18 @@ def import_filetype(db: Session, file_path: str) -> Dict[str, Any]:
|
||||
file_type = clean_string(row.get('File_Type'))
|
||||
if not file_type:
|
||||
continue
|
||||
|
||||
|
||||
# Skip if we've already queued this file_type in current batch
|
||||
if file_type in seen_in_batch:
|
||||
continue
|
||||
|
||||
# Skip if it already exists in DB (prevents UNIQUE violations when re-importing)
|
||||
if db.query(FileType).filter(FileType.file_type == file_type).first():
|
||||
continue
|
||||
|
||||
record = FileType(file_type=file_type)
|
||||
batch.append(record)
|
||||
seen_in_batch.add(file_type)
|
||||
|
||||
if len(batch) >= BATCH_SIZE:
|
||||
db.bulk_save_objects(batch)
|
||||
|
||||
Reference in New Issue
Block a user