From 6f8c46ab74fe59273c6d824045913187cd1e48a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20L=C3=A9ger?= Date: Thu, 19 Dec 2024 19:53:12 -0500 Subject: [PATCH] feat(organization): backfill Asset search field for owner label TASK-1334 (#5391) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ### 📣 Summary Populated the asset search field to include the owner username and organization name for improved search functionality. ### 📖 Description The asset search field has been updated to include tthe owner username and organization name, ensuring better usability when searching for asset owner labels. This backfill operation populates the field for existing assets where the owner label was previously missing. --- ...fill_asset_search_field_for_owner_label.py | 42 +++++++++++++++++++ ...fill_asset_search_field_for_owner_label.py | 25 +++++++++++ 2 files changed, 67 insertions(+) create mode 100644 kobo/apps/long_running_migrations/jobs/0004_back_fill_asset_search_field_for_owner_label.py create mode 100644 kobo/apps/long_running_migrations/migrations/0004_back_fill_asset_search_field_for_owner_label.py diff --git a/kobo/apps/long_running_migrations/jobs/0004_back_fill_asset_search_field_for_owner_label.py b/kobo/apps/long_running_migrations/jobs/0004_back_fill_asset_search_field_for_owner_label.py new file mode 100644 index 0000000000..eb790781a0 --- /dev/null +++ b/kobo/apps/long_running_migrations/jobs/0004_back_fill_asset_search_field_for_owner_label.py @@ -0,0 +1,42 @@ +# Generated on 2024-12-19 15:34 +from django.db.models import Prefetch +from more_itertools import chunked + +from kobo.apps.organizations.models import Organization +from kpi.models.asset import Asset + + +def run(): + """ + Transfers all assets owned by members to their respective organizations. + """ + CHUNK_SIZE = 2000 + + assets = ( + Asset.objects.only('search_field', 'name', 'uid', 'owner') + .exclude(search_field__has_keys='owner_username') + .select_related('owner') + .prefetch_related( + Prefetch( + 'owner__organizations_organization', + queryset=Organization.objects.all().order_by( + '-organization_users__created' + ), + to_attr='organizations', + ) + ) + ).iterator(CHUNK_SIZE) + + for asset_batch in chunked(assets, CHUNK_SIZE): + for asset in asset_batch: + try: + organization_name = asset.owner.organizations[0].name + except IndexError: + organization_name = f'{asset.owner.username}’s organization' + + asset.update_search_field( + owner_username=asset.owner.username, + organization_name=organization_name, + ) + + Asset.objects.bulk_update(asset_batch, fields=['search_field']) diff --git a/kobo/apps/long_running_migrations/migrations/0004_back_fill_asset_search_field_for_owner_label.py b/kobo/apps/long_running_migrations/migrations/0004_back_fill_asset_search_field_for_owner_label.py new file mode 100644 index 0000000000..34caffff82 --- /dev/null +++ b/kobo/apps/long_running_migrations/migrations/0004_back_fill_asset_search_field_for_owner_label.py @@ -0,0 +1,25 @@ +# Generated by Django 4.2.15 on 2024-12-19 14:54 + +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='0004_back_fill_asset_search_field_for_owner_label' + ) + + +def noop(*args, **kwargs): + pass + + +class Migration(migrations.Migration): + + dependencies = [ + ('long_running_migrations', '0003_transfer_members_data_ownership_to_org'), + ] + + operations = [ + migrations.RunPython(add_long_running_migration, noop), + ]