Skip to content

fix(mediarequest): refactor to .save() instead of .update() for TypeORM - #1619

Closed
gauthier-th wants to merge 4 commits into
developfrom
fix-typeorm-update
Closed

gauthier-th wants to merge 4 commits into
developfrom
fix-typeorm-update

Conversation

@gauthier-th

@gauthier-th gauthier-th commented Apr 30, 2025 •

Copy link
Copy Markdown
Member

Description

The .update() method of TypeORM doesn't come with all the functionalities of the .save() method. Its goal is to partially update a Repository without doing any extra operation. This introduces issues with event from the database subscriber not triggered correctly, because of this partial update: typeorm/typeorm#2809 (comment)
A positive side effect of this change is that web push are now fixed.

This PR rollback to .save() instead of .update() to avoid the aforementioned issues, at the cost of some lifehook issues. These issues are happening with PostgreSQL because the .save() method shouldn't be used inside an Entity Listener, an Event Subscriber should be used instead: https://orkhan.gitbook.io/typeorm/docs/listeners-and-subscribers#what-is-an-entity-listener

EDIT (By @fallenbagel):

This PR consolidates all database‐accessing lifecycle hooks into the MediaRequestSubscriber as this is the recommended way of doing database calls.
See: https://orkhan.gitbook.io/typeorm/docs/listeners-and-subscribers#what-is-an-entity-listener.

It also tightens the cascade options on the MediaRequest→Media relation to only insert and remove to avoid hook‐within‐hook recursions that could cause unexpected behavior.

These changes should guard against recursive hook invocations in any database backend and simplify the entity lifecycle.

Upstream PR also opened: sct/overseerr#4124

Screenshot (if UI-related)

To-Dos

  • Successful build pnpm build
  • Translation keys pnpm i18n:extract
  • Database migration (if required)

The .update() method of TypeORM doesn't come with all the functionalities of the .save()
method. Its goal is to partially update a Repository without doing any extra operation. This
introduces issues with event from the database subscriber not triggered correctly, because of
this partial update: typeorm/typeorm#2809 (comment)

This PR rollback to .save() instead of .update() to avoid the aforementioned issues, at the
cost of some lifehook issues. These issues are happening with PostgreSQL because the .save()
method shouldn't be used inside an Entity Listener, an Event Subscriber should be used instead:
https://orkhan.gitbook.io/typeorm/docs/listeners-and-subscribers#what-is-an-entity-listener
@fallenbagel

Copy link
Copy Markdown
Member

Just to add to the description: the lifecycle issues also affect sqlite to an extent in some scenarios due to database access in event listeners (which is as mentioned, not supported)

@gauthier-th
gauthier-th marked this pull request as ready for review April 30, 2025 17:00
…emove

Replace `getRepository(Media)` calls with `event.manager` in the `afterRemove` hook so that
parent-media status resets run within the same transaction/QueryRunner (important for postgresql.
Doesnt affect sqlite).
…t hook recursion

Restrict cascade options on the MediaRequest→Media relation to only `insert` and `remove`to avoid
nested subscriber/AfterUpdate recursion when saving entities.
@Gauvino

Gauvino commented May 2, 2025 •

Copy link
Copy Markdown
Contributor

Tested cause I was having the same problem as reported in the issue and now it's fixed thanks !

@github-actions

github-actions Bot commented May 6, 2025

Copy link
Copy Markdown

This pull request has merge conflicts. Please resolve the conflicts so the PR can be successfully reviewed and merged.

@github-actions github-actions Bot added the merge conflict Cannot merge due to merge conflicts label May 6, 2025
@fallenbagel

Copy link
Copy Markdown
Member

Superseded by #1631

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge conflict Cannot merge due to merge conflicts preview PRs deployed for testing with tag `:preview-prxx`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants