from pathlib import Path


try:
    Attachment = env["ir.attachment"].sudo()
    assets = Attachment.search([("url", "like", "/web/assets/%")])
    missing_assets = assets.filtered(
        lambda attachment: attachment.store_fname
        and not Path(attachment._full_path(attachment.store_fname)).is_file()
    )
    missing_ids = missing_assets.ids
    missing_assets.unlink()
    env.cr.commit()
    print(f"MISSING_ASSET_REPAIR removed={len(missing_ids)} ids={missing_ids}")
except Exception:
    env.cr.rollback()
    raise
