3
3
4
4
from django .conf import settings
5
5
from django .core .management .base import CommandParser
6
- from django .core .paginator import Paginator
7
6
from django .utils import timezone
8
7
9
8
from stac_api .models .general import BaseAssetUpload
@@ -42,20 +41,16 @@ def delete_by_batch(self, queryset, object_type, batch_size):
42
41
# than waiting forever, to fail and to have to start from scratch next
43
42
# time.
44
43
type_name = f'stac_api.{ object_type .__name__ } '
44
+ total = queryset .count ()
45
45
deleted_count = 0
46
- # We delete rows as we iterate over them. This only works if we iterate
47
- # from the end to the beginning. But we also want to delete the objects
48
- # in the order of the QuerySet we received. Hence, we first reverse the
49
- # the QuerySet then we reverse the iterator.
50
- queryset = queryset .reverse ()
51
- paginator = Paginator (queryset , batch_size )
52
- for page_number in reversed (paginator .page_range ):
53
- page = paginator .page (page_number )
54
- # We cannot just call page.object_list.delete() because DELETE
46
+ while deleted_count < total :
47
+ # We cannot just call queryset[:batch_size].delete() because DELETE
55
48
# does not support LIMIT/OFFSET. So instead we extract the ids
56
49
# then we'll build a new QuerySet to DELETE them.
57
- ids = page . object_list . values ('id' )
50
+ ids = queryset . values ('id' )[: batch_size ]
58
51
expected_deletions = len (ids )
52
+ if expected_deletions == 0 :
53
+ break
59
54
dry_run_prefix = ''
60
55
if self .options ['dry_run' ]:
61
56
dry_run_prefix = '[dry run]: '
@@ -66,7 +61,7 @@ def delete_by_batch(self, queryset, object_type, batch_size):
66
61
actual_deletions = deleted_objs .get (type_name , 0 )
67
62
deleted_count += actual_deletions
68
63
self .print_success (
69
- f'{ dry_run_prefix } Deleted { deleted_count } /{ paginator . count } { type_name } .'
64
+ f'{ dry_run_prefix } Deleted { deleted_count } /{ total } { type_name } .'
70
65
f' In this batch: { actual_deletions } /{ expected_deletions } .'
71
66
f' All objects in this batch: { deleted_objs } .'
72
67
)
0 commit comments