Skip to content

Commit d76e446

Browse files
authored
Merge pull request #263 from treyhunner/mikeengland-master
Added batch size option to the management command for populating the history
2 parents f2f23e6 + 45ddf14 commit d76e446

File tree

6 files changed

+30
-5
lines changed

6 files changed

+30
-5
lines changed

AUTHORS.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ Authors
3333
- Ulysses Vilela
3434
- vnagendra
3535
- Lucas Wiman
36+
- Michael England
3637

3738
Background
3839
==========

CHANGES.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changes
22
=======
33

4+
tip (unreleased)
5+
----------------
6+
- Added --batchsize option to the populate_history management command.
7+
48
1.8.2 (2017-01-19)
59
------------------
610
- Add Polish locale.

docs/usage.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ initial change for preexisting model instances:
8383
8484
$ python manage.py populate_history --auto
8585
86+
By default, history rows are inserted in batches of 200. This can be changed if needed for large tables
87+
by using the ``--batchsize`` option, for example ``--batchsize 500``.
88+
8689
.. _admin_integration:
8790

8891
Integration with Django Admin

simple_history/management/commands/_populate_utils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def get_history_model_for_model(model):
1515
return getattr(model, manager_name).model
1616

1717

18-
def bulk_history_create(model, history_model):
18+
def bulk_history_create(model, history_model, batch_size):
1919
"""Save a copy of all instances to the historical model."""
2020
historical_instances = [
2121
history_model(
@@ -26,4 +26,4 @@ def bulk_history_create(model, history_model):
2626
for field in instance._meta.fields
2727
}
2828
) for instance in model.objects.all()]
29-
history_model.objects.bulk_create(historical_instances)
29+
history_model.objects.bulk_create(historical_instances, batch_size=batch_size)

simple_history/management/commands/populate_history.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class Command(BaseCommand):
3030
if hasattr(BaseCommand, 'option_list'): # Django < 1.8
3131
option_list = BaseCommand.option_list + (
3232
make_option('--auto', action='store_true', dest='auto', default=False),
33+
make_option('--batchsize', action='store', dest='batchsize', default=200, type=int),
3334
)
3435

3536
def add_arguments(self, parser):
@@ -43,6 +44,14 @@ def add_arguments(self, parser):
4344
help='Automatically search for models with the '
4445
'HistoricalRecords field type',
4546
)
47+
parser.add_argument(
48+
'--batchsize',
49+
action='store',
50+
dest='batchsize',
51+
default=200,
52+
type=int,
53+
help='Set a custom batch size when bulk inserting historical records.',
54+
)
4655

4756
def handle(self, *args, **options):
4857
to_process = set()
@@ -65,7 +74,7 @@ def handle(self, *args, **options):
6574
else:
6675
self.stdout.write(self.COMMAND_HINT)
6776

68-
self._process(to_process)
77+
self._process(to_process, batch_size=options['batchsize'])
6978

7079
def _handle_model_list(self, *args):
7180
failing = False
@@ -101,7 +110,7 @@ def _model_from_natural_key(self, natural_key):
101110
" < {model} >\n".format(model=natural_key))
102111
return model, history_model
103112

104-
def _process(self, to_process):
113+
def _process(self, to_process, batch_size):
105114
for model, history_model in to_process:
106115
if history_model.objects.count():
107116
self.stderr.write("{msg} {model}\n".format(
@@ -110,5 +119,5 @@ def _process(self, to_process):
110119
))
111120
continue
112121
self.stdout.write(self.START_SAVING_FOR_MODEL.format(model=model))
113-
utils.bulk_history_create(model, history_model)
122+
utils.bulk_history_create(model, history_model, batch_size)
114123
self.stdout.write(self.DONE_SAVING_FOR_MODEL.format(model=model))

simple_history/tests/tests/test_commands.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ def test_auto_populate(self):
5454
stdout=StringIO(), stderr=StringIO())
5555
self.assertEqual(models.Poll.history.all().count(), 1)
5656

57+
def test_populate_with_custom_batch_size(self):
58+
models.Poll.objects.create(question="Will this populate?",
59+
pub_date=datetime.now())
60+
models.Poll.history.all().delete()
61+
management.call_command(self.command_name, auto=True, batchsize=500,
62+
stdout=StringIO(), stderr=StringIO())
63+
self.assertEqual(models.Poll.history.all().count(), 1)
64+
5765
def test_specific_populate(self):
5866
models.Poll.objects.create(question="Will this populate?",
5967
pub_date=datetime.now())

0 commit comments

Comments
 (0)