Skip to content

fix(migration): migrate guest invoices to Supabase on signup — prevent data loss#7

Draft
cursor[bot] wants to merge 2 commits intomainfrom
cursor/critical-bug-inspection-035a
Draft

fix(migration): migrate guest invoices to Supabase on signup — prevent data loss#7
cursor[bot] wants to merge 2 commits intomainfrom
cursor/critical-bug-inspection-035a

Conversation

@cursor
Copy link
Copy Markdown

@cursor cursor Bot commented Apr 28, 2026

Bug and Impact

Severity: Critical — permanent data loss

migrateGuestData() migrated clients, contracts, and time entries to Supabase when a guest user signs up, but completely skipped invoices. After a successful migration, clearAll() wiped the entire guest store including invoices, permanently deleting all invoices the user created in guest mode.

Trigger scenario: A user works in guest mode, creates invoices (e.g., a freelancer generating an invoice for a client before signing up). When they sign up and the ?migration=pending flow runs, all their invoices are silently and permanently lost.

Secondary bug: The contract insert was missing .select('id'), so even if invoices had been migrated, contract_id foreign key references could not have been remapped from guest nanoids to Supabase UUIDs—resulting in broken invoice→contract links.

Root Cause

The migration function in lib/migration/migrateGuestData.ts had steps for clients (step 1), contracts (step 2), and time entries (step 3), but no step for invoices. The guest store holds invoices (guest.invoices), and the GuestInvoice type is well-defined, but the migration was never implemented for them.

Fix

  • Added invoice migration (step 4) that inserts guest invoices into Supabase with:
    • client_id remapped via clientIdMap (guest nanoid → Supabase UUID)
    • contract_id remapped via a new contractIdMap
    • Computed tax_amount from subtotal and tax_rate (matching createInvoice API logic)
    • line_items deep-cloned via JSON.parse(JSON.stringify(...)) (matching existing pattern)
  • Added .select('id') to the contract insert to build contractIdMap
  • Invoice migration failure throws before clearAll(), preserving guest data (consistent with existing error handling contract)

Validation

  • 4 new tests covering:
    • Invoice migration with proper client_id/contract_id remapping
    • Guest store preservation when invoice migration fails (no data cleared on error)
    • Contract ID map construction (.select('id') chained on contract insert)
    • Invoices with null client/contract references
  • All 21 tests pass (5 test files)
  • TypeScript strict mode passes with no errors
Open in Web View Automation 

CRITICAL BUG: migrateGuestData migrated clients, contracts, and time
entries but completely skipped invoices. After successful migration,
clearAll() wiped all guest data including invoices, causing permanent
data loss for any invoices created in guest mode.

Also fixes: contract insert was missing .select('id'), so even if
invoices had been migrated, contract_id references could not have been
remapped from guest nanoids to Supabase UUIDs.

Changes:
- Add invoice migration step (step 4) with proper client_id and
  contract_id remapping via ID maps
- Add .select('id') to contract insert to build contractIdMap
- Add tests covering invoice migration, error rollback safety,
  contract ID mapping, and orphan invoice handling

Co-authored-by: Siddhartha Dwivedi <info@siddart.net>
@vercel
Copy link
Copy Markdown

vercel Bot commented Apr 28, 2026

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

Project Deployment Actions Updated (UTC)
stacklite-live Ready Ready Preview, Comment Apr 28, 2026 11:11am

@supabase
Copy link
Copy Markdown

supabase Bot commented Apr 28, 2026

This pull request has been ignored for the connected project gakcnwqvkusuifdqjqzz because there are no changes detected in supabase directory. You can change this behaviour in Project Integrations Settings ↗︎.


Preview Branches by Supabase.
Learn more about Supabase Branching ↗︎.

Co-authored-by: Siddhartha Dwivedi <info@siddart.net>
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.

1 participant