Skip to content

feat(scripts): remove duplicate paper requests before the unique index - #579

Open
Rithish-2914 wants to merge 1 commit into
CodeChefVIT:stagingfrom
Rithish-2914:fix/dedupe-paper-requests
Open

Rithish-2914 wants to merge 1 commit into
CodeChefVIT:stagingfrom
Rithish-2914:fix/dedupe-paper-requests

Conversation

@Rithish-2914

Copy link
Copy Markdown
Contributor

Purpose

#571 adds a unique index on subject + exam + slot + year to PaperRequest, but prod already has duplicate requests, so Mongo can't build the index. This script cleans them up so #571 can be merged.

Corresponding issue: related to #550, unblocks #571

Changes

  • scripts/dedupe-paper-requests.mjs, run with npm run dedupe-requests
    • dry run by default: lists duplicate groups and what would change
    • groups by subject, exam, slot and year, ignoring surrounding spaces (fix(request): drop duplicate paper requests and validate the body #571 trims input)
    • --delete: keeps the oldest request in each group, removes the rest, trims kept rows
    • backs up removed/trimmed rows to paper-request-duplicates-<time>.json (EJSON, gitignored)
    • reports exams outside the allowed list without touching them
    • checks again at the end and fails if new duplicates came in meanwhile
    • --collection=<name> to point at another collection

Testing

  • dry run on the staging DB: 26 requests, 0 duplicates, nothing changed
  • --delete on an in-memory MongoDB seeded with exact duplicates, space-only duplicates, a request without createdAt and an unknown exam: keeps the oldest, trims, backup restores ids and dates exactly, unique index builds afterwards, second run does nothing

Additional Notes

Order for prod: npm run dedupe-requests → check the output → npm run dedupe-requests -- --delete → merge #571 soon after, since repeat requests can come in before it's live (re-running the script fixes that).
Needs Node 20.6+ for --env-file.

CodeChefVIT#571 adds a unique index on subject + exam + slot + year to PaperRequest,
but production already has duplicate requests, so the index can't be built.

scripts/dedupe-paper-requests.mjs (npm run dedupe-requests):
- dry run by default, lists duplicate groups and what would change
- groups by the four fields with surrounding spaces ignored, matching the
  trimming /api/request does after CodeChefVIT#571
- --delete keeps the oldest request per group, removes the rest and trims
  kept rows, after backing them up to paper-request-duplicates-<time>.json
  (EJSON, so ids and dates restore exactly; gitignored)
- reports exams outside the allowed list without touching them
- checks again afterwards and fails if new duplicates came in meanwhile
@YOGESH-08

Copy link
Copy Markdown
Collaborator

looks safe to run 👀

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.

2 participants