Skip to content

Commit e61cbaa

Browse files
craigmaloneyRoss Mechanic
authored andcommitted
Add an iterator to prevent OOM when cleaning large databases (#604)
* Fixes OOM issue when cleaning over 8M records * Adding name to Authors * Added changes to Changelog * Removing redundant .all() * Removing unnecessary typo. * Adding black formatting to unrelated files
1 parent 2d3de6a commit e61cbaa

File tree

5 files changed

+9
-5
lines changed

5 files changed

+9
-5
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Authors
2222
- Brian Dixon
2323
- Christopher Broderick (`uhurusurfa <https://github.com/uhurusurfa>`_)
2424
- Corey Bertram
25+
- Craig Maloney (`craigmaloney <https://github.com/craigmaloney>`_)
2526
- Damien Nozay
2627
- Daniel Gilge
2728
- Daniel Levy

CHANGES.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Unreleased
99
- Fix `model_to_dict` to detect changes in a parent model when using
1010
`inherit=True` (backwards-incompatible for users who were directly
1111
using previous version) (gh-576)
12+
- Use an iterator for `clean_duplicate_history`
1213

1314
2.7.3 (2019-07-15)
1415
------------------

simple_history/management/commands/clean_duplicate_history.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ def _process(self, to_process, date_back=None, dry_run=True):
6969
# have changes (in the given period) but
7070
# `m_qs.values(model._meta.pk.name).distinct()`
7171
# is actually slower than looping all and filtering in the code...
72-
for o in model.objects.all():
72+
for o in model.objects.iterator():
7373
self._process_instance(o, model, stop_date=stop_date, dry_run=dry_run)
7474

7575
def _process_instance(self, instance, model, stop_date=None, dry_run=True):

simple_history/tests/tests/test_manager.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ def setUp(self):
2222
self.obj.changed_by = user
2323
self.obj.save()
2424
self.model.objects.all().delete() # allows us to leave PK on instance
25-
self.delete_history, self.change_history, self.create_history = (
26-
self.model.history.all()
27-
)
25+
(
26+
self.delete_history,
27+
self.change_history,
28+
self.create_history,
29+
) = self.model.history.all()
2830
self.create_history.history_date = self.now - timedelta(days=2)
2931
self.create_history.save()
3032
self.change_history.history_date = self.now - timedelta(days=1)

simple_history/tests/tests/test_models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ def assertRecordValues(self, record, klass, values_dict):
123123
def test_create(self):
124124
p = Poll(question="what's up?", pub_date=today)
125125
p.save()
126-
record, = p.history.all()
126+
(record,) = p.history.all()
127127
self.assertRecordValues(
128128
record,
129129
Poll,

0 commit comments

Comments
 (0)