Skip to content

Commit

Permalink
Merge branch 'long-running-migrations' of github.com:kobotoolbox/kpi …
Browse files Browse the repository at this point in the history
…into long-running-migrations
  • Loading branch information
noliveleger committed Dec 18, 2024
2 parents 9a12c0c + 815cece commit d5181d7
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Generated on 2024-12-18
from django.db.models import Q, OuterRef, Subquery

from kobo.apps.openrosa.apps.logger.models import XForm
from kobo.apps.openrosa.apps.main.models import MetaData
from kpi.models.asset import Asset
from kpi.models.asset_file import AssetFile
from kobo.apps.project_ownership.models import Transfer


def run():
"""
Update OpenRosa MetaData objects that were not updated when project
ownership was transferred to someone else. This fixes a bug introduced
and later addressed in KPI (issue #5365).
"""

# Step 1: Retrieve all assets that were transferred since the bug was present and
# use media files
asset_uids = Asset.objects.filter(
Q(
pk__in=AssetFile.objects.values_list('asset_id', flat=True).exclude(
file_type=AssetFile.PAIRED_DATA
)
)
& Q(
pk__in=Transfer.objects.values_list('asset_id', flat=True).filter(
invite__date_created__date__gte='2024-09-15'
)
)
).values_list('uid', flat=True)

username_subquery = XForm.objects.filter(pk=OuterRef('xform_id')).values(
'user__username'
)[:1]

# Step 2: Iterate through relevant MetaData objects and fix their data_file fields
for metadata in (
MetaData.objects.filter(
xform_id__in=XForm.objects.filter(
kpi_asset_uid__in=list(asset_uids)
),
)
.exclude(
Q(data_file__startswith=Subquery(username_subquery))
| Q(data_file__isnull=True)
| Q(data_file='')
)
.select_related('xform', 'xform__user')
.iterator()
):
data_file = str(metadata.data_file)
old_username, *other_parts = data_file.split('/')
other_parts.insert(0, metadata.xform.user.username)
metadata.data_file = '/'.join(other_parts)
metadata.save(update_fields=['data_file'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 4.2.15 on 2024-12-18 20:00

from django.db import migrations


def add_long_running_migration(apps, schema_editor):
LongRunningMigration = apps.get_model('long_running_migrations', 'LongRunningMigration') # noqa
LongRunningMigration.objects.create(
name='0002_fix_project_ownership_transfer_with_media_files'
)


def noop(*args, **kwargs):
pass


class Migration(migrations.Migration):

dependencies = [
('long_running_migrations', '0001_initial'),
]

operations = [
migrations.RunPython(add_long_running_migration, noop),
]

0 comments on commit d5181d7

Please sign in to comment.