Skip to content

Commit

Permalink
[db] Use query to update HistoricalTestCase in migrations
Browse files Browse the repository at this point in the history
using test_case.history.latest() doesn't work b/c we don't have
the historical manager available during migrations so we need to
query the object directly.

The test suite doesn't catch this b/c initially there is no data
and the for loop isn't executed.
  • Loading branch information
atodorov committed Mar 19, 2019
1 parent e0f0d3f commit e854618
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,17 @@ def convert_test_case_text(test_case_text):
def forward_copy_data(apps, schema_editor):
TestCase = apps.get_model('testcases', 'TestCase')
TestCaseText = apps.get_model('testcases', 'TestCaseText')
HistoricalTestCase = apps.get_model('testcases', 'HistoricalTestCase')

for test_case in TestCase.objects.all():
latest_text = TestCaseText.objects.filter(case=test_case.pk).order_by('-pk').first()
if latest_text:
test_case.case_text = convert_test_case_text(latest_text)
test_case.save()
# b/c the above will not generate history
history = test_case.history.latest()
history = HistoricalTestCase.objects.filter(
case_id=test_case.pk
).order_by('-history_id').first()
history.case_text = test_case.case_text
history.save()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@

def forward_copy_data(apps, schema_editor):
TestCase = apps.get_model('testcases', 'TestCase')
HistoricalTestCase = apps.get_model('testcases', 'HistoricalTestCase')

for test_case in TestCase.objects.all():
history = test_case.history.latest()
history = HistoricalTestCase.objects.filter(
case_id=test_case.pk
).order_by('-history_id').first()

# In 0006_merge_text_field_into_testcase_model we may have
# failed to save the text into the history record leaving
Expand Down

0 comments on commit e854618

Please sign in to comment.