Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle exceptions from running migrations #2661

Merged
merged 3 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,36 +1,64 @@
# 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 = [
("logger", "0019_alter_project_options_and_more"),
]

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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down