update segment service methods for typeorm 1.0 cascading behavior - #3253
update segment service methods for typeorm 1.0 cascading behavior#3253bcb37 wants to merge 5 commits into
Conversation
There was a problem hiding this comment.
Pull request overview
Updates SegmentService to work around TypeORM 1.0+ cascade behavior changes by avoiding save() with loaded relations and instead managing subSegments relationships via the QueryBuilder relation API.
Changes:
- Updated list creation to link parent/child segments using
relation('subSegments')...add(...)rather than persisting loaded relations. - Updated segment upsert pipeline to (a) clear old
subSegmentsrelationships explicitly and (b) re-attach newsubSegmentsvia the relation API rather than cascading viasave(). - Updated unit tests to reflect the new repository
find()existence-check flow and relation-based linking.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| packages/backend/src/api/services/SegmentService.ts | Refactors segment/list upsert and relationship management to avoid cascade side effects by using TypeORM relation APIs. |
| packages/backend/test/unit/services/SegmentService.test.ts | Adjusts mocks and expectations for the new find() + relation-API based behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (5)
packages/backend/test/unit/services/SegmentService.test.ts:1082
- This test verifies
repo.saveomitssubSegments, but it doesn't verify the new QueryBuilder relation update path (.relation(...).add(...)) ran. Asserting.add(...)was called makes the test cover the behavior change this PR introduces.
// Verify that repo.save was called WITHOUT subSegments (relations are added via QueryBuilder)
expect(repo.save).toHaveBeenCalledWith({
packages/backend/test/unit/services/SegmentService.test.ts:1119
- This test verifies
repo.saveomitssubSegments, but doesn't assert that the relation is applied via QueryBuilder (.add(...)). Adding an assertion here helps ensure missing subsegment IDs are skipped and valid ones still get linked.
// Verify repo.save was called WITHOUT subSegments (relations are added via QueryBuilder)
expect(repo.save).toHaveBeenCalledWith({
packages/backend/src/api/services/SegmentService.ts:469
- In deleteList, the current guard throws
List ... not foundeven when the parent segment itself is missing. This makes the error misleading and harder to debug.
const parentSegment = await this.getSegmentById(parentSegmentId, logger);
if (!parentSegment || !parentSegment.subSegments.some((subSegment) => subSegment.id === segmentId)) {
throw new Error(`List ${segmentId} not found in parent segment ${parentSegmentId}`);
}
packages/backend/src/api/services/SegmentService.ts:1006
- The comment says missing subsegment references are skipped “silently”, but the code logs a warning for each missing ID. Update the comment so it matches the actual behavior.
// Skip unknown subsegment references silently — validation already warned about these
logger.warn({ message: `SubSegment: ${subSegmentId} not found, skipping reference.` });
packages/backend/test/unit/services/SegmentService.test.ts:1043
- These updated tests only assert that
repo.saveis called withoutsubSegments, but they don't assert that the relationship is actually set via the QueryBuilder. Adding an assertion that.add(...)was called will prevent regressions where relations stop being created.
This issue also appears in the following locations of the same file:
- line 1081
- line 1118
// Verify repo.save was called WITHOUT subSegments (relations are added via QueryBuilder)
expect(repo.save).toHaveBeenCalledWith({
No description provided.