-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to backpopulate
file_id
field
This can be removed once we've deployed and successfully migrated the existing data.
- Loading branch information
Showing
6 changed files
with
57 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from django.core.management.base import BaseCommand | ||
|
||
from airlock.business_logic import bll, store_file | ||
from local_db.models import RequestFileMetadata | ||
|
||
|
||
class Command(BaseCommand): | ||
def handle(self, **kwargs): | ||
empty_file_ids = RequestFileMetadata.objects.filter(file_id="") | ||
for file_meta in list(empty_file_ids): | ||
request_id = file_meta.filegroup.request_id | ||
request = bll.get_release_request(request_id) | ||
original_path = ( | ||
request.root() / file_meta.filegroup.name / file_meta.relpath | ||
) | ||
file_meta.file_id = store_file(request, original_path) | ||
file_meta.save() | ||
original_path.unlink() |
Empty file.
Empty file.
39 changes: 39 additions & 0 deletions
39
tests/integration/management/commands/test_backpopulate_file_id.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import pytest | ||
from django.core.management import call_command | ||
|
||
from local_db.models import RequestFileMetadata | ||
from tests import factories | ||
|
||
|
||
@pytest.mark.django_db | ||
def test_command(): | ||
# Create a ReleaseRequest with a single file | ||
factories.write_workspace_file("workspace", "subdir/file.txt", "some content") | ||
release_request = factories.create_release_request("workspace") | ||
factories.create_filegroup( | ||
release_request, group_name="default_group", filepaths=["subdir/file.txt"] | ||
) | ||
|
||
# Determine its current path and its "old style" path | ||
file_meta = RequestFileMetadata.objects.get() | ||
full_relpath = f"{file_meta.filegroup.name}/{file_meta.relpath}" | ||
path_with_hash = release_request.abspath(full_relpath) | ||
old_style_path = release_request.root() / full_relpath | ||
|
||
# Move the file to its old location | ||
old_style_path.parent.mkdir(parents=True, exist_ok=True) | ||
path_with_hash.rename(old_style_path) | ||
|
||
# Remove the file hash from the database record | ||
file_meta.file_id = "" | ||
file_meta.save() | ||
|
||
call_command("backpopulate_file_id") | ||
|
||
# Confirm the database record now contains a hash | ||
file_meta = RequestFileMetadata.objects.get() | ||
assert file_meta.file_id != "" | ||
|
||
# Confirm that the file is in its expected location and not in its old location | ||
assert path_with_hash.exists() | ||
assert not old_style_path.exists() |