elixir-sqlite / ecto_sqlite3

An Ecto SQLite3 adapter.
https://hexdocs.pm/ecto_sqlite3
MIT License
295 stars 44 forks source link

Immediate transaction #68

Closed LostKobrakai closed 2 years ago

LostKobrakai commented 2 years ago

I've a usecase for a read-then-update flow and I was considering using an immediate transaction to prevent the race condition of a concurrent transaction changing the db after the initial read, but before updates were written. I saw exqlite has some code around those, but I'm wondering if that can be exposed to ecto based transactions.

warmwaffles commented 2 years ago

I'm not sure how to set the mode for the transaction from within ecto, but the db_connection implementation supports it

https://github.com/elixir-sqlite/exqlite/blob/b45c25c6509e56530baccea97b1c1363b0892f02/lib/exqlite/connection.ex#L195-L196

LostKobrakai commented 2 years ago

I just noticed this just works: Edith.Repo.transaction(…, mode: :immediate) :)

warmwaffles commented 2 years ago

I didn't realize that transaction/2 took that as an option. It isn't documented AFAIK.

LostKobrakai commented 2 years ago

Yeah, things passed through to drivers are not the best documented thing in ecto.

warmwaffles commented 2 years ago

Perhaps a PR to upstream ecto would be welcome about passing the mode. I believe postgres and mysql implementations support the mode: directive.