Skip to content
Open
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
17 changes: 17 additions & 0 deletions spp_programs/models/entitlement_base_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,23 @@ class SPPEntitlement(models.Model):
_order = "partner_id asc,id desc"
_check_company_auto = True

def init(self):
super().init()
self.env.cr.execute(
"""
CREATE INDEX IF NOT EXISTS
spp_entitlement_cycle_id_partner_id_idx
ON spp_entitlement (cycle_id, partner_id)
"""
)
self.env.cr.execute(
"""
CREATE INDEX IF NOT EXISTS
spp_entitlement_cycle_id_state_idx
ON spp_entitlement (cycle_id, state)
"""
)

@api.model
def _generate_code(self):
return str(uuid4())[4:-8][3:]
Expand Down
10 changes: 10 additions & 0 deletions spp_programs/models/program_membership.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ class SPPProgramMembership(models.Model):
_inherits = {"res.partner": "partner_id"}
_order = "id desc"

def init(self):
super().init()
self.env.cr.execute(
"""
CREATE INDEX IF NOT EXISTS
spp_program_membership_program_id_state_idx
ON spp_program_membership (program_id, state)
"""
)

partner_id = fields.Many2one(
"res.partner",
"Registrant",
Expand Down
1 change: 1 addition & 0 deletions spp_programs/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
from . import test_eligibility_cel_integration
from . import test_compliance_cel
from . import test_create_program_wizard_cel
from . import test_composite_indexes
64 changes: 64 additions & 0 deletions spp_programs/tests/test_composite_indexes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Part of OpenSPP. See LICENSE file for full copyright and licensing details.
from odoo.tests import TransactionCase


class TestCompositeIndexes(TransactionCase):
"""Test that composite indexes exist for frequent query patterns."""

def test_entitlement_cycle_partner_index_exists(self):
"""Composite index on spp_entitlement(cycle_id, partner_id) must exist.

The prepare_entitlements duplicate check searches entitlements by
(cycle_id, partner_id). Without this index, each batch does a
sequential scan.
"""
self.env.cr.execute(
"""
SELECT 1 FROM pg_indexes
WHERE tablename = 'spp_entitlement'
AND indexdef LIKE '%%cycle_id%%'
AND indexdef LIKE '%%partner_id%%'
Comment on lines +19 to +20

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current query to check for the index is not specific enough. It checks for the presence of cycle_id and partner_id in the index definition but doesn't enforce that they are the only columns or their specific order. A more robust test would check for the exact index name and column order.

Suggested change
AND indexdef LIKE '%%cycle_id%%'
AND indexdef LIKE '%%partner_id%%'
AND indexname = 'spp_entitlement_cycle_id_partner_id_idx'
AND indexdef LIKE '%(cycle_id, partner_id)%'

"""
)
self.assertTrue(
self.env.cr.fetchone(),
"Composite index on (cycle_id, partner_id) must exist on spp_entitlement",
)

def test_entitlement_cycle_state_index_exists(self):
"""Composite index on spp_entitlement(cycle_id, state) must exist.

Cycle computed fields (total_amount, show_approve_button,
entitlements_count) filter entitlements by cycle_id and state.
"""
self.env.cr.execute(
"""
SELECT 1 FROM pg_indexes
WHERE tablename = 'spp_entitlement'
AND indexdef LIKE '%%cycle_id%%'
AND indexdef LIKE '%%state%%'
Comment on lines +38 to +39

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The query to verify the index's existence is too broad. It only checks that cycle_id and state are mentioned in the index definition but doesn't validate the column order or confirm it's a composite index specifically on these two columns. This could lead to false positives.

Suggested change
AND indexdef LIKE '%%cycle_id%%'
AND indexdef LIKE '%%state%%'
AND indexname = 'spp_entitlement_cycle_id_state_idx'
AND indexdef LIKE '%(cycle_id, state)%'

"""
)
self.assertTrue(
self.env.cr.fetchone(),
"Composite index on (cycle_id, state) must exist on spp_entitlement",
)

def test_program_membership_program_state_index_exists(self):
"""Composite index on spp_program_membership(program_id, state) must exist.

get_beneficiaries() and count_beneficiaries() filter by
(program_id, state) on every async batch dispatch.
"""
self.env.cr.execute(
"""
SELECT 1 FROM pg_indexes
WHERE tablename = 'spp_program_membership'
AND indexdef LIKE '%%program_id%%'
AND indexdef LIKE '%%state%%'
Comment on lines +57 to +58

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The test for the program membership index is not specific enough. It confirms that program_id and state are in the index definition but doesn't check their order or if they are the only columns. This could allow an incorrectly defined index to pass the test.

Suggested change
AND indexdef LIKE '%%program_id%%'
AND indexdef LIKE '%%state%%'
AND indexname = 'spp_program_membership_program_id_state_idx'
AND indexdef LIKE '%(program_id, state)%'

"""
)
self.assertTrue(
self.env.cr.fetchone(),
"Composite index on (program_id, state) must exist on spp_program_membership",
)
Loading