fix(migration): migrate guest invoices to Supabase on signup — prevent data loss#7
Draft
cursor[bot] wants to merge 2 commits intomainfrom
Draft
fix(migration): migrate guest invoices to Supabase on signup — prevent data loss#7cursor[bot] wants to merge 2 commits intomainfrom
cursor[bot] wants to merge 2 commits intomainfrom
Conversation
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>
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
|
This pull request has been ignored for the connected project Preview Branches by Supabase. |
Co-authored-by: Siddhartha Dwivedi <info@siddart.net>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=pendingflow 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_idforeign 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.tshad 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 theGuestInvoicetype is well-defined, but the migration was never implemented for them.Fix
client_idremapped viaclientIdMap(guest nanoid → Supabase UUID)contract_idremapped via a newcontractIdMaptax_amountfromsubtotalandtax_rate(matchingcreateInvoiceAPI logic)line_itemsdeep-cloned viaJSON.parse(JSON.stringify(...))(matching existing pattern).select('id')to the contract insert to buildcontractIdMapclearAll(), preserving guest data (consistent with existing error handling contract)Validation
client_id/contract_idremapping.select('id')chained on contract insert)