70 lines
2.4 KiB
Python
70 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
"""Install BGP prefix geography functions and the large GiST index separately from init_db.
|
|
|
|
Run from the repository root with:
|
|
uv run --frozen --project . python scripts/install_bgp_prefix_geo_indexes.py
|
|
|
|
The index builds concurrently outside a transaction and scans collected_data.
|
|
Schedule it as a maintenance operation; do not run it during normal startup.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from sqlalchemy import text
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
sys.path.insert(0, str(ROOT / "backend"))
|
|
|
|
from app.db.bgp_prefix_geo import ( # noqa: E402
|
|
BGP_PREFIX_GEO_INDEX_NAME,
|
|
BGP_PREFIX_GEO_INDEX_SQL,
|
|
install_bgp_prefix_geo_functions,
|
|
)
|
|
from app.db.session import engine # noqa: E402
|
|
|
|
|
|
async def main() -> None:
|
|
async with engine.connect() as connection:
|
|
connection = await connection.execution_options(isolation_level="AUTOCOMMIT")
|
|
await install_bgp_prefix_geo_functions(connection)
|
|
|
|
invalid = await connection.scalar(
|
|
text("""
|
|
SELECT NOT index_metadata.indisvalid
|
|
FROM pg_class index_class
|
|
JOIN pg_index index_metadata ON index_metadata.indexrelid = index_class.oid
|
|
WHERE index_class.relname = :index_name
|
|
AND index_metadata.indrelid = 'collected_data'::regclass
|
|
"""),
|
|
{"index_name": BGP_PREFIX_GEO_INDEX_NAME},
|
|
)
|
|
if invalid:
|
|
raise RuntimeError(
|
|
f"{BGP_PREFIX_GEO_INDEX_NAME} is invalid; inspect and drop it concurrently before retrying"
|
|
)
|
|
|
|
await connection.execute(text(BGP_PREFIX_GEO_INDEX_SQL))
|
|
valid = await connection.scalar(
|
|
text("""
|
|
SELECT index_metadata.indisvalid
|
|
FROM pg_class index_class
|
|
JOIN pg_index index_metadata ON index_metadata.indexrelid = index_class.oid
|
|
WHERE index_class.relname = :index_name
|
|
AND index_metadata.indrelid = 'collected_data'::regclass
|
|
"""),
|
|
{"index_name": BGP_PREFIX_GEO_INDEX_NAME},
|
|
)
|
|
if not valid:
|
|
raise RuntimeError(f"{BGP_PREFIX_GEO_INDEX_NAME} was not installed as a valid index")
|
|
print(f"Installed {BGP_PREFIX_GEO_INDEX_NAME}")
|
|
|
|
await engine.dispose()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
asyncio.run(main())
|