"""Project existing article sections into the section index. `_rebuild_section_index` runs when an article is saved, so articles written before that existed have no rows — the index was empty and section-level retrieval had nothing to search. docker compose exec backend python -m scripts.backfill_section_index docker compose exec backend python -m scripts.backfill_section_index --apply """ import sys from app.database import SessionLocal from app.models.article import Article from app.routers.articles import _rebuild_section_index def main(): apply_changes = "--apply" in sys.argv db = SessionLocal() try: articles = db.query(Article).all() sections = sum(len(a.sections or []) for a in articles) print(f" articles : {len(articles)}") print(f" sections : {sections}") if apply_changes: for article in articles: _rebuild_section_index(db, article) db.commit() print(" indexed and embedded.") else: print("\n Re-run with --apply to build the index.") finally: db.close() if __name__ == "__main__": sys.exit(main())