Skip to content

feat(scripts): find and remove papers whose file is gone - #572

Open
Dx-Alz-xD wants to merge 1 commit into
CodeChefVIT:stagingfrom
Dx-Alz-xD:krish/remove-ghost-papers
Open

Dx-Alz-xD wants to merge 1 commit into
CodeChefVIT:stagingfrom
Dx-Alz-xD:krish/remove-ghost-papers

Conversation

@Dx-Alz-xD

Copy link
Copy Markdown

closes #545

adds scripts/remove-ghost-papers.mjs for removing the records whose pdf no longer exists in the bucket

usage, with MONGODB_URI in .env:

  • npm run ghost-papers lists the ghost records and deletes nothing
  • npm run ghost-papers -- --delete removes them

how it decides

  • sends a HEAD request to file_url for every record in papers and admins (the upload queue), same as the viewer check from fix: Resolve bulk downloads, PostHog crash, and missing file UX #544
  • only a 404 counts as missing. 403, 5xx, timeouts, unreachable hosts and records without a file_url are listed as "could not be checked" and never deleted

safety

  • dry run unless --delete is passed
  • before deleting it writes the full records to ghost-papers-.json (gitignored) so they can be restored
  • deletes by _id and file_url together, so a record that was re-uploaded in the meantime is kept
  • refuses to delete when more than half the files are missing, since a moved or renamed bucket also answers 404

tested against an in-memory mongodb and a local file server: the dry run changed nothing, --delete removed exactly the 404 records from both collections and kept the 403/500/unreachable/no-url ones, the backup had the full records, a second run found nothing, and the more-than-half guard refused with all files missing. also checked that storage.googleapis.com answers 404 for a missing object in a public bucket

notes

  • --env-file needs node 20.6 or newer
  • only file_url is checked, a missing thumbnail alone doesn't count

adds scripts/remove-ghost-papers.mjs, run with npm run ghost-papers. it
sends a HEAD request to the file_url of every record in papers and admins,
the same check the viewer does, and lists the ones that answer 404.
anything else (403, 5xx, timeouts, no url) is left alone

it is a dry run unless --delete is passed. with --delete it saves the full
records to ghost-papers-<time>.json first, then deletes them, and only if
their file_url has not changed since the check. it refuses to delete when
more than half the files are missing, since a moved or renamed bucket
also answers 404

refs CodeChefVIT#545

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Remove "ghost" papers from MongoDB where GCS file is deleted

1 participant