fix(retain): preserve document created_at across upsert + UI edit flow#1194
Merged
Conversation
Re-ingesting a document via retain with the same document_id deletes and reinserts the documents row, which reset created_at to NOW(). The ON CONFLICT DO UPDATE branch preserved it, but was never reached because the explicit DELETE removed the row first. - Capture created_at via RETURNING on the DELETE and pass it through to _upsert_document_row, which now uses COALESCE($7, NOW()) on INSERT. - updated_at continues to advance on every insert/update. Control plane: - File upload defaults document_id to the file name so uploads keep a meaningful identifier instead of a server-generated UUID. - Documents table shows an "Updated" column alongside "Created". - Document detail panel supports editing original_text; Save calls retain with the same document_id and preserves the original context, event date, metadata, and tags, triggering the upsert path. Regression test added for created_at preservation.
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.
Summary
document_idused to resetcreated_attoNOW()because the explicit DELETE on the documents row ran before theINSERT ... ON CONFLICT DO UPDATE— so theON CONFLICTbranch was never reached. Now weRETURNING created_aton the DELETE and pass it through to the INSERT (COALESCE($7, NOW())), preserving the original creation timestamp.updated_atstill advances on every write.document_idto the file name instead of letting the server generate a UUID.Updatedcolumn alongsideCreated.original_text. Save callsretainwith the samedocument_idplus the originalcontext,event_date,metadata, andtags(preserved fromretain_params), triggering the upsert path — which now correctly keepscreated_at.Test plan
uv run pytest tests/test_retain.py::test_document_upsert_preserves_created_at— new regression test (passes)uv run pytest tests/test_retain.py::test_document_upsert_behavior tests/test_delta_retain_duplicates.py— existing upsert + concurrent-upsert suite (5 passed)./scripts/hooks/lint.sh— greendocument_iddefaults to the file nameupdated_atadvances whilecreated_atstays stable, and tags/metadata/context are preserved