Skip to content

Commit

Permalink
Apply indexes concurrently to prevent lock writes
Browse files Browse the repository at this point in the history
Signed-off-by: Kipchirchir Sigei <[email protected]>
  • Loading branch information
KipSigei committed Aug 6, 2023
1 parent fe4c26f commit d8d1feb
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 16 deletions.
13 changes: 8 additions & 5 deletions onadata/apps/logger/migrations/0004_update_instance_geoms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@

from django.db import migrations
from onadata.apps.logger.models.instance import Instance
from onadata.libs.utils.model_tools import queryset_iterator


def update_instance_geoms(apps, schema_editor):
"""
Update instance geom field with valid geom values
"""
for inst in Instance.objects.filter(
deleted_at__isnull=True,
xform__downloadable=True,
xform__deleted_at__isnull=True,
for inst in queryset_iterator(
Instance.objects.filter(
deleted_at__isnull=True,
xform__downloadable=True,
xform__deleted_at__isnull=True,
)
):
if inst.geom and inst.geom.empty:
inst.geom = None
Expand All @@ -21,7 +24,7 @@ def update_instance_geoms(apps, schema_editor):
class Migration(migrations.Migration):

dependencies = [
('logger', '0003_alter_instance_media_all_received'),
("logger", "0003_alter_instance_media_all_received"),
]

operations = [migrations.RunPython(update_instance_geoms)]
47 changes: 36 additions & 11 deletions onadata/apps/logger/migrations/0005_auto_20230308_0252.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,45 @@


class Migration(migrations.Migration):
atomic = False

dependencies = [
('logger', '0004_update_instance_geoms'),
("logger", "0004_update_instance_geoms"),
]

operations = [
migrations.AlterField(
model_name='instance',
name='date_created',
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name='instance',
name='date_modified',
field=models.DateTimeField(auto_now=True, db_index=True),
),
migrations.SeparateDatabaseAndState(
database_operations=[
migrations.RunSQL(
sql="""
CREATE INDEX "logger_instance_date_created_b2427770"
ON "logger_instance" ("date_created");
""",
reverse_sql="""
DROP INDEX "logger_instance_date_created_b2427770";
""",
),
migrations.RunSQL(
sql="""
CREATE INDEX "logger_instance_date_modified_a32599fc"
ON "logger_instance" ("date_modified");
""",
reverse_sql="""
DROP INDEX "logger_instance_date_modified_a32599fc";
""",
),
],
state_operations=[
migrations.AlterField(
model_name="instance",
name="date_created",
field=models.DateTimeField(auto_now_add=True, db_index=True),
),
migrations.AlterField(
model_name="instance",
name="date_modified",
field=models.DateTimeField(auto_now=True, db_index=True),
),
],
)
]

0 comments on commit d8d1feb

Please sign in to comment.