Skip to content

Commit bee617d

Browse files
committed
support for --default-date for populate_history command
1 parent 9fb7c5c commit bee617d

File tree

4 files changed

+45
-10
lines changed

4 files changed

+45
-10
lines changed

runtests.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,16 +46,16 @@ def __getitem__(self, item):
4646
"default": {
4747
"ENGINE": "django.db.backends.postgresql",
4848
"NAME": "test",
49-
"USER": "postgres",
50-
"PASSWORD": "postgres",
49+
"USER": "debug",
50+
"PASSWORD": "debug",
5151
"HOST": "127.0.0.1",
5252
"PORT": 5432,
5353
},
5454
"other": {
5555
"ENGINE": "django.db.backends.postgresql",
5656
"NAME": "other",
57-
"USER": "postgres",
58-
"PASSWORD": "postgres",
57+
"USER": "debug",
58+
"PASSWORD": "debug",
5959
"HOST": "127.0.0.1",
6060
"PORT": 5432,
6161
},

simple_history/management/commands/populate_history.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ def add_arguments(self, parser):
4242
type=int,
4343
help="Set a custom batch size when bulk inserting historical records.",
4444
)
45+
parser.add_argument(
46+
"--default-date",
47+
action="store",
48+
dest="default_date",
49+
default=None,
50+
help="Set a custom date for the historical records.",
51+
)
4552

4653
def handle(self, *args, **options):
4754
self.verbosity = options["verbosity"]
@@ -60,7 +67,7 @@ def handle(self, *args, **options):
6067
if self.verbosity >= 1:
6168
self.stdout.write(self.COMMAND_HINT)
6269

63-
self._process(to_process, batch_size=options["batchsize"])
70+
self._process(to_process, batch_size=options["batchsize"], default_date=options["default_date"])
6471

6572
def _auto_models(self):
6673
to_process = set()
@@ -109,11 +116,12 @@ def _model_from_natural_key(self, natural_key):
109116
raise ValueError(msg)
110117
return model, history_model
111118

112-
def _bulk_history_create(self, model, batch_size):
119+
def _bulk_history_create(self, model, batch_size, default_date=None):
113120
"""Save a copy of all instances to the historical model.
114121
115122
:param model: Model you want to bulk create
116123
:param batch_size: number of models to create at once.
124+
:param default_date: date to set for the historical records.
117125
:return:
118126
"""
119127

@@ -135,7 +143,7 @@ def _bulk_history_create(self, model, batch_size):
135143
# creating them. So we only keep batch_size worth of models in
136144
# historical_instances and clear them after we hit batch_size
137145
if index % batch_size == 0:
138-
history.bulk_history_create(instances, batch_size=batch_size)
146+
history.bulk_history_create(instances, batch_size=batch_size, default_date=default_date)
139147

140148
instances = []
141149

@@ -151,9 +159,9 @@ def _bulk_history_create(self, model, batch_size):
151159

152160
# create any we didn't get in the last loop
153161
if instances:
154-
history.bulk_history_create(instances, batch_size=batch_size)
162+
history.bulk_history_create(instances, batch_size=batch_size, default_date=default_date)
155163

156-
def _process(self, to_process, batch_size):
164+
def _process(self, to_process, batch_size, default_date=None):
157165
for model, history_model in to_process:
158166
if history_model.objects.exists():
159167
self.stderr.write(
@@ -164,6 +172,6 @@ def _process(self, to_process, batch_size):
164172
continue
165173
if self.verbosity >= 1:
166174
self.stdout.write(self.START_SAVING_FOR_MODEL.format(model=model))
167-
self._bulk_history_create(model, batch_size)
175+
self._bulk_history_create(model, batch_size, default_date)
168176
if self.verbosity >= 1:
169177
self.stdout.write(self.DONE_SAVING_FOR_MODEL.format(model=model))

simple_history/tests/tests/test_commands.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,16 @@ def test_excluded_fields(self):
194194
initial_history_record = PollWithExcludeFields.history.all()[0]
195195
self.assertEqual(initial_history_record.question, poll.question)
196196

197+
def test_populate_with_default_date(self):
198+
date = datetime(2020, 7, 1)
199+
Poll.objects.create(question="Will this populate?", pub_date=datetime.now())
200+
Poll.history.all().delete()
201+
management.call_command(
202+
self.command_name, auto=True, default_date=date, stdout=StringIO(), stderr=StringIO()
203+
)
204+
self.assertEqual(Poll.history.all().count(), 1)
205+
self.assertEqual(Poll.history.all()[0].history_date, date)
206+
197207

198208
class TestCleanDuplicateHistory(TestCase):
199209
command_name = "clean_duplicate_history"

simple_history/tests/tests/test_utils.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,22 @@ def test_bulk_manager_with_custom_model_attributes(self):
630630
5,
631631
)
632632

633+
def test_bulk_history_create_with_default_date(self):
634+
date = datetime(2020, 7, 1)
635+
history_manager = get_history_manager_for_model(PollWithHistoricalSessionAttr)
636+
history_manager.bulk_history_create(self.data, default_date=date)
637+
638+
self.assertEqual(PollWithHistoricalSessionAttr.objects.count(), 0)
639+
self.assertEqual(PollWithHistoricalSessionAttr.history.count(), 5)
640+
self.assertTrue(
641+
all(
642+
[
643+
history.history_date == date
644+
for history in PollWithHistoricalSessionAttr.history.all()
645+
]
646+
)
647+
)
648+
633649

634650
class UpdateChangeReasonTestCase(TestCase):
635651
def test_update_change_reason_with_excluded_fields(self):
@@ -640,3 +656,4 @@ def test_update_change_reason_with_excluded_fields(self):
640656
update_change_reason(poll, "Test change reason.")
641657
most_recent = poll.history.order_by("-history_date").first()
642658
self.assertEqual(most_recent.history_change_reason, "Test change reason.")
659+

0 commit comments

Comments
 (0)