04/25 fix phase12
This commit is contained in:
@@ -21,10 +21,24 @@ DB index, causing full table scans as row counts grow:
|
|||||||
- assigned_to — filtered for inspector-scoped views
|
- assigned_to — filtered for inspector-scoped views
|
||||||
- reported_at — used for ordering
|
- reported_at — used for ordering
|
||||||
|
|
||||||
All indexes are created with IF NOT EXISTS so the migration is safe to re-run.
|
Existence checks use information_schema so the migration is safe to re-run
|
||||||
|
on any MySQL version (compatible back to 5.7).
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from alembic import op
|
from alembic import op
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
|
|
||||||
|
def _index_exists(conn, table: str, index_name: str) -> bool:
|
||||||
|
"""Return True if the named index already exists on the given table."""
|
||||||
|
result = conn.execute(text(
|
||||||
|
"SELECT COUNT(*) FROM information_schema.statistics "
|
||||||
|
"WHERE table_schema = DATABASE() "
|
||||||
|
" AND table_name = :table "
|
||||||
|
" AND index_name = :index"
|
||||||
|
), {'table': table, 'index': index_name})
|
||||||
|
return result.scalar() > 0
|
||||||
|
|
||||||
|
|
||||||
revision = 'phase12_performance_indexes'
|
revision = 'phase12_performance_indexes'
|
||||||
down_revision = 'phase11_director_role'
|
down_revision = 'phase11_director_role'
|
||||||
@@ -32,51 +46,32 @@ branch_labels = None
|
|||||||
depends_on = None
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
def upgrade():
|
# (table, index_name, column)
|
||||||
# ── inspections ──────────────────────────────────────────────────────────
|
INDEXES = [
|
||||||
op.execute(
|
('inspections', 'ix_inspections_status', 'status'),
|
||||||
"CREATE INDEX IF NOT EXISTS ix_inspections_status "
|
('inspections', 'ix_inspections_facility_id', 'facility_id'),
|
||||||
"ON inspections (status)"
|
('inspections', 'ix_inspections_inspector_id', 'inspector_id'),
|
||||||
)
|
('inspections', 'ix_inspections_inspection_date', 'inspection_date'),
|
||||||
op.execute(
|
('issues', 'ix_issues_status', 'status'),
|
||||||
"CREATE INDEX IF NOT EXISTS ix_inspections_facility_id "
|
('issues', 'ix_issues_severity', 'severity'),
|
||||||
"ON inspections (facility_id)"
|
('issues', 'ix_issues_assigned_to', 'assigned_to'),
|
||||||
)
|
('issues', 'ix_issues_reported_at', 'reported_at'),
|
||||||
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(
|
def upgrade():
|
||||||
"CREATE INDEX IF NOT EXISTS ix_issues_status "
|
conn = op.get_bind()
|
||||||
"ON issues (status)"
|
for table, index_name, column in INDEXES:
|
||||||
)
|
if not _index_exists(conn, table, index_name):
|
||||||
op.execute(
|
op.execute(text(
|
||||||
"CREATE INDEX IF NOT EXISTS ix_issues_severity "
|
f'CREATE INDEX {index_name} ON {table} ({column})'
|
||||||
"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():
|
def downgrade():
|
||||||
op.execute("DROP INDEX IF EXISTS ix_inspections_status ON inspections")
|
conn = op.get_bind()
|
||||||
op.execute("DROP INDEX IF EXISTS ix_inspections_facility_id ON inspections")
|
for table, index_name, _column in INDEXES:
|
||||||
op.execute("DROP INDEX IF EXISTS ix_inspections_inspector_id ON inspections")
|
if _index_exists(conn, table, index_name):
|
||||||
op.execute("DROP INDEX IF EXISTS ix_inspections_inspection_date ON inspections")
|
op.execute(text(
|
||||||
|
f'DROP INDEX {index_name} ON {table}'
|
||||||
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