Skip to content

Commit

Permalink
Refs kiwitcms#1774. Play around with restoring non-nullable BugSystem…
Browse files Browse the repository at this point in the history
… fields

in case the migration is applied backwards

NOTES:

- Can't use obj.serialize() b/c apps.get_model() retruns a fake
  class, not the real one. Fields are there but methods aren't.
- Use Serializer(model=...).serialize_model() instead
- ^^^ doesn't fail on previous migrations b/c the respective tables
  are empty during test! Will fail as soon as we have some records
  inside of them

- Make `validate_reg_exp` and `url_reg_exp` fields nullable before
  removing them so that in the reverse direction the values could be
  restored and finally the fields set to NOT NULL once again!
  • Loading branch information
atodorov authored and brymut committed Aug 18, 2020
1 parent d60a214 commit 3743501
Showing 1 changed file with 37 additions and 39 deletions.
76 changes: 37 additions & 39 deletions tcms/testcases/migrations/0011_trim_bugsystem_fields.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,33 @@
# Generated by Django 2.2.4 on 2019-08-16 12:17
import glob
import json

from django.db import migrations, models

from tcms.rpc.serializer import Serializer


def forwards_store_data(apps, schema_editor):
bug_system_model = apps.get_model('testcases', 'BugSystem')
file_name = '/tmp/kiwitcms-testcases-migration-0011-\
BugSystem' # nosec:B108:hardcoded_tmp_directory

with open(file_name, 'w') as temp_file:
for bug_system in bug_system_model.objects.all():
temp_file.write('{},{},{},{}\n'.format(
bug_system.name,
bug_system.description,
bug_system.url_reg_exp,
bug_system.validate_reg_exp
))
for bug_system in bug_system_model.objects.all():
file_name = '/tmp/kiwitcms-testcases-migration\
-0011-BugSystem-%d' % bug_system.pk # nosec:B108:hardcoded_tmp_directory

with open(file_name, 'w') as outfile:
json.dump(Serializer(model=bug_system).serialize_model(), outfile)


def backwards_restore_data(apps, schema_editor):
bug_system_model = apps.get_model('testcases', 'BugSystem')
file_name = '/tmp/kiwitcms-testcases-migration-0011-\
BugSystem' # nosec:B108:hardcoded_tmp_directory

with open(file_name, 'r') as temp_file:
for line in temp_file:
try:
bug_system = bug_system_model.objects.get(name=line[0])
bug_system.description = line[1]
bug_system.url_reg_exp = line[2]
bug_system.validate_reg_exp = line[3]
bug_system.save()
except bug_system_model.DoesNotExist:
print('BugSystem {} not found'.format(line[0]))
for file_name in glob.glob(
'/tmp/kiwitcms-testcases-migration-0011-\
BugSystem-*' # nosec:B108:hardcoded_tmp_directory
):
with open(file_name, 'r') as infile:
data = json.load(infile)
bug_system = bug_system_model(**data)
bug_system.save()


class Migration(migrations.Migration):
Expand All @@ -41,7 +37,25 @@ class Migration(migrations.Migration):
]

operations = [
# make these 2 fields null=True so they can be restored in the reverse migration
# once these 2 operations are reversed the fields should become once again NOT NULL
migrations.AlterField(model_name='bugsystem',
name='validate_reg_exp',
field=models.CharField(max_length=128,
help_text='A valid JavaScript regular '
'expression such as ^\\d$',
verbose_name='RegExp for ID validation',
null=True)),
migrations.AlterField(model_name='bugsystem',
name='url_reg_exp',
field=models.CharField(max_length=8192,
help_text='A valid Python format string such '
'as http://bugs.example.com/%s',
verbose_name='URL format string',
null=True)),

migrations.RunPython(forwards_store_data, backwards_restore_data),

migrations.RemoveField(
model_name='bugsystem',
name='description',
Expand All @@ -54,20 +68,4 @@ class Migration(migrations.Migration):
model_name='bugsystem',
name='validate_reg_exp',
),
migrations.AlterField(model_name='bugsystem',
name='validate_reg_exp',
preserve_default=False,
field=models.CharField(max_length=128,
help_text='A valid JavaScript regular '
'expression such as ^\\d$',
verbose_name='RegExp for ID validation',
default='')),
migrations.AlterField(model_name='bugsystem',
name='url_reg_exp',
preserve_default=False,
field=models.CharField(max_length=8192,
help_text='A valid Python format string such '
'as http://bugs.example.com/%s',
verbose_name='URL format string',
default='')),
]

0 comments on commit 3743501

Please sign in to comment.