-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Polymorphic relations broke SQL Transactions #10194
Comments
I don't use them, but it looks like this bug will affect pretty much every SQL connector, not just PG, since they all have copies of the same "is this my transaction" check:
|
@OnTheThirdDay as the author of the PR that caused this, any thoughts? It looks like this could be fixed by just using a shallow clone / spread of the options object instead of a deep clone? |
Oh I see.. would the following work? I think maybe just don't clone it would be fine. Sorry that I can't quite remember why I deep-cloned it instead of using the options directly..
|
@OnTheThirdDay Yep, that's roughly what I submitted as #10195, but I used spread syntax in my PR. Is there a particular benefit of using |
@mgabeler-lee-6rs I think not much difference, just a styling thing across the repo. |
…ions The `options` object will often have a `transaction`, which contains database connection resources that must not be cloned. Fixes #10194, introduced by #8026 Signed-off-by: Matthew Gabeler-Lee <[email protected]>
Describe the bug
PR #8026 broke using transactions with the PostgreSQL connector. The general problem is that doing
cloneDeep()
on theoptions
parameter is not safe, because the transaction object lives inside there, and a connection object inside there. Cloning the entire DB connection is just ... not a good idea in general, and specifically breaks the PG connector.This issue seems to apply to all the relations, but walking through it with
has-one
since that's where I found it.This line clones the options, which clones the transaction which clones the connector/connection:
loopback-next/packages/repository/src/relations/has-one/has-one.repository.ts
Line 185 in 7198aac
Then, if nothing worse happens, we get to the connector checking to see if the transaction belongs to it here: https://github.com/loopbackio/loopback-connector-postgresql/blob/4e9d5ca2993609692bd69d8ce5d9de7fcb9739f7/lib/postgresql.js#L180
This test fails, because it was cloned, and so it instead runs the query on a new connection. This poses multiple problems, including:
Logs
No response
Additional information
No response
Reproduction
https://github.com/mgabeler-lee-6rs/lb4-bug-10194
npm run migrate
npm start
GET
the/ping
endpointDEBUG=loopback:connector:postgresql
, or set some breakpoints, to see that the relation queries aren't using the transaction. Breakpoints will help prove the bug is as described.The text was updated successfully, but these errors were encountered: