Skip to content

Commit

Permalink
Add command to backpopulate file_id field
Browse files Browse the repository at this point in the history
This can be removed once we've deployed and successfully migrated the
existing data.
  • Loading branch information
evansd committed Mar 13, 2024
1 parent aa209c1 commit 2c76678
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
Empty file added local_db/management/__init__.py
Empty file.
Empty file.
18 changes: 18 additions & 0 deletions local_db/management/commands/backpopulate_file_id.py
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 tests/integration/management/commands/test_backpopulate_file_id.py
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()

0 comments on commit 2c76678

Please sign in to comment.