Skip to content

Commit

Permalink
Use Django's own DurationField. Fixes #183
Browse files Browse the repository at this point in the history
Migrations for 'testcases':
    - Alter field estimated_time on testcase
Migrations for 'testruns':
    - Alter field estimated_time on testrun

Will properly convert existing field values
  • Loading branch information
atodorov committed Mar 13, 2018
1 parent acbe777 commit b7e92a0
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 3 deletions.
48 changes: 48 additions & 0 deletions tcms/testcases/migrations/0016_duration_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 2.0.3 on 2018-03-13 21:35
from datetime import timedelta
from django.db import migrations, models


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

for tc in TestCase.objects.all():
tc.estimated_time_new = timedelta(seconds=tc.estimated_time)
tc.save()


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

for tc in TestCase.objects.all():
tc.estimated_time = tc.estimated_time_new.total_seconds()
tc.save()


class Migration(migrations.Migration):

dependencies = [
('testcases', '0015_rename_testcasebug'),
]

operations = [
# add a timedelta field
migrations.AddField(
model_name='testcase',
name='estimated_time_new',
field=models.DurationField(default=timedelta(0)),
),
# migrate the values
migrations.RunPython(forward, reverse),
# remove the integer field
migrations.RemoveField(
model_name='testcase',
name='estimated_time',
),
# rename the new field to the old name
migrations.RenameField(
model_name='testcase',
old_name='estimated_time_new',
new_name='estimated_time',
),
]
4 changes: 2 additions & 2 deletions tcms/testcases/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
from datetime import datetime
from datetime import datetime, timedelta
from html2text import html2text

from django.conf import settings
Expand Down Expand Up @@ -127,7 +127,7 @@ class TestCase(TCMSActionModel):
summary = models.CharField(max_length=255)
requirement = models.CharField(max_length=255, blank=True, null=True)
alias = models.CharField(max_length=255, blank=True)
estimated_time = models.IntegerField(db_column='estimated_time', default=0) # DurationField
estimated_time = models.DurationField(default=timedelta(0))
notes = models.TextField(blank=True, null=True)

case_status = models.ForeignKey(TestCaseStatus, on_delete=models.CASCADE)
Expand Down
48 changes: 48 additions & 0 deletions tcms/testruns/migrations/0010_duration_field.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Generated by Django 2.0.3 on 2018-03-13 21:35
from datetime import timedelta
from django.db import migrations, models


def forward(apps, schema_editor):
TestRun = apps.get_model('testruns', 'TestRun')

for tr in TestRun.objects.all():
tr.estimated_time_new = timedelta(seconds=tr.estimated_time)
tr.save()


def reverse(apps, schema_editor):
TestRun = apps.get_model('testruns', 'TestRun')

for tr in TestRun.objects.all():
tr.estimated_time = tr.estimated_time_new.total_seconds()
tr.save()


class Migration(migrations.Migration):

dependencies = [
('testruns', '0009_rename_testtag'),
]

operations = [
# add a timedelta field
migrations.AddField(
model_name='testrun',
name='estimated_time_new',
field=models.DurationField(default=timedelta(0)),
),
# migrate the values
migrations.RunPython(forward, reverse),
# remove the integer field
migrations.RemoveField(
model_name='testrun',
name='estimated_time',
),
# rename the new field to the old name
migrations.RenameField(
model_name='testrun',
old_name='estimated_time_new',
new_name='estimated_time',
),
]
2 changes: 1 addition & 1 deletion tcms/testruns/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TestRun(TCMSActionModel):
stop_date = models.DateTimeField(null=True, blank=True, db_index=True)
summary = models.TextField()
notes = models.TextField(blank=True)
estimated_time = models.IntegerField(default=0) # todo: DurationField
estimated_time = models.DurationField(default=datetime.timedelta(0))

plan = models.ForeignKey('testplans.TestPlan', related_name='run',
on_delete=models.CASCADE)
Expand Down

0 comments on commit b7e92a0

Please sign in to comment.