perf(indexes): add critical indexes for Test and AuditLog models (P0)
Some checks failed
Aegis CI / lint-and-test (push) Has been cancelled

- Declare __table_args__ on Test with 5 indexes: technique_id, state, created_at, (technique_id,state), (state,created_at)

- Declare __table_args__ on AuditLog with 3 indexes: (entity_type,entity_id), timestamp, (entity_type,entity_id,action)

- Alembic b024: create only the 2 new indexes (ix_tests_created_at, ix_tests_state_created_at); existing indexes from b005/b018/b019 are preserved

- Model index names aligned with existing migration names to prevent duplicates
This commit is contained in:
2026-02-18 12:12:54 +01:00
parent 51c927394d
commit 898bb7e4e7
3 changed files with 53 additions and 2 deletions

View File

@@ -0,0 +1,37 @@
"""add_critical_test_audit_indexes
Add missing critical indexes for tests and audit_logs tables to match
model __table_args__ declarations. Existing indexes (from b005, b018,
b019) are left untouched; only the two genuinely new indexes are created.
Revision ID: b024critidx
Revises: b023mustchgpwd
Create Date: 2026-02-18 12:00:00.000000
"""
from typing import Sequence, Union
from alembic import op
revision: str = "b024critidx"
down_revision: Union[str, None] = "b023mustchgpwd"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
op.create_index(
"ix_tests_created_at",
"tests",
["created_at"],
)
op.create_index(
"ix_tests_state_created_at",
"tests",
["state", "created_at"],
)
def downgrade() -> None:
op.drop_index("ix_tests_state_created_at", table_name="tests")
op.drop_index("ix_tests_created_at", table_name="tests")

View File

@@ -1,5 +1,5 @@
import uuid
from sqlalchemy import Column, String, DateTime, ForeignKey, func
from sqlalchemy import Column, String, DateTime, ForeignKey, Index, func
from sqlalchemy.dialects.postgresql import UUID, JSONB
from sqlalchemy.orm import relationship
@@ -25,3 +25,9 @@ class AuditLog(Base):
# Relationships
user = relationship("User")
__table_args__ = (
Index("ix_audit_logs_entity", "entity_type", "entity_id"),
Index("ix_audit_logs_timestamp", "timestamp"),
Index("ix_audit_logs_entity_type_entity_id_action", "entity_type", "entity_id", "action"),
)

View File

@@ -1,5 +1,5 @@
import uuid
from sqlalchemy import Column, String, Text, Boolean, Integer, DateTime, ForeignKey, Enum, func
from sqlalchemy import Column, String, Text, Boolean, Integer, DateTime, ForeignKey, Enum, Index, func
from sqlalchemy.dialects.postgresql import UUID
from sqlalchemy.orm import relationship
@@ -72,3 +72,11 @@ class Test(Base):
remediation_user = relationship("User", foreign_keys=[remediation_assignee])
original_test = relationship("Test", remote_side="Test.id", foreign_keys=[retest_of])
retests = relationship("Test", foreign_keys=[retest_of], back_populates="original_test")
__table_args__ = (
Index("ix_tests_technique_id", "technique_id"),
Index("ix_tests_state", "state"),
Index("ix_tests_created_at", "created_at"),
Index("ix_tests_technique_state", "technique_id", "state"),
Index("ix_tests_state_created_at", "state", "created_at"),
)