diff --git a/onadata/apps/logger/migrations/0020_rename_logger_inst_deleted_at_da31a3_idx_logger_inst_deleted_da31a3_idx_and_more.py b/onadata/apps/logger/migrations/0020_rename_logger_inst_deleted_at_da31a3_idx_logger_inst_deleted_da31a3_idx_and_more.py index dc126a410a..125c47ab0b 100644 --- a/onadata/apps/logger/migrations/0020_rename_logger_inst_deleted_at_da31a3_idx_logger_inst_deleted_da31a3_idx_and_more.py +++ b/onadata/apps/logger/migrations/0020_rename_logger_inst_deleted_at_da31a3_idx_logger_inst_deleted_da31a3_idx_and_more.py @@ -1,9 +1,56 @@ # Generated by Django 4.2.13 on 2024-07-04 11:26 -from django.db import migrations, models +from django.db import migrations, models, connection import onadata.apps.logger.models.instance +def rename_index_if_exists(apps, schema_editor, old_name, new_name, table_name): + with connection.cursor() as cursor: + # Check if the index exists + cursor.execute( + """ + SELECT COUNT(1) + FROM pg_indexes + WHERE indexname = %s AND tablename = %s + """, + [old_name, table_name], + ) + exists = cursor.fetchone()[0] + if exists: + cursor.execute(f"ALTER INDEX {old_name} RENAME TO {new_name}") + + +def rename_indexes(apps, schema_editor): + rename_index_if_exists( + apps, + schema_editor, + "logger_inst_deleted_at_da31a3_idx", + "logger_inst_deleted_da31a3_idx", + "logger_instance", + ) + rename_index_if_exists( + apps, + schema_editor, + "logger_instance_id_xform_id_index", + "logger_inst_xform_i_504638_idx", + "logger_instance", + ) + rename_index_if_exists( + apps, + schema_editor, + "logger_inst_hist_checksum_05f7bf_idx", + "logger_inst_checksu_05f7bf_idx", + "logger_instancehistory", + ) + rename_index_if_exists( + apps, + schema_editor, + "logger_inst_hist_uuid_f5ae42_idx", + "logger_inst_uuid_f5ae42_idx", + "logger_instancehistory", + ) + + class Migration(migrations.Migration): dependencies = [ @@ -11,26 +58,7 @@ class Migration(migrations.Migration): ] operations = [ - migrations.RenameIndex( - model_name="instance", - new_name="logger_inst_deleted_da31a3_idx", - old_name="logger_inst_deleted_at_da31a3_idx", - ), - migrations.RenameIndex( - model_name="instance", - new_name="logger_inst_xform_i_504638_idx", - old_name="logger_instance_id_xform_id_index", - ), - migrations.RenameIndex( - model_name="instancehistory", - new_name="logger_inst_checksu_05f7bf_idx", - old_name="logger_inst_hist_checksum_05f7bf_idx", - ), - migrations.RenameIndex( - model_name="instancehistory", - new_name="logger_inst_uuid_f5ae42_idx", - old_name="logger_inst_hist_uuid_f5ae42_idx", - ), + migrations.RunPython(rename_indexes, reverse_code=migrations.RunPython.noop), migrations.AlterField( model_name="instance", name="date_created", diff --git a/onadata/apps/logger/migrations/0023_populate_project_entity_list_perm.py b/onadata/apps/logger/migrations/0023_populate_project_entity_list_perm.py index 4e662b7e75..14d695d281 100644 --- a/onadata/apps/logger/migrations/0023_populate_project_entity_list_perm.py +++ b/onadata/apps/logger/migrations/0023_populate_project_entity_list_perm.py @@ -3,36 +3,55 @@ from django.db import migrations from django.contrib.auth import get_user_model from django.contrib.auth.models import Group +from django.contrib.auth.models import Permission +from django.contrib.contenttypes.models import ContentType + from guardian.shortcuts import assign_perm +from multidb.pinning import use_master + def add_project_entitylist_perm(apps, schema_editor): """Assign `add_project_entitylist` permission to existing Owners, Managers""" Project = apps.get_model("logger", "Project") - project_qs = Project.objects.filter(deleted_at__isnull=True) - eta = project_qs.count() + content_type = ContentType.objects.get_for_model(Project) + perm = "add_project_entitylist" - User = get_user_model() - - for project in project_qs.iterator(chunk_size=200): - # Owners and Managers have the `add_project` permission - project_user_obj_perm_qs = project.projectuserobjectpermission_set.filter( - permission__codename="add_project" - ) - project_group_obj_perm_qs = project.projectgroupobjectpermission_set.filter( - permission__codename="add_project" + with use_master: + _, created = Permission.objects.get_or_create( + codename=perm, + content_type=content_type, + defaults={ + "name": "Can add entitylist to project", + }, ) - for perm in project_user_obj_perm_qs.iterator(chunk_size=100): - user = User.objects.get(id=perm.user_id) - _ = assign_perm("add_project_entitylist", user, project) + if created: + print(f"Permission {perm} created because it does not exist") + + project_qs = Project.objects.filter(deleted_at__isnull=True) + eta = project_qs.count() + User = get_user_model() + + for project in project_qs.iterator(chunk_size=200): + # Owners and Managers have the `add_project` permission + project_user_obj_perm_qs = project.projectuserobjectpermission_set.filter( + permission__codename="add_project" + ) + project_group_obj_perm_qs = project.projectgroupobjectpermission_set.filter( + permission__codename="add_project" + ) + + for perm in project_user_obj_perm_qs.iterator(chunk_size=100): + user = User.objects.get(id=perm.user_id) + assign_perm(perm, user, project) - for perm in project_group_obj_perm_qs.iterator(chunk_size=100): - group = Group.objects.get(id=perm.group_id) - _ = assign_perm("add_project_entitylist", group, project) + for perm in project_group_obj_perm_qs.iterator(chunk_size=100): + group = Group.objects.get(id=perm.group_id) + assign_perm(perm, group, project) - eta -= 1 - print("eta", eta) + eta -= 1 + print("eta", eta) class Migration(migrations.Migration):