Skip to content

Commit 0439937

Browse files
authored
fix: index expense groups to avoid timeout (#923)
1 parent d9a22db commit 0439937

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Generated by Django 4.2.28 on 2026-02-12 08:26
2+
3+
from django.db import migrations, models
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('fyle', '0042_expense_expenses_workspa_ad984e_idx'),
10+
]
11+
12+
operations = [
13+
migrations.AddIndex(
14+
model_name='expensegroup',
15+
index=models.Index(condition=models.Q(('exported_at__isnull', True)), fields=['workspace_id', 'fund_source'], name='eg_ws_fund_null_idx'),
16+
),
17+
]

apps/fyle/models.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@
44
from django.contrib.postgres.aggregates import ArrayAgg
55
from django.contrib.postgres.fields import ArrayField
66
from django.db import models
7-
from django.db.models import Count, JSONField
7+
from django.db.models import Count, JSONField, Q
88
from django.db.models.fields.json import KeyTextTransform
99
from fyle_accounting_library.fyle_platform.constants import IMPORTED_FROM_CHOICES
1010
from fyle_accounting_library.fyle_platform.enums import ExpenseImportSourceEnum
1111
from fyle_accounting_mappings.mixins import AutoAddCreateUpdateInfoMixin
1212
from fyle_accounting_mappings.models import ExpenseAttribute
1313

1414
from apps.users.models import User
15+
from apps.workspaces.enums import (
16+
ExportTypeEnum,
17+
SystemCommentEntityTypeEnum,
18+
SystemCommentIntentEnum,
19+
SystemCommentReasonEnum,
20+
SystemCommentSourceEnum,
21+
)
1522
from apps.workspaces.models import Configuration, Workspace
1623
from apps.workspaces.system_comments import add_system_comment
17-
from apps.workspaces.enums import ExportTypeEnum, SystemCommentEntityTypeEnum, SystemCommentIntentEnum, SystemCommentReasonEnum, SystemCommentSourceEnum
1824

1925
ALLOWED_FIELDS = [
2026
'employee_email', 'report_id', 'claim_number', 'settlement_id',
@@ -490,6 +496,13 @@ class ExpenseGroup(models.Model):
490496

491497
class Meta:
492498
db_table = 'expense_groups'
499+
indexes = [
500+
models.Index(
501+
fields=['workspace_id', 'fund_source'],
502+
condition=Q(exported_at__isnull=True),
503+
name='eg_ws_fund_null_idx',
504+
),
505+
]
493506

494507
@staticmethod
495508
def create_expense_groups_by_report_id_fund_source(expense_objects: list[Expense], configuration: Configuration, workspace_id: int, system_comments: list = None) -> None:

apps/internal/migrations/0001_auto_generated_sql.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ class Migration(migrations.Migration):
5757
('fyle', '0036_auto_20250108_0702'),
5858
('users', '0002_auto_20201228_0802'),
5959
('mappings', '0016_auto_20250108_0702'),
60-
('tasks', '0010_alter_tasklog_expense_group'),
61-
('workspaces','0043_configuration_skip_accounting_export_summary_post'),
60+
('tasks', '0016_tasklog_is_attachment_upload_failed'),
61+
('workspaces','0060_workspace_org_settings'),
6262
('sage_intacct', '0030_auto_20241112_0425'),
6363
] # This is the first migration
6464
operations = safe_run_sql(sql_files)

0 commit comments

Comments
 (0)