Updated functionalities 2
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
"""Phase 12: Add performance indexes on high-filter columns
|
||||
|
||||
Revision ID: phase12_performance_indexes
|
||||
Revises: phase11_director_role
|
||||
Create Date: 2026-04-25
|
||||
|
||||
Rationale
|
||||
---------
|
||||
The following columns are filtered or ordered on every page load but had no
|
||||
DB index, causing full table scans as row counts grow:
|
||||
|
||||
inspections
|
||||
- status — filtered on list/dashboard/SLA queries
|
||||
- facility_id — filtered for customer-scoped views and reports
|
||||
- inspector_id — filtered for inspector-scoped views
|
||||
- inspection_date — used in all trend/score queries (ORDER BY, range filter)
|
||||
|
||||
issues
|
||||
- status — filtered on every issues list/dashboard load
|
||||
- severity — filtered in dashboard breakdown and issues list
|
||||
- assigned_to — filtered for inspector-scoped views
|
||||
- reported_at — used for ordering
|
||||
|
||||
All indexes are created with IF NOT EXISTS so the migration is safe to re-run.
|
||||
"""
|
||||
|
||||
from alembic import op
|
||||
|
||||
revision = 'phase12_performance_indexes'
|
||||
down_revision = 'phase11_director_role'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
|
||||
def upgrade():
|
||||
# ── inspections ──────────────────────────────────────────────────────────
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_inspections_status "
|
||||
"ON inspections (status)"
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_inspections_facility_id "
|
||||
"ON inspections (facility_id)"
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_inspections_inspector_id "
|
||||
"ON inspections (inspector_id)"
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_inspections_inspection_date "
|
||||
"ON inspections (inspection_date)"
|
||||
)
|
||||
|
||||
# ── issues ───────────────────────────────────────────────────────────────
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_issues_status "
|
||||
"ON issues (status)"
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_issues_severity "
|
||||
"ON issues (severity)"
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_issues_assigned_to "
|
||||
"ON issues (assigned_to)"
|
||||
)
|
||||
op.execute(
|
||||
"CREATE INDEX IF NOT EXISTS ix_issues_reported_at "
|
||||
"ON issues (reported_at)"
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.execute("DROP INDEX IF EXISTS ix_inspections_status ON inspections")
|
||||
op.execute("DROP INDEX IF EXISTS ix_inspections_facility_id ON inspections")
|
||||
op.execute("DROP INDEX IF EXISTS ix_inspections_inspector_id ON inspections")
|
||||
op.execute("DROP INDEX IF EXISTS ix_inspections_inspection_date ON inspections")
|
||||
|
||||
op.execute("DROP INDEX IF EXISTS ix_issues_status ON issues")
|
||||
op.execute("DROP INDEX IF EXISTS ix_issues_severity ON issues")
|
||||
op.execute("DROP INDEX IF EXISTS ix_issues_assigned_to ON issues")
|
||||
op.execute("DROP INDEX IF EXISTS ix_issues_reported_at ON issues")
|
||||
Reference in New Issue
Block a user