Skip to content

Commit d59aa97

Browse files
committed
Fix IntegrityError on rerun of collect_and_create_codebase_resources
Rerunning a pipeline that calls collect_and_create_codebase_resources() on a project that already has resources raises a raw IntegrityError. Catch it and re-raise with a message pointing to the Reset feature. Fixes #1712 Signed-off-by: reehassan <theailaboratories@gmail.com>
1 parent a76e0cc commit d59aa97

2 files changed

Lines changed: 30 additions & 6 deletions

File tree

scanpipe/pipes/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from itertools import islice
3232
from pathlib import Path
3333

34+
from django.db import IntegrityError
3435
from django.db.models import Count
3536

3637
from scanpipe.models import AbstractTaskFieldsModel
@@ -139,12 +140,17 @@ def collect_and_create_codebase_resources(project, batch_size=5000):
139140
"""
140141
model_class = CodebaseResource
141142
objs = yield_resources_from_codebase(project)
142-
143-
while True:
144-
batch = list(islice(objs, batch_size))
145-
if not batch:
146-
break
147-
model_class.objects.bulk_create(batch, batch_size)
143+
try:
144+
while True:
145+
batch = list(islice(objs, batch_size))
146+
if not batch:
147+
break
148+
model_class.objects.bulk_create(batch, batch_size)
149+
except IntegrityError as e:
150+
raise IntegrityError(
151+
"Codebase resources already exist for this project. "
152+
"Reset the project before re-running."
153+
) from e
148154

149155

150156
def update_or_create_resource(project, resource_data):

scanpipe/tests/pipes/test_pipes.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from pathlib import Path
2626
from unittest import mock
2727

28+
from django.db import IntegrityError
2829
from django.test import TestCase
2930
from django.test import TransactionTestCase
3031

@@ -448,3 +449,20 @@ def test_scanpipe_pipes_collect_and_create_codebase_resources(self):
448449
self.assertEqual("from", from_resource.tag)
449450
to_resource = p1.codebaseresources.get(path="to/a.txt")
450451
self.assertEqual("to", to_resource.tag)
452+
453+
def test_scanpipe_pipes_collect_and_create_codebase_resources_duplicate_run(self):
454+
p1 = Project.objects.create(name="Analysis")
455+
input_location = self.data / "codebase" / "a.txt"
456+
to_dir = p1.codebase_path / "to"
457+
to_dir.mkdir()
458+
from_dir = p1.codebase_path / "from"
459+
from_dir.mkdir()
460+
copy_input(input_location, to_dir)
461+
copy_input(input_location, from_dir)
462+
463+
pipes.collect_and_create_codebase_resources(p1)
464+
self.assertEqual(4, p1.codebaseresources.count())
465+
466+
with self.assertRaises(IntegrityError) as ctx:
467+
pipes.collect_and_create_codebase_resources(p1)
468+
self.assertIn("Reset", str(ctx.exception))

0 commit comments

Comments
 (0)