Skip to content

Commit

Permalink
Fix location of temp files in migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
mfonism authored and atodorov committed Sep 7, 2020
1 parent 9e06eb4 commit c36428a
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 34 deletions.
4 changes: 4 additions & 0 deletions tcms/settings/common.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-

import os
import pathlib
import tempfile
from importlib import import_module

import pkg_resources
Expand Down Expand Up @@ -446,3 +448,5 @@
MESSAGE_TAGS = {
messages.ERROR: 'danger',
}

TEMP_DIR = pathlib.Path(tempfile.gettempdir())
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import glob
import json

from django.conf import settings
from django.db import migrations, models

from tcms.rpc.serializer import Serializer
Expand Down Expand Up @@ -32,9 +32,9 @@ def forward_copy_data(apps, schema_editor):
for test_case in test_case_model.objects.all():
latest_text = test_case_text_model.objects.filter(case=test_case.pk).order_by('-pk').first()
if latest_text:
file_name = '/tmp/kiwitcms-testcases-migrations-0006-\
TestCaseText-%d' % latest_text.pk # nosec:B108:hardcoded_tmp_directory
with open(file_name, 'w') as outfile:
file_name = 'kiwitcms-testcases-migrations-0006-TestCaseText-%d' % latest_text.pk
test_case_file = settings.TEMP_DIR / file_name
with test_case_file.open('w') as outfile:
json.dump(Serializer(model=latest_text).serialize_model(), outfile)

test_case.case_text = convert_test_case_text(latest_text)
Expand All @@ -50,11 +50,8 @@ def forward_copy_data(apps, schema_editor):
def backward_restore_data(apps, schema_editor):
test_case_text_model = apps.get_model('testcases', 'TestCaseText')

for file_name in glob.glob(
'/tmp/kiwitcms-testcases-migrations-0006-\
TestCaseText-*' # nosec:B108:hardcoded_tmp_directory
):
with open(file_name, 'r') as infile:
for file in settings.TEMP_DIR.glob('kiwitcms-testcases-migrations-0006-TestCaseText-*'):
with file.open('r') as infile:
data = json.load(infile)
test_case_text = test_case_text_model(**data)
test_case_text.save()
Expand Down
30 changes: 14 additions & 16 deletions tcms/testcases/migrations/0010_remove_bug.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import glob
import json

from django.conf import settings
from django.db import migrations

from tcms.rpc.serializer import Serializer
Expand All @@ -12,9 +12,9 @@ def forward_copy_data(apps, schema_editor):
link_reference_ids = []

for bug in bug_model.objects.all():
file_name = '/tmp/kiwitcms-testcases-migrations-\
0010-Bug-%d' % bug.pk # nosec:B108:hardcoded_tmp_directory
with open(file_name, 'w') as outfile:
bug_file_name = 'kiwitcms-testcases-migrations-0010-Bug-%d' % bug.pk
bug_file = settings.TEMP_DIR / bug_file_name
with bug_file.open('w') as outfile:
json.dump(Serializer(model=bug).serialize_model(), outfile)

if not bug.case_run_id:
Expand All @@ -28,28 +28,26 @@ def forward_copy_data(apps, schema_editor):
)
link_reference_ids.append(link_reference.pk)

link_reference_ids_file_name = '/tmp/kiwitcms-testcases-migrations-0010-\
new-LinkReference-IDs' # nosec:B108:hardcoded_tmp_directory
with open(link_reference_ids_file_name, 'w') as link_reference_ids_file:
json.dump(link_reference_ids, link_reference_ids_file)
link_reference_ids_file_name = 'kiwitcms-testcases-migrations-0010-new-LinkReference-IDs'
link_reference_ids_file = settings.TEMP_DIR / link_reference_ids_file_name
with link_reference_ids_file.open('w') as outfile:
json.dump(link_reference_ids, outfile)


def backward_restore_data(apps, schema_editor):
bug_model = apps.get_model('testcases', 'Bug')
link_reference_model = apps.get_model('linkreference', 'LinkReference')

for file_name in glob.glob(
'/tmp/kiwitcms-testcases-migrations-0010-Bug-*' # nosec:B108:hardcoded_tmp_directory
):
with open(file_name, 'r') as infile:
for file in settings.TEMP_DIR.glob('kiwitcms-testcases-migrations-0010-Bug-*'):
with file.open('r') as infile:
data = json.load(infile)
bug = bug_model(**data)
bug.save()

link_reference_ids_file_name = '/tmp/kiwitcms-testcases-migrations-0010-\
new-LinkReference-IDs' # nosec:B108:hardcoded_tmp_directory
with open(link_reference_ids_file_name, 'r') as link_reference_ids_file:
link_reference_ids = json.load(link_reference_ids_file)
link_reference_ids_file_name = 'kiwitcms-testcases-migrations-0010-new-LinkReference-IDs'
link_reference_ids_file = settings.TEMP_DIR / link_reference_ids_file_name
with link_reference_ids_file.open('r') as infile:
link_reference_ids = json.load(infile)
link_reference_model.objects.filter(pk__in=link_reference_ids).delete()


Expand Down
15 changes: 6 additions & 9 deletions tcms/testcases/migrations/0011_trim_bugsystem_fields.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import glob
import json

from django.conf import settings
from django.db import migrations, models

from tcms.rpc.serializer import Serializer
Expand All @@ -10,21 +10,18 @@ def forwards_store_data(apps, schema_editor):
bug_system_model = apps.get_model('testcases', 'BugSystem')

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
file_name = 'kiwitcms-testcases-migration-0011-BugSystem-%d' % bug_system.pk
bug_file = settings.TEMP_DIR / file_name

with open(file_name, 'w') as outfile:
with bug_file.open('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')

for file_name in glob.glob(
'/tmp/kiwitcms-testcases-migration-0011-\
BugSystem-*' # nosec:B108:hardcoded_tmp_directory
):
with open(file_name, 'r') as infile:
for file in settings.TEMP_DIR.glob('kiwitcms-testcases-migration-0011-BugSystem-*'):
with file.open('r') as infile:
data = json.load(infile)
bug_system = bug_system_model(**data)
bug_system.save()
Expand Down

0 comments on commit c36428a

Please sign in to comment.