Skip to content

Prevent orphaned photo uploads - #12

Merged
nedcut merged 3 commits into
mainfrom
codex/prevent-photo-storage-orphans
Jul 11, 2026
Merged

Prevent orphaned photo uploads#12
nedcut merged 3 commits into
mainfrom
codex/prevent-photo-storage-orphans

Conversation

@nedcut

@nedcut nedcut commented Jul 11, 2026

Copy link
Copy Markdown
Owner

What changed

  • key new image and thumbnail objects by content hash and upsert retries onto the same paths
  • make collision cleanup safe for both legacy UUID paths and new hash-based paths
  • preserve potentially live objects when the database outcome is unknown and surface cleanup failures
  • add a dry-run-first orphan purge utility with age and reference safeguards

Why

Failed upload retries used random object paths, leaving unreachable files in Storage. The production purge removed 2,712 existing orphans and reclaimed about 1.8 GB; this change prevents retries from recreating that growth pattern.

Safety

The collision re-check now handles query errors explicitly. It only removes clashing uploads when the existing row points somewhere else (legacy UUID storage), and it never blindly cleans up after an unknown insert outcome.

Validation

  • npm run ci
  • 197/197 tests passed
  • TypeScript clean
  • ESLint: zero errors; one pre-existing useMembership.ts warning
  • node --check scripts/purge-orphan-objects.mjs

Summary by CodeRabbit

  • New Features

    • Added a maintenance tool to identify unreferenced photo and thumbnail files.
    • Supports safe, read-only orphan reports and optional cleanup of confirmed orphaned files.
  • Bug Fixes

    • Improved photo uploads to prevent duplicate files and safely handle retries and concurrent uploads.
    • Added rollback and cleanup protections to avoid leaving unused files after upload or database failures.
    • Preserved existing files when conflicts involve older storage paths.

@vercel

vercel Bot commented Jul 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lofoten-map Ready Ready Preview, Comment Jul 11, 2026 5:46pm

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@nedcut, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 44 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: c46a9d0c-6bb7-4bbd-a028-09e4ae511af1

📥 Commits

Reviewing files that changed from the base of the PR and between b30d2cd and 6e75183.

📒 Files selected for processing (3)
  • lib/photo-upload.test.ts
  • lib/photo-upload.ts
  • scripts/purge-orphan-objects.mjs
📝 Walkthrough

Walkthrough

uploadPhotoBatch now uses content-addressed, idempotent storage paths with clash-aware rollback and expanded tests. A new script inventories and optionally removes old unreferenced Supabase Storage objects, while .orphan-report.json is ignored.

Changes

Photo storage lifecycle

Layer / File(s) Summary
Idempotent upload and rollback behavior
lib/photo-upload.ts, lib/photo-upload.test.ts
Photo uploads use content-hash paths and upsert, preserve legacy references during clash cleanup, normalize insert failures, and test retry, cleanup, and rollback behavior.
Orphan inventory and deletion
scripts/purge-orphan-objects.mjs, .gitignore
The maintenance script reports live, new, missing, and orphaned objects, and in --apply mode rechecks and batch-deletes confirmed orphans; generated reports are ignored.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant Client
  participant uploadPhotoBatch
  participant SupabaseStorage
  participant PhotosTable
  Client->>uploadPhotoBatch: submit photo batch
  uploadPhotoBatch->>SupabaseStorage: upsert content-addressed objects
  uploadPhotoBatch->>PhotosTable: check clashes and insert fresh rows
  uploadPhotoBatch-->>Client: return results and warnings
Loading
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 37.50% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly captures the main goal of preventing orphaned photo uploads.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/prevent-photo-storage-orphans

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@nedcut
nedcut marked this pull request as ready for review July 11, 2026 17:29
Copilot AI review requested due to automatic review settings July 11, 2026 17:29

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@lib/photo-upload.ts`:
- Around line 218-229: The catch block’s failedClientIds handling duplicates IDs
already recorded by the clash check when insertion throws. Update the catch
logic in the surrounding upload-save flow to append only rows whose client_id
was not previously added, preserving existing error-message handling and
ensuring each failed client ID appears once.
- Around line 126-127: Remove the removeObjects call from the imageUpload.error
branch in the photo upload flow, preserving successfully uploaded thumbnails for
retries or later orphan purging even when the image upload fails.

In `@scripts/purge-orphan-objects.mjs`:
- Around line 58-75: Update the apply-mode inventory operations in
fetchReferencedPaths and the related object-listing and deletion flows to use
the service-role writeKey rather than the anonymous key. Preserve read-only
behavior if applicable, but ensure every authoritative row fetch and storage
listing used to classify or delete orphans passes key: writeKey to rest.
- Around line 159-171: The purge flow around fetchReferencedPaths, confirmed,
and the deletion loop must prevent uploads or retries from creating references
between confirmation and deletion. Acquire and hold the existing shared
maintenance/advisory lock before re-reading rows, retain it through all
confirmed deletions, and release it afterward, ensuring upload paths honor the
same lock before creating references.
- Around line 104-113: Update partitionOrphans so objects with missing or
invalid createdAt values are retained in tooNew rather than classified as
orphans. Validate the parsed timestamp before applying the cutoff comparison,
while preserving the existing referenced-object handling and age-based orphan
classification for valid timestamps.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: a34ddc0e-68ac-4211-b626-b176ebadc1e7

📥 Commits

Reviewing files that changed from the base of the PR and between 70b8518 and b30d2cd.

📒 Files selected for processing (4)
  • .gitignore
  • lib/photo-upload.test.ts
  • lib/photo-upload.ts
  • scripts/purge-orphan-objects.mjs

Comment thread lib/photo-upload.ts Outdated
Comment thread lib/photo-upload.ts Outdated
Comment thread scripts/purge-orphan-objects.mjs Outdated
Comment thread scripts/purge-orphan-objects.mjs
Comment thread scripts/purge-orphan-objects.mjs Outdated
A unique-violation insert usually means a racer inserted one of the
batch's hashes after the clash check, and that row references the same
content-addressed paths this batch uploaded. Blindly rolling back the
fresh rows' objects would delete the racer's live objects, so re-query
which hashes gained rows and remove only unreferenced paths; if the
re-check itself fails, leave the objects for the retry/purge to handle.

Also key the purge script's min-age guard on updated_at: a retried
upload upserts onto its old key without resetting created_at, so a
just-re-uploaded object awaiting its row insert must count as new.

@nedcut nedcut left a comment

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

from grok 4.5

Comment thread scripts/purge-orphan-objects.mjs Outdated
Comment thread scripts/purge-orphan-objects.mjs Outdated
Comment thread lib/photo-upload.ts Outdated
Comment thread lib/photo-upload.ts Outdated
Comment thread scripts/purge-orphan-objects.mjs Outdated
@nedcut
nedcut merged commit 97eda21 into main Jul 11, 2026
4 checks passed
@nedcut
nedcut deleted the codex/prevent-photo-storage-orphans branch July 11, 2026 17:46
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