Files
leocrm/scripts/check_indexes.py
T
leocrm-bot 69e91fd5d0 T10: Monitoring, Performance, Documentation & Environment Config — 38 tests, ruff clean, docs OK
- Extended health endpoint: DB+Redis+Storage+Worker checks with degraded status
- Prometheus metrics endpoint: admin-only, text/plain format
- Metrics: http_requests_total, db_pool_connections, arq_jobs_total
- Structured JSON logging (structlog): timestamp, level, method, path, status, duration_ms, tenant_id
- Performance: page_size max 100 enforced (422), streaming CSV export (StreamingResponse)
- Scripts: seed_perf_data.py, check_indexes.py
- Docs: admin-guide.md (Deploy, Backup, Restore, Env-Vars, Troubleshooting), api-overview.md
- README updated: prod setup, API section, env profiles, admin-guide link
- .env.example: added SECRET_KEY, STORAGE_PATH, SMTP_* vars
- 38 new tests, full regression 564/564 pass (0 failures)
- Ruff: all checks passed
2026-07-01 23:15:35 +02:00

90 lines
2.5 KiB
Python

#!/usr/bin/env python3
"""Check database indexes — verify all required indexes exist for performance.
Usage:
python scripts/check_indexes.py
Checks that critical indexes for contacts, companies, and tenant scoping
are present in the database.
"""
from __future__ import annotations
import asyncio
import os
import sys
# Add project root to path
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from sqlalchemy import text
from sqlalchemy.ext.asyncio import create_async_engine
from app.config import get_settings
# Required indexes for performance (table, index_name)
REQUIRED_INDEXES = [
("contacts", "ix_contacts_tenant_deleted"),
("contacts", "ix_contacts_tenant_name"),
("contacts", "ix_contacts_email"),
("companies", "ix_companies_tenant_deleted"),
("companies", "ix_companies_tenant_name"),
("companies", "ix_companies_industry"),
("company_contacts", "ix_cc_company"),
("company_contacts", "ix_cc_contact"),
]
async def check_indexes() -> int:
"""Check that all required indexes exist in the database.
Returns 0 if all indexes present, 1 if any missing.
"""
settings = get_settings()
engine = create_async_engine(settings.database_url)
missing: list[tuple[str, str]] = []
present: list[tuple[str, str]] = []
async with engine.connect() as conn:
for table_name, index_name in REQUIRED_INDEXES:
result = await conn.execute(
text(
"SELECT 1 FROM pg_indexes "
"WHERE schemaname = 'public' "
"AND tablename = :table "
"AND indexname = :index"
),
{"table": table_name, "index": index_name},
)
if result.scalar():
present.append((table_name, index_name))
print(f"{table_name}.{index_name}")
else:
missing.append((table_name, index_name))
print(f" ✗ MISSING: {table_name}.{index_name}")
await engine.dispose()
print(f"\nSummary: {len(present)} present, {len(missing)} missing")
if missing:
print("\nMissing indexes:")
for table, idx in missing:
print(f" - {table}.{idx}")
return 1
print("\nAll required indexes present!")
return 0
def main():
"""Run index check."""
print("Checking database indexes...")
exit_code = asyncio.run(check_indexes())
sys.exit(exit_code)
if __name__ == "__main__":
main()