refactor(sync): AbstractLwwUpserter + AbstractCursorReader + partial-update preservation#11
Draft
lchogan wants to merge 1 commit into
Draft
refactor(sync): AbstractLwwUpserter + AbstractCursorReader + partial-update preservation#11lchogan wants to merge 1 commit into
lchogan wants to merge 1 commit into
Conversation
…update preservation Addresses three cross-cutting concerns surfaced in Tasks 1.7-1.14 code review: 1. Partial-update preservation: preserve() helper returns the existing model attribute when a key is absent from the push payload, so an omitted optional field (e.g. color_hint, description, ended_at_ms) no longer overwrites the stored value with null. 2. TOCTOU on stale LWW check: existence-check-and-write is now wrapped in DB::transaction with lockForUpdate(), serialising concurrent pushes of the same id so LWW semantics are guaranteed at the database level. 3. Rule-of-four duplication: AbstractLwwUpserter (template-method for upsert guard/write) and AbstractCursorReader (template-method for cursor-paginated read) eliminate the identical scaffolding repeated across 5 upserters and 5 readers. Subclasses now declare only modelClass(), checkOwnership()/scopeForUser(), and applyFields()/ projectRow(). CardSubTopicUpserter, ReviewUpserter, CardSubTopicReader, and ReviewReader are intentionally left standalone; each has a docblock explaining why (two-part ownership / insert-only / nested-whereHas). Tests: 56 → 58 (+2 partial-update preservation tests for Deck.description and Topic.color_hint). PHPStan level-9 clean. Pint clean. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
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
Cross-cutting follow-up to Phase 1 (merged in #9), addressing three concerns surfaced during code review:
$row['field'] ?? nullno longer overwrites existing values when the client omits an optional key. A newpreserve()helper onAbstractLwwUpserterreturns the existing attribute value when a key is absent.DB::transactionwithlockForUpdate()on the target row. Two concurrent pushes of the same id can no longer both pass the stale guard.AbstractLwwUpserter. Five readers (Topic, Deck, SubTopic, Card, Session) now extendAbstractCursorReader. Each subclass declares only: model class, ownership check (upserter) or scope predicate (reader), and field mapping / row projection.Not migrated (different semantics, documented in their docblocks):
CardSubTopicUpserter— two-part ownership check is uniqueReviewUpserter— insert-only, no LWW update pathCardSubTopicReader— nestedwhereHastraversalReviewReader— append-only read with a different row shapeHeld-open status
Per agreement with the user, this PR stays open as draft through Phase 2 and merges after Phase 2 is complete. New Phase 2 upserters should extend these base classes directly (see
project_sync_upserter_refactor.mdmemory).Test plan
Supersedes stale #10 (auto-closed when phase/1-data-sync was deleted after merge).
🤖 Generated with Claude Code