LemmyNet / lemmy

🐀 A link aggregator and forum for the fediverse
https://join-lemmy.org
GNU Affero General Public License v3.0
13.31k stars 883 forks source link

Have all DB functions support either a postgres connection, or a postgres pool. #4745

Closed dessalines closed 2 months ago

dessalines commented 6 months ago

Requirements

Is your proposal related to a problem?

When using diesel transactions, you need to use an individual postgres connection, rather than passing in an entire DB pool. This is a problem because some API actions, like comment creation, have many operations, and should be done in a transaction.

Currently, nearly all our functions in db_schema use the pool, rather than individual connections. So for the above example, we'd have to create nearly identical functions just for the PgConn versions.

Describe the solution you'd like.

Create a PoolOrConnection enum, and alter all the functions in db_schema to use it, instead of the pool.

Add a helper function to the enum, that if given a pool, extracts the connection, but if given a connection, just uses it.

Describe alternatives you've considered.

NA

Additional context

4742

dessalines commented 6 months ago

Looks like this is already done, but that should be renamed:

https://github.com/LemmyNet/lemmy/blob/ec77c00ef80248e9eceec800082e34547f9aa084/crates/db_schema/src/utils.rs#L68

phiresky commented 6 months ago

this was done in #3420