Skip to content

Commit

Permalink
Update diesel, diesel_migrations, and uuid
Browse files Browse the repository at this point in the history
This implements the major upgrade of the Diesel crate from version 1 to
version 2. The relevant changes are the following:
- Diesel now requires mutable access to the `Connection` to perform any
  database interaction [0].
- The Diesel derive attributes need to be wrapped by `diesel()` now [1].
- The diesel_migration crate has been completely rewritten and the
  `embed_migrations!()` macro had to be replaced [2].
- The `connection::Connection::transaction` closure now takes one
  argument (the database connection). See [3] vs. [4].
  - I also decided to rename `database_connection` to `conn` in these
    cases to better differentiate the two (we could keep/shadow the
    outer variable for a smaller diff but it seems cleaner that way).

Note: The `Arc<PgConnection> -> Arc<Mutex<PgConnection>>` change is only
intended as a PoC. It is not a good idea as the mutex locking makes some
of the async blocks/code completely pointless. This problem already
existed before though. The documentation sates that the requirement of
mutable connections is expected to be a "straightforward change as the
connection already can execute only one query at a time". So this change
should only make matters worse by moving the locking up and therefore
increasing the time for which locks are held. This should only make the
execution a bit slower (not sure if it would even be noticeable) but it
increases the risk of deadlocks which is the real danger.
To avoid this issue we should use a connection pool instead (e.g., via
r2d2) but there are alternatives like trying to move the locking further
down in the code again or rewriting the database logic.

[0]: https://diesel.rs/guides/migration_guide.html#2-0-0-mutable-connection
[1]: https://diesel.rs/guides/migration_guide.html#2-0-0-derive-attributes
[2]: https://diesel.rs/guides/migration_guide.html#2-0-0-upgrade-migrations
[3]: https://docs.diesel.rs/2.0.x/diesel/connection/trait.Connection.html#method.transaction
[4]: https://docs.diesel.rs/1.4.x/diesel/connection/trait.Connection.html#method.transaction
  • Loading branch information
primeos-work committed May 22, 2023
1 parent c257952 commit 13bcbde
Show file tree
Hide file tree
Showing 22 changed files with 224 additions and 218 deletions.
93 changes: 40 additions & 53 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ config = { version = "0.11", default-features = false, features = [ "tom
csv = "1"
daggy = { version = "0.8", features = [ "serde" ] }
dialoguer = "0.10"
diesel = { version = "1", features = ["postgres", "chrono", "uuid", "serde_json"] }
diesel_migrations = "1"
diesel = { version = "2", features = ["postgres", "chrono", "uuid", "serde_json"] }
diesel_migrations = "2"
filters = "0.4"
futures = "0.3"
getset = "0.1"
Expand Down Expand Up @@ -74,7 +74,7 @@ tokio-stream = "0.1"
typed-builder = "0.14"
unindent = "0.2"
url = { version = "2", features = ["serde"] }
uuid = { version = "0.6", features = ["serde", "v4"] }
uuid = { version = "1", features = ["serde", "v4"] }
walkdir = "2"
which = "4"
xdg = "2"
Expand Down
Loading

0 comments on commit 13bcbde

Please sign in to comment.