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

Add indexes to instance model date fields #2393

Merged
merged 6 commits into from
Aug 7, 2023
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
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)]
64 changes: 64 additions & 0 deletions onadata/apps/logger/migrations/0008_add_date_fields_indexing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Generated by Django 3.2.20 on 2023-08-07 08:11

from django.db import migrations, models


class Migration(migrations.Migration):
atomic = False

dependencies = [
('logger', '0007_alter_projectinvitation_unique_together'),
]

operations = [
migrations.SeparateDatabaseAndState(
database_operations=[
# index the json date fields
migrations.RunSQL(
sql='CREATE INDEX CONCURRENTLY "logger_inst_date_cr_json_42899d_idx" ON "logger_instance" ((json->>\'_date_created\')) WHERE (json->>\'_date_created\') IS NOT NULL;',
reverse_sql='DROP INDEX "logger_inst_date_cr_json_42899d_idx";',
),
migrations.RunSQL(
sql='CREATE INDEX CONCURRENTLY "logger_inst_date_mo_json_5a1bd3_idx" ON "logger_instance" ((json->>\'_date_modified\')) WHERE (json->>\'_date_modified\') IS NOT NULL;',
reverse_sql='DROP INDEX "logger_inst_date_mo_json_5a1bd3_idx";',
),
migrations.RunSQL(
sql='CREATE INDEX CONCURRENTLY "logger_inst_deleted_at_json_da31a3_idx" ON "logger_instance" ((json->>\'_deleted_at\')) WHERE (json->>\'_deleted_at\') IS NOT NULL;',
reverse_sql='DROP INDEX "logger_inst_deleted_at_json_da31a3_idx";',
),
# index model date fields
migrations.RunSQL(
sql='CREATE INDEX CONCURRENTLY "logger_inst_date_cr_42899d_idx" ON "logger_instance" ("date_created");',
reverse_sql='DROP INDEX "logger_inst_date_cr_42899d_idx";',
),
migrations.RunSQL(
sql='CREATE INDEX CONCURRENTLY "logger_inst_date_mo_5a1bd3_idx" ON "logger_instance" ("date_modified");',
reverse_sql='DROP INDEX "logger_inst_date_mo_5a1bd3_idx";',
),
migrations.RunSQL(
sql='CREATE INDEX CONCURRENTLY "logger_inst_deleted_at_da31a3_idx" ON "logger_instance" ("deleted_at");',
reverse_sql='DROP INDEX "logger_inst_deleted_at_da31a3_idx";',
),
],
state_operations=[
migrations.AddIndex(
model_name="instance",
index=models.Index(
fields=["date_created"], name="logger_inst_date_cr_42899d_idx"
),
),
migrations.AddIndex(
model_name="instance",
index=models.Index(
fields=["date_modified"], name="logger_inst_date_mo_5a1bd3_idx"
),
),
migrations.AddIndex(
model_name="instance",
index=models.Index(
fields=["deleted_at"], name="logger_inst_deleted_at_da31a3_idx"
),
),
],
)
]
5 changes: 5 additions & 0 deletions onadata/apps/logger/models/instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -697,6 +697,11 @@ class Instance(models.Model, InstanceBaseClass):
class Meta:
app_label = "logger"
unique_together = ("xform", "uuid")
indexes = [
models.Index(fields=['date_created']),
models.Index(fields=['date_modified']),
models.Index(fields=['deleted_at']),
]

@classmethod
def set_deleted_at(cls, instance_id, deleted_at=timezone.now(), user=None):
Expand Down
Loading